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
9 Set.prototype.filter = function (f) {
10 return new Set([...this].filter(f));
15 this.data = this.datafilter(json);
16 let rows = Object.values(this.data);
17 rows = rows.filter(row => !row[3].length); // remove referenced categories
18 return rows.shuffle();
22 // find viable rows from json data
23 let ids = new Set(Object.keys(json));
24 const selection = {...json}; // clone
26 for (let cat of selection[''][3]) {
28 selection[cat][1] = 0; // keep root categories
31 if (this.preset.cat !== undefined) {
33 let children = [this.preset.cat];
34 for (let loop = 0; children.length && loop < 20; loop++) {
35 for (let child of children) ids.add(child.toString());
36 children = children.map(cat => json[cat][3]).filter(is => is).flat()
39 if (this.preset.image) {
40 ids = ids.filter(id => json[id][2]);
42 if (this.preset.level !== undefined) {
43 ids = ids.filter(id => json[id][1] <= this.preset.level);
46 // keep only wanted ids
47 for (let id in selection) {
48 if (id && !ids.has(id)) {
53 // retain orphaned references in grandparent categories
54 for (let id in selection) {
55 selection[id][3] = function subresolve(subs) {
57 return (subs || []).flatMap(sub =>
58 sub in selection ? [sub] : json[sub] ? subresolve(json[sub][3]) : []
68 if (input = window.location.hash.match(/\d+/)) {
69 this.preset.cat = parseInt(input[0]);
71 if (window.location.hash.match(/a/)) {
72 this.preset.level = 3;
75 fetch(dataurl).then(res => res.json()).then(json => {
76 this.words = this.dataselect(json)
82 this.history.push([new Date().toISOString(), ...args]);
87 window.onbeforeunload = null;
88 fetch('/word/report', {method: 'POST', body: JSON.stringify(this.history)});
91 constructor(dataurl) {
94 window.onbeforeunload = e => {
97 window.onhashchange = e => {