-
Notifications
You must be signed in to change notification settings - Fork 1
/
generateStats.js
84 lines (77 loc) · 2.44 KB
/
generateStats.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
/**
* Gets all dictionaries from indexedDB
* @returns {Promise<Array<Dict>>}
*/
async function getAllDicts() {
return new Promise(async (resolve, reject) => {
let request = await window.indexedDB.open('dict');
/**
* @param {IDBRequest} event
*/
request.onsuccess = function (event) {
/**
* @type {IDBDatabase}
*/
let db = event.target.result;
let transaction = db.transaction(['dictionaries'], 'readonly');
let store = transaction.objectStore('dictionaries');
const dicts = [];
// open a cursor to iterate over all entries
let request = store.openCursor();
request.onsuccess = function (event) {
let cursor = event.target.result;
if (cursor) {
let entry = cursor.value;
dicts.push(entry);
cursor.continue();
} else {
console.log('No more entries');
resolve(dicts);
}
};
};
});
}
const entryTypes = ['terms', 'termMeta', 'kanji', 'kanjiMeta'];
(async () => {
const dicts = await getAllDicts();
const output = [];
for (let dict of dicts) {
const { title, author, revision, url, description, attribution } = dict;
let entryCount = 0;
for (const entryType of entryTypes) {
entryCount += dict.counts[entryType]?.total ?? 0;
}
output.push({
title: sanitizeForMarkdown(title),
author: sanitizeForMarkdown(author),
revision: sanitizeForMarkdown(revision),
url: sanitizeForMarkdown(url),
description: sanitizeForMarkdown(description),
attribution: sanitizeForMarkdown(attribution),
entryCount,
});
}
// export to md table
let md = '| Title | Entry Count | Information |\n';
md += '| ------ | ----------- | ----------- |\n';
for (const row of output) {
let line = `| ${row.title} | ${row.entryCount} |`;
if (row.author) line += ` **Author**: ${row.author} <br />`;
if (row.revision) line += ` **Revision**: ${row.revision} <br />`;
if (row.url) line += ` **URL**: ${row.url} <br />`;
if (row.attribution) line += ` **Attribution**: ${row.attribution} <br />`;
if (row.description)
line += ` **Description**:<br /> ${row.description} <br />`;
line += ' |';
md += line + '\n';
}
console.log(md);
})();
function sanitizeForMarkdown(str) {
if (!str) return '';
if (typeof str !== 'string') return str;
str = str.replace(/\n/g, '<br />');
str = str.replace(/\|/g, '\\|');
return str;
}