-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlib.js
More file actions
639 lines (604 loc) · 23.5 KB
/
Copy pathlib.js
File metadata and controls
639 lines (604 loc) · 23.5 KB
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
"use strict";
function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
function _defineProperty(e, r, t) { return (r = _toPropertyKey(r)) in e ? Object.defineProperty(e, r, { value: t, enumerable: !0, configurable: !0, writable: !0 }) : e[r] = t, e; }
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : i + ""; }
function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
function _toConsumableArray(r) { return _arrayWithoutHoles(r) || _iterableToArray(r) || _unsupportedIterableToArray(r) || _nonIterableSpread(); }
function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
function _iterableToArray(r) { if ("undefined" != typeof Symbol && null != r[Symbol.iterator] || null != r["@@iterator"]) return Array.from(r); }
function _arrayWithoutHoles(r) { if (Array.isArray(r)) return _arrayLikeToArray(r); }
function _slicedToArray(r, e) { return _arrayWithHoles(r) || _iterableToArrayLimit(r, e) || _unsupportedIterableToArray(r, e) || _nonIterableRest(); }
function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
function _unsupportedIterableToArray(r, a) { if (r) { if ("string" == typeof r) return _arrayLikeToArray(r, a); var t = {}.toString.call(r).slice(8, -1); return "Object" === t && r.constructor && (t = r.constructor.name), "Map" === t || "Set" === t ? Array.from(r) : "Arguments" === t || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(t) ? _arrayLikeToArray(r, a) : void 0; } }
function _arrayLikeToArray(r, a) { (null == a || a > r.length) && (a = r.length); for (var e = 0, n = Array(a); e < a; e++) n[e] = r[e]; return n; }
function _iterableToArrayLimit(r, l) { var t = null == r ? null : "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (null != t) { var e, n, i, u, a = [], f = !0, o = !1; try { if (i = (t = t.call(r)).next, 0 === l) { if (Object(t) !== t) return; f = !1; } else for (; !(f = (e = i.call(t)).done) && (a.push(e.value), a.length !== l); f = !0); } catch (r) { o = !0, n = r; } finally { try { if (!f && null != t["return"] && (u = t["return"](), Object(u) !== u)) return; } finally { if (o) throw n; } } return a; } }
function _arrayWithHoles(r) { if (Array.isArray(r)) return r; }
function hasKanji(str) { return /[\u4E00-\u9FFF\u3400-\u4DBF]/.test(str); }
// ── Furigana helper ──────────────────────────────────────────────────────────
function furiganaHTML(word, reading) {
if (!reading || reading === word || !hasKanji(word)) return word;
return '<ruby>' + word + '<rt>' + reading + '</rt></ruby>';
}
// ── Level helper ─────────────────────────────────────────────────────────────
function dayToLevel(day) {
if (day <= 365) return 'N5';
if (day <= 660) return 'N4';
if (day <= 960) return 'N3';
if (day <= 1320) return 'N2';
return 'N1';
}
function exerciseCap(day) {
if (day <= 660) return 5;
if (day <= 960) return 7;
return 9;
}
// ── SRS (SM-2) ───────────────────────────────────────────────────────────────
function sm2Update(card, grade) {
var _ref = card || {},
_ref$interval = _ref.interval,
interval = _ref$interval === void 0 ? 1 : _ref$interval,
_ref$ef = _ref.ef,
ef = _ref$ef === void 0 ? 2.5 : _ref$ef,
_ref$reps = _ref.reps,
reps = _ref$reps === void 0 ? 0 : _ref$reps;
if (grade >= 3) {
if (reps === 0) interval = 1;else if (reps === 1) interval = 6;else interval = Math.round(interval * ef);
reps++;
ef = Math.max(1.3, ef + 0.1 - (5 - grade) * (0.08 + (5 - grade) * 0.02));
} else {
reps = 0;
interval = 1;
}
var due = Date.now() + interval * 24 * 60 * 60 * 1000;
return {
interval: interval,
ef: ef,
reps: reps,
due: due
};
}
function cardId(type, day, idx) {
return "".concat(type, "_").concat(day, "_").concat(idx);
}
function addDayCards(srs, lesson) {
var now = Date.now();
(lesson.vocab || []).forEach(function (v, i) {
var id = cardId('v', lesson.day, i);
if (!srs[id]) srs[id] = {
interval: 1,
ef: 2.5,
reps: 0,
due: now
};
});
(lesson.chars || []).forEach(function (c, i) {
var id = cardId('c', lesson.day, i);
if (!srs[id]) srs[id] = {
interval: 1,
ef: 2.5,
reps: 0,
due: now
};
});
return srs;
}
function getDueCards(srs) {
var now = Date.now();
return Object.entries(srs).filter(function (_ref2) {
var _ref3 = _slicedToArray(_ref2, 2),
c = _ref3[1];
return c.due <= now;
}).map(function (_ref4) {
var _ref5 = _slicedToArray(_ref4, 1),
id = _ref5[0];
return id;
});
}
function cardToItem(id, srs) {
var parts = id.split('_');
var type = parts[0],
day = parseInt(parts[1]),
idx = parseInt(parts[2]);
var lesson = curriculum[day - 1];
if (!lesson) return null;
if (type === 'v') {
var v = (lesson.vocab || [])[idx];
if (!v) return null;
return {
id: id,
type: 'vocab',
front: v[0],
back: v[2],
reading: v[1] !== v[0] ? v[1] : null,
day: day
};
} else {
var c = (lesson.chars || [])[idx];
if (!c) return null;
return {
id: id,
type: 'char',
front: c[0],
back: c[1],
day: day
};
}
}
// ── Helpers ─────────────────────────────────────────────────────────────────
function rndShuffle(arr) {
var a = _toConsumableArray(arr);
for (var i = a.length - 1; i > 0; i--) {
var j = Math.floor(Math.random() * (i + 1));
var _ref7 = [a[j], a[i]];
a[i] = _ref7[0];
a[j] = _ref7[1];
}
return a;
}
function buildExercises(lesson) {
var exs = [];
var allVocab = curriculum.flatMap(function (d) {
return d.vocab || [];
});
var allChars = curriculum.flatMap(function (d) {
return d.chars || [];
});
// Listening exercise: hear a word, pick its meaning
if (lesson.vocab && lesson.vocab.length >= 2 && window.speechSynthesis) {
var vocab = rndShuffle(lesson.vocab);
var v = vocab[0];
var wrongM = rndShuffle(allVocab.filter(function (w) {
return w[2] && w[2].trim() && w[2] !== v[2];
})).slice(0, 3);
var opts = rndShuffle([v[2]].concat(_toConsumableArray(wrongM.map(function (w) {
return w[2];
}))));
exs.push({
type: 'listen',
prompt: 'Listen and choose the meaning:',
question: v[0],
audio: v[0],
options: opts,
correct: opts.indexOf(v[2])
});
}
// ── Char exercises (hiragana/katakana/kanji days) ──
if (lesson.chars && lesson.chars.length > 0) {
var chars = rndShuffle(lesson.chars);
// MC: character → romaji/reading
var c0 = chars[0];
var wrongs = rndShuffle(allChars.filter(function (c) {
return c[1] && c[1].trim() && c[1] !== c0[1];
})).slice(0, 3);
var mcOpts = rndShuffle([c0[1]].concat(_toConsumableArray(wrongs.map(function (c) {
return c[1];
}))));
exs.push({
type: 'mc',
prompt: 'What is the reading for this character?',
question: c0[0],
options: mcOpts,
correct: mcOpts.indexOf(c0[1])
});
// Typing: character → reading
if (chars.length > 1) {
var c1 = chars[1];
exs.push({
type: 'typing',
prompt: 'Type the reading for this character:',
question: c1[0],
answers: [c1[1]],
placeholder: 'e.g. ' + c1[1]
});
}
}
// ── Vocab exercises ──
if (lesson.vocab && lesson.vocab.length > 0) {
var _vocab = rndShuffle(lesson.vocab);
// MC: word → meaning
var v0 = _vocab[0];
var _wrongM = rndShuffle(allVocab.filter(function (w) {
return w[2] && w[2].trim() && w[2] !== v0[2];
})).slice(0, 3);
var mcM = rndShuffle([v0[2]].concat(_toConsumableArray(_wrongM.map(function (w) {
return w[2];
}))));
exs.push({
type: 'mc',
prompt: 'What does this word mean?',
question: v0[0],
options: mcM,
correct: mcM.indexOf(v0[2])
});
// MC: meaning → word (pick the right Japanese)
if (_vocab.length > 1) {
var v1 = _vocab[1];
var wrongW = rndShuffle(allVocab.filter(function (w) {
return w[0] && w[0].trim() && w[0] !== v1[0];
})).slice(0, 3);
var mcW = rndShuffle([v1[0]].concat(_toConsumableArray(wrongW.map(function (w) {
return w[0];
}))));
exs.push({
type: 'mc',
prompt: 'Which word means "' + v1[2] + '"?',
question: '',
options: mcW,
correct: mcW.indexOf(v1[0])
});
}
// Typing: word → English meaning
if (_vocab.length > 2) {
var v2 = _vocab[2];
var answers = v2[2].split(/[\/,]/).map(function (s) {
return s.trim();
}).filter(Boolean);
exs.push({
type: 'typing',
prompt: 'What does this word mean? (type in English)',
question: v2[0],
answers: answers,
placeholder: 'English meaning...'
});
}
}
// Listening: hear a word, pick meaning
if (lesson.vocab && lesson.vocab.length > 0) {
var vL = rndShuffle(lesson.vocab)[0];
var wrongL = rndShuffle(allVocab.filter(function (w) {
return w[2] !== vL[2];
})).slice(0, 3);
var mcL = rndShuffle([vL[2]].concat(_toConsumableArray(wrongL.map(function (w) {
return w[2];
}))));
exs.push({
type: 'listen',
prompt: 'Listen and choose the meaning:',
question: vL[0],
audio: vL[0],
options: mcL,
correct: mcL.indexOf(vL[2])
});
}
// ── N3+ exercise types (day > 660) ──────────────────────────────────────
var day = lesson.day || 0;
// Reading comprehension (N3+)
if (day > 660 && lesson.passage && lesson.passage.questions && lesson.passage.questions.length > 0) {
var q0 = lesson.passage.questions[0];
exs.push({
type: 'reading',
prompt: 'Read the passage and answer:',
question: q0.question_jp || q0.question_en,
answers: [q0.answer],
passage: lesson.passage.text_jp,
placeholder: 'Type your answer...'
});
}
// Conjugation exercise (N3+, verb days)
if (day > 660 && lesson.type === 'verbs' && lesson.vocab && lesson.vocab.length >= 2) {
var verbForms = ['て-form', 'ない-form', 'た-form', 'potential', 'passive', 'causative', 'volitional'];
var form = verbForms[day % verbForms.length];
var vConj = rndShuffle(lesson.vocab)[0];
exs.push({
type: 'conjugation',
prompt: 'Conjugate to ' + form + ':',
question: vConj[0],
answers: [vConj[1]],
targetForm: form,
placeholder: form + '...'
});
}
// Pair match (N3+, verb days with transitive/intransitive pairs)
if (day > 660 && lesson.type === 'verbs' && lesson.vocab && lesson.vocab.length >= 4) {
var pairs = lesson.vocab.slice(0, 4);
var pairItems = rndShuffle(pairs.map(function (v) { return v[0]; }));
var pairAnswers = pairs.map(function (v) { return v[2]; });
exs.push({
type: 'pair_match',
prompt: 'Match each word to its meaning:',
question: '',
items: pairItems,
answers: pairAnswers,
options: rndShuffle(pairAnswers)
});
}
// Fill-in-the-blank (N3+, grammar days)
if (day > 660 && lesson.grammar && lesson.grammar.pattern && lesson.grammar.example_jp) {
var gPat = lesson.grammar.pattern;
var wrongPats = rndShuffle(curriculum.filter(function (d) {
return d.grammar && d.grammar.pattern && d.grammar.pattern !== gPat;
}).map(function (d) { return d.grammar.pattern; })).slice(0, 3);
if (wrongPats.length >= 2) {
var fillOpts = rndShuffle([gPat].concat(wrongPats));
exs.push({
type: 'fill_blank',
prompt: 'Choose the correct grammar pattern:',
question: lesson.grammar.meaning,
options: fillOpts,
correct: fillOpts.indexOf(gPat)
});
}
}
// Synonym exercise (N2+)
if (day > 960 && lesson.vocab && lesson.vocab.length >= 3) {
var vSyn = rndShuffle(lesson.vocab)[0];
var wrongSyn = rndShuffle(allVocab.filter(function (w) {
return w[2] && w[2].trim() && w[2] !== vSyn[2] && w[0] !== vSyn[0];
})).slice(0, 3);
var synOpts = rndShuffle([vSyn[2]].concat(wrongSyn.map(function (w) { return w[2]; })));
exs.push({
type: 'synonym',
prompt: 'Choose the closest meaning to: ' + vSyn[0],
question: vSyn[0],
options: synOpts,
correct: synOpts.indexOf(vSyn[2])
});
}
// Reorder exercise (N2+, grammar days)
if (day > 960 && lesson.grammar && lesson.grammar.example_jp) {
var words = lesson.grammar.example_jp.replace(/[。、!?]/g, '').split('');
if (words.length >= 4) {
var chunks = [];
for (var ci = 0; ci < words.length; ci += 2) {
chunks.push(words.slice(ci, ci + 2).join(''));
}
if (chunks.length >= 3) {
exs.push({
type: 'reorder',
prompt: 'Arrange into a correct sentence:',
question: lesson.grammar.example_en,
items: rndShuffle(chunks),
answer: chunks.join('')
});
}
}
}
// Error find (N2+)
if (day > 960 && lesson.grammar && lesson.grammar.pattern) {
exs.push({
type: 'error_find',
prompt: 'Which grammar pattern is used correctly?',
question: '',
options: [lesson.grammar.example_jp, lesson.grammar.example_jp.split('').reverse().join('')],
correct: 0
});
}
// Kanji reading (N2+, kanji days)
if (day > 960 && lesson.type === 'kanji' && lesson.chars && lesson.chars.length >= 2) {
var kChar = rndShuffle(lesson.chars)[0];
var wrongK = rndShuffle(allChars.filter(function (c) {
return c[1] && c[1].trim() && c[1] !== kChar[1];
})).slice(0, 3);
var kOpts = rndShuffle([kChar[1]].concat(wrongK.map(function (c) { return c[1]; })));
exs.push({
type: 'kanji_reading',
prompt: 'Select the correct reading:',
question: kChar[0],
options: kOpts,
correct: kOpts.indexOf(kChar[1])
});
}
// Passage cloze (N1+)
if (day > 1320 && lesson.passage && lesson.passage.text_jp) {
exs.push({
type: 'passage_cloze',
prompt: 'Fill in the blank in the passage:',
question: lesson.passage.text_jp,
answers: lesson.passage.questions ? [lesson.passage.questions[0].answer] : [],
placeholder: 'Type the missing word...'
});
}
// Register exercise (N1+)
if (day > 1320 && lesson.vocab && lesson.vocab.length >= 2) {
var regV = rndShuffle(lesson.vocab)[0];
var regOpts = rndShuffle(['Formal', 'Informal', 'Honorific', 'Humble']);
exs.push({
type: 'register',
prompt: 'What formality level is this expression?',
question: regV[0],
options: regOpts,
correct: 0
});
}
// Paraphrase (N1+)
if (day > 1320 && lesson.grammar && lesson.grammar.example_jp && lesson.grammar.example_en) {
exs.push({
type: 'paraphrase',
prompt: 'Which sentence has the same meaning?',
question: lesson.grammar.example_jp,
options: [lesson.grammar.example_en],
correct: 0
});
}
var cap = exerciseCap(day);
return rndShuffle(exs).slice(0, cap);
}
function normAns(s) {
return s.trim().toLowerCase().replace(/[!"#$%&'()*+,./:;<=>?@[\]^_`{|}~\\]/g, '').trim();
}
function checkTyping(userAns, answers) {
var u = userAns.trim().toLowerCase();
var uNorm = normAns(userAns);
return answers.some(function (a) {
var parts = a.split(/[\/,]/).map(function (s) {
return s.trim().toLowerCase();
});
var partsNorm = a.split(/[\/,]/).map(function (s) {
return normAns(s);
});
return a.trim().toLowerCase() === u || parts.includes(u) || partsNorm.includes(uNorm);
});
}
// ── Storage helpers ──────────────────────────────────────────────────────────
function storageAvailable() {
try {
var test = '__storage_test__';
localStorage.setItem(test, '1');
localStorage.removeItem(test);
return true;
} catch (e) {
return false;
}
}
// safeSave: wraps localStorage.setItem; dispatches 'storage-save-error' event on failure.
// Returns { ok: boolean, error: string|null }.
function safeSave(key, value) {
try {
localStorage.setItem(key, value);
return { ok: true, error: null };
} catch (e) {
var msg = e && e.message ? e.message : String(e);
if (typeof window !== 'undefined') {
window.dispatchEvent(new CustomEvent('storage-save-error', { detail: msg }));
}
return { ok: false, error: msg };
}
}
// ── SVG sanitization ─────────────────────────────────────────────────────────
// Strips XSS vectors from untrusted SVG strings before injecting into the DOM.
// Regex-based (no DOMParser) so it works headlessly in Node test environments.
function sanitizeSvg(raw) {
if (!raw || typeof raw !== 'string') return '';
var out = raw;
// Remove <script> blocks
out = out.replace(/<script[\s\S]*?<\/script>/gi, '');
// Remove event handler attributes (onclick, onload, onerror, …)
out = out.replace(/\s+on\w+\s*=\s*(?:"[^"]*"|'[^']*'|[^\s>]*)/gi, '');
// Remove <foreignObject> blocks (can embed arbitrary HTML)
out = out.replace(/<foreignObject[\s\S]*?<\/foreignObject>/gi, '');
// Remove javascript: href values
out = out.replace(/href\s*=\s*["']?\s*javascript:[^"'\s>]*/gi, '');
// Remove <use> tags referencing external URLs (SVG sprite injection)
out = out.replace(/<use[^>]*(?:href|xlink:href)\s*=\s*["'][^"']*:\/\/[^"']*["'][^>]*>/gi, '');
return out;
}
// ── SRS: SM-2 ───────────────────────────────────────────────────────────────
var SRS_KEY = 'n5_srs';
function srsLoad() {
try {
return JSON.parse(localStorage.getItem(SRS_KEY)) || {};
} catch (e) {
return {};
}
}
function srsSave(cards) {
safeSave(SRS_KEY, JSON.stringify(cards));
}
function srsAddCards(dayLesson, cards) {
var changed = false;
var now = Date.now();
(dayLesson.vocab || []).forEach(function (v, i) {
var id = "v_".concat(dayLesson.day, "_").concat(i);
if (!cards[id]) {
cards[id] = {
id: id,
type: 'vocab',
front: v[0],
back: v[2],
reading: v[1],
interval: 1,
ease: 2.5,
due: now,
reps: 0
};
changed = true;
}
});
(dayLesson.chars || []).forEach(function (c, i) {
var id = "c_".concat(dayLesson.day, "_").concat(i);
if (!cards[id]) {
cards[id] = {
id: id,
type: 'char',
front: c[0],
back: c[1],
interval: 1,
ease: 2.5,
due: now,
reps: 0
};
changed = true;
}
});
return changed;
}
function srsReview(card, quality) {
var q = [0, 3, 4, 5][quality];
var interval = card.interval,
ease = card.ease,
reps = card.reps;
if (q < 3) {
reps = 0;
interval = 1;
} else {
if (reps === 0) interval = 1;else if (reps === 1) interval = 6;else interval = Math.round(interval * ease);
reps += 1;
}
ease = Math.max(1.3, ease + 0.1 - (5 - q) * (0.08 + (5 - q) * 0.02));
var due = Date.now() + interval * 86400000;
return _objectSpread(_objectSpread({}, card), {}, {
interval: interval,
ease: ease,
reps: reps,
due: due
});
}
function srsDueCards(cards) {
var now = Date.now();
return Object.values(cards).filter(function (c) {
return c.due <= now;
});
}
// exportProgress: collects all jlpt_*/n5_* keys from localStorage and returns
// a JSON-serialisable object with a version field.
// Returns null if localStorage is unavailable.
function exportProgress() {
try {
var KEYS = ['n5_day', 'n5_completed', 'n5_furigana', 'n5_srs', 'n5_2025', 'jlpt_tts_rate'];
var data = { version: 1, exported: new Date().toISOString(), keys: {} };
KEYS.forEach(function (k) {
var v = localStorage.getItem(k);
if (v !== null) data.keys[k] = v;
});
return data;
} catch (e) {
return null;
}
}
// validateProgressData: checks that a parsed progress object has the expected
// shape. Returns { valid: boolean, error: string|null }.
function validateProgressData(data) {
if (!data || typeof data !== 'object') return { valid: false, error: 'not an object' };
if (data.version !== 1) return { valid: false, error: 'unsupported version: ' + data.version };
if (!data.keys || typeof data.keys !== 'object') return { valid: false, error: 'missing keys field' };
var allowedKeys = ['n5_day', 'n5_completed', 'n5_furigana', 'n5_srs', 'n5_2025', 'jlpt_tts_rate'];
var found = Object.keys(data.keys);
for (var i = 0; i < found.length; i++) {
if (allowedKeys.indexOf(found[i]) === -1) return { valid: false, error: 'unknown key: ' + found[i] };
}
return { valid: true, error: null };
}
// validateCurriculum: runtime sanity-check on the curriculum array.
// Returns { valid: boolean, error: string|null }.
function validateCurriculum(cur) {
if (!Array.isArray(cur)) return { valid: false, error: 'curriculum is not an array' };
if (cur.length !== 1720) return { valid: false, error: 'curriculum length is ' + cur.length + ', expected 1720' };
var required = ['day', 'title', 'type'];
var spots = [0, 1, 364, 365, 1719];
for (var i = 0; i < spots.length; i++) {
var idx = spots[i];
var lesson = cur[idx];
if (!lesson) return { valid: false, error: 'curriculum[' + idx + '] is missing' };
for (var j = 0; j < required.length; j++) {
if (lesson[required[j]] === undefined) {
return { valid: false, error: 'curriculum[' + idx + '] missing field: ' + required[j] };
}
}
if (lesson.day !== idx + 1) {
return { valid: false, error: 'curriculum[' + idx + '].day is ' + lesson.day + ', expected ' + (idx + 1) };
}
}
return { valid: true, error: null };
}