1 Array.prototype.shuffle = function () {
2 for (let i = this.length - 1; i > 0; i--) {
3 const j = Math.floor(Math.random() * (i + 1)); // random index 0..i
4 [this[i], this[j]] = [this[j], this[i]]; // swap elements
10 constructor(data, root = undefined) {
12 this.selection = root || this.data[''][3];
13 this.visible = new Set(root || Object.keys(data).flatMap(id => id && parseInt(id)));
16 for (let loop = 0; children.length && loop < 20; loop++) {
17 for (let child of children) this.visible.add(child);
18 children = children.map(cat => data[cat][3]).filter(is => is).flat();
24 // keep only matching entries, and root selection regardless
25 this.visible = new Set([...this.visible].filter(f).concat(this.selection));
29 for (let i of this.selection) {
38 let order = [...this.visible.keys()].shuffle();
39 for (let i of order) {
48 return this.visible.has(id);
52 let refs = this.data[id][3];
56 for (let ref of refs) {
57 // retain orphaned references in grandparent categories
59 refs = refs.concat(this.subs(ref));
70 const row = this.data[id];
75 return row[0].replace(/\/.*/, ''); // primary form
80 return `/data/word/${size}/${row[2]}.jpg`;
83 return p.subs(id).map(e => p.get(e));
91 this.data = this.datafilter(json);
92 return [...this.data.random()];
96 // find viable rows from json data
97 const selection = new Words(json, this.preset.cat);
99 if (this.preset.images) {
100 selection.filter(id => json[id][2]);
102 if (this.preset.level !== undefined) {
103 selection.filter(id => json[id][1] <= this.preset.level);
106 if (this.preset.distinct) {
107 // remove referenced categories
108 selection.filter(id => !selection.get(id).subs.length);
114 configure(params = window.location.hash.split('#')) {
115 const opts = new Map(params.map(arg => arg.split(/[:=](.*)/)));
116 for (let [query, val] of opts) {
117 if (query.match(/^\d+$/)) {
118 this.preset.cat = [parseInt(query)];
120 else if (query === 'level') {
121 this.preset.level = parseInt(val);
124 this.preset[query] = val;
127 this.preset.dataurl = `/data/wordlist.${this.preset.lang}.json`
131 this.form = document.getElementById('quiz');
136 fetch(this.preset.dataurl).then(res => res.json()).then(json => {
137 this.words = this.dataselect(json)
143 this.history.push([new Date().toISOString(), ...args]);
148 window.onbeforeunload = null;
149 fetch('/word/report', {method: 'POST', body: JSON.stringify(this.history)});
153 this.preset = {images: true, lang: 'en'};
156 window.onbeforeunload = e => {
159 window.onhashchange = e => {