ts-fsrs 5.3.3 → 5.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +475 -0
- package/dist/index.cjs +57 -53
- package/dist/index.cjs.map +1 -1
- package/dist/index.mjs +57 -53
- package/dist/index.mjs.map +1 -1
- package/dist/index.umd.js +59 -53
- package/dist/index.umd.js.map +1 -1
- package/package.json +1 -1
package/dist/index.mjs
CHANGED
|
@@ -1,3 +1,18 @@
|
|
|
1
|
+
class FSRSError extends Error {
|
|
2
|
+
constructor(message = "FSRS Error") {
|
|
3
|
+
super(message);
|
|
4
|
+
this.name = "FSRSError";
|
|
5
|
+
Error.captureStackTrace?.(this, FSRSError);
|
|
6
|
+
}
|
|
7
|
+
}
|
|
8
|
+
class FSRSValidationError extends FSRSError {
|
|
9
|
+
constructor(message) {
|
|
10
|
+
super(message);
|
|
11
|
+
this.name = "FSRSValidationError";
|
|
12
|
+
Error.captureStackTrace?.(this, FSRSValidationError);
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
|
|
1
16
|
var State = /* @__PURE__ */ ((State2) => {
|
|
2
17
|
State2[State2["New"] = 0] = "New";
|
|
3
18
|
State2[State2["Learning"] = 1] = "Learning";
|
|
@@ -29,13 +44,13 @@ class TypeConvert {
|
|
|
29
44
|
const restOfString = value.slice(1).toLowerCase();
|
|
30
45
|
const ret = Rating[`${firstLetter}${restOfString}`];
|
|
31
46
|
if (ret === void 0) {
|
|
32
|
-
throw new
|
|
47
|
+
throw new FSRSValidationError(`Invalid rating:[${value}]`);
|
|
33
48
|
}
|
|
34
49
|
return ret;
|
|
35
50
|
} else if (typeof value === "number") {
|
|
36
51
|
return value;
|
|
37
52
|
}
|
|
38
|
-
throw new
|
|
53
|
+
throw new FSRSValidationError(`Invalid rating:[${value}]`);
|
|
39
54
|
}
|
|
40
55
|
static state(value) {
|
|
41
56
|
if (typeof value === "string") {
|
|
@@ -43,13 +58,13 @@ class TypeConvert {
|
|
|
43
58
|
const restOfString = value.slice(1).toLowerCase();
|
|
44
59
|
const ret = State[`${firstLetter}${restOfString}`];
|
|
45
60
|
if (ret === void 0) {
|
|
46
|
-
throw new
|
|
61
|
+
throw new FSRSValidationError(`Invalid state:[${value}]`);
|
|
47
62
|
}
|
|
48
63
|
return ret;
|
|
49
64
|
} else if (typeof value === "number") {
|
|
50
65
|
return value;
|
|
51
66
|
}
|
|
52
|
-
throw new
|
|
67
|
+
throw new FSRSValidationError(`Invalid state:[${value}]`);
|
|
53
68
|
}
|
|
54
69
|
static time(value) {
|
|
55
70
|
if (value instanceof Date) {
|
|
@@ -63,12 +78,12 @@ class TypeConvert {
|
|
|
63
78
|
if (!Number.isNaN(timestamp)) {
|
|
64
79
|
return new Date(timestamp);
|
|
65
80
|
} else {
|
|
66
|
-
throw new
|
|
81
|
+
throw new FSRSValidationError(`Invalid date:[${value}]`);
|
|
67
82
|
}
|
|
68
83
|
} else if (typeof value === "number") {
|
|
69
84
|
return new Date(value);
|
|
70
85
|
}
|
|
71
|
-
throw new
|
|
86
|
+
throw new FSRSValidationError(`Invalid date:[${value}]`);
|
|
72
87
|
}
|
|
73
88
|
static review_log(log) {
|
|
74
89
|
return {
|
|
@@ -104,7 +119,7 @@ function date_scheduler(now, t, isDay) {
|
|
|
104
119
|
}
|
|
105
120
|
function date_diff(now, pre, unit) {
|
|
106
121
|
if (!now || !pre) {
|
|
107
|
-
throw new
|
|
122
|
+
throw new FSRSValidationError("Invalid date");
|
|
108
123
|
}
|
|
109
124
|
const diff = TypeConvert.time(now).getTime() - TypeConvert.time(pre).getTime();
|
|
110
125
|
let r = 0;
|
|
@@ -230,7 +245,7 @@ const ConvertStepUnitToMinutes = (step) => {
|
|
|
230
245
|
const unit = step.slice(-1);
|
|
231
246
|
const value = parseInt(step.slice(0, -1), 10);
|
|
232
247
|
if (Number.isNaN(value) || !Number.isFinite(value) || value < 0) {
|
|
233
|
-
throw new
|
|
248
|
+
throw new FSRSValidationError(`Invalid step value: ${step}`);
|
|
234
249
|
}
|
|
235
250
|
switch (unit) {
|
|
236
251
|
case "m":
|
|
@@ -240,7 +255,9 @@ const ConvertStepUnitToMinutes = (step) => {
|
|
|
240
255
|
case "d":
|
|
241
256
|
return value * 1440;
|
|
242
257
|
default:
|
|
243
|
-
throw new
|
|
258
|
+
throw new FSRSValidationError(
|
|
259
|
+
`Invalid step unit: ${step}, expected m/h/d`
|
|
260
|
+
);
|
|
244
261
|
}
|
|
245
262
|
};
|
|
246
263
|
const BasicLearningStepsStrategy = (params, state, cur_step) => {
|
|
@@ -337,8 +354,8 @@ class AbstractScheduler {
|
|
|
337
354
|
this.init();
|
|
338
355
|
}
|
|
339
356
|
checkGrade(grade) {
|
|
340
|
-
if (!Number.isFinite(grade) || grade <
|
|
341
|
-
throw new
|
|
357
|
+
if (!Number.isFinite(grade) || grade < 1 || grade > 4) {
|
|
358
|
+
throw new FSRSValidationError(`Invalid grade "${grade}",expected 1-4`);
|
|
342
359
|
}
|
|
343
360
|
}
|
|
344
361
|
init() {
|
|
@@ -481,7 +498,7 @@ function alea(seed) {
|
|
|
481
498
|
return prng;
|
|
482
499
|
}
|
|
483
500
|
|
|
484
|
-
const version="5.
|
|
501
|
+
const version="5.4.0";
|
|
485
502
|
|
|
486
503
|
const default_request_retention = 0.9;
|
|
487
504
|
const default_maximum_interval = 36500;
|
|
@@ -570,13 +587,13 @@ const clipParameters = (parameters, numRelearningSteps, enableShortTerm = defaul
|
|
|
570
587
|
);
|
|
571
588
|
};
|
|
572
589
|
const checkParameters = (parameters) => {
|
|
573
|
-
const invalid = parameters.find(
|
|
574
|
-
(param) => !Number.isFinite(param) && !Number.isNaN(param)
|
|
575
|
-
);
|
|
590
|
+
const invalid = parameters.find((param) => !Number.isFinite(param));
|
|
576
591
|
if (invalid !== void 0) {
|
|
577
|
-
throw
|
|
592
|
+
throw new FSRSValidationError(
|
|
593
|
+
`Non-finite or NaN value in parameters ${parameters}`
|
|
594
|
+
);
|
|
578
595
|
} else if (![17, 19, 21].includes(parameters.length)) {
|
|
579
|
-
throw
|
|
596
|
+
throw new FSRSValidationError(
|
|
580
597
|
`Invalid parameter length: ${parameters.length}. Must be 17, 19 or 21 for FSRSv4, 5 and 6 respectively.`
|
|
581
598
|
);
|
|
582
599
|
}
|
|
@@ -694,7 +711,9 @@ class FSRSAlgorithm {
|
|
|
694
711
|
*/
|
|
695
712
|
calculate_interval_modifier(request_retention) {
|
|
696
713
|
if (request_retention <= 0 || request_retention > 1) {
|
|
697
|
-
throw new
|
|
714
|
+
throw new FSRSValidationError(
|
|
715
|
+
"Requested retention rate should be in the range (0,1]"
|
|
716
|
+
);
|
|
698
717
|
}
|
|
699
718
|
const { decay, factor } = computeDecayFactor(this.param.w);
|
|
700
719
|
return roundTo((Math.pow(request_retention, 1 / decay) - 1) / factor, 8);
|
|
@@ -910,10 +929,10 @@ class FSRSAlgorithm {
|
|
|
910
929
|
stability: 0
|
|
911
930
|
};
|
|
912
931
|
if (t < 0) {
|
|
913
|
-
throw new
|
|
932
|
+
throw new FSRSValidationError(`Invalid delta_t "${t}"`);
|
|
914
933
|
}
|
|
915
934
|
if (g < 0 || g > 4) {
|
|
916
|
-
throw new
|
|
935
|
+
throw new FSRSValidationError(`Invalid grade "${g}"`);
|
|
917
936
|
}
|
|
918
937
|
if (d === 0 && s === 0) {
|
|
919
938
|
return {
|
|
@@ -928,7 +947,7 @@ class FSRSAlgorithm {
|
|
|
928
947
|
};
|
|
929
948
|
}
|
|
930
949
|
if (d < 1 || s < S_MIN) {
|
|
931
|
-
throw new
|
|
950
|
+
throw new FSRSValidationError(
|
|
932
951
|
`Invalid memory state { difficulty: ${d}, stability: ${s} }`
|
|
933
952
|
);
|
|
934
953
|
}
|
|
@@ -1309,7 +1328,9 @@ class Reschedule {
|
|
|
1309
1328
|
*/
|
|
1310
1329
|
handleManualRating(card, state, reviewed, elapsed_days, stability, difficulty, due) {
|
|
1311
1330
|
if (typeof state === "undefined") {
|
|
1312
|
-
throw new
|
|
1331
|
+
throw new FSRSValidationError(
|
|
1332
|
+
"reschedule: state is required for manual rating"
|
|
1333
|
+
);
|
|
1313
1334
|
}
|
|
1314
1335
|
let log;
|
|
1315
1336
|
let next_card;
|
|
@@ -1330,7 +1351,9 @@ class Reschedule {
|
|
|
1330
1351
|
next_card.last_review = reviewed;
|
|
1331
1352
|
} else {
|
|
1332
1353
|
if (typeof due === "undefined") {
|
|
1333
|
-
throw new
|
|
1354
|
+
throw new FSRSValidationError(
|
|
1355
|
+
"reschedule: due is required for manual rating"
|
|
1356
|
+
);
|
|
1334
1357
|
}
|
|
1335
1358
|
const scheduled_days = date_diff(due, reviewed, "days");
|
|
1336
1359
|
log = {
|
|
@@ -1420,6 +1443,9 @@ class Reschedule {
|
|
|
1420
1443
|
}
|
|
1421
1444
|
}
|
|
1422
1445
|
|
|
1446
|
+
function applyAfterHandler(value, afterHandler) {
|
|
1447
|
+
return typeof afterHandler === "function" ? afterHandler(value) : value;
|
|
1448
|
+
}
|
|
1423
1449
|
class FSRS extends FSRSAlgorithm {
|
|
1424
1450
|
strategyHandler = /* @__PURE__ */ new Map();
|
|
1425
1451
|
Scheduler;
|
|
@@ -1535,11 +1561,7 @@ class FSRS extends FSRSAlgorithm {
|
|
|
1535
1561
|
repeat(card, now, afterHandler) {
|
|
1536
1562
|
const instance = this.getScheduler(card, now);
|
|
1537
1563
|
const recordLog = instance.preview();
|
|
1538
|
-
|
|
1539
|
-
return afterHandler(recordLog);
|
|
1540
|
-
} else {
|
|
1541
|
-
return recordLog;
|
|
1542
|
-
}
|
|
1564
|
+
return applyAfterHandler(recordLog, afterHandler);
|
|
1543
1565
|
}
|
|
1544
1566
|
/**
|
|
1545
1567
|
* Display the collection of cards and logs for the card scheduled at the current time, after applying a specific grade rating.
|
|
@@ -1599,14 +1621,10 @@ class FSRS extends FSRSAlgorithm {
|
|
|
1599
1621
|
const instance = this.getScheduler(card, now);
|
|
1600
1622
|
const g = TypeConvert.rating(grade);
|
|
1601
1623
|
if (g === Rating.Manual) {
|
|
1602
|
-
throw new
|
|
1624
|
+
throw new FSRSValidationError("Cannot review a manual rating");
|
|
1603
1625
|
}
|
|
1604
1626
|
const recordLogItem = instance.review(g);
|
|
1605
|
-
|
|
1606
|
-
return afterHandler(recordLogItem);
|
|
1607
|
-
} else {
|
|
1608
|
-
return recordLogItem;
|
|
1609
|
-
}
|
|
1627
|
+
return applyAfterHandler(recordLogItem, afterHandler);
|
|
1610
1628
|
}
|
|
1611
1629
|
/**
|
|
1612
1630
|
* Get the retrievability of the card
|
|
@@ -1651,7 +1669,7 @@ class FSRS extends FSRSAlgorithm {
|
|
|
1651
1669
|
const processedCard = TypeConvert.card(card);
|
|
1652
1670
|
const processedLog = TypeConvert.review_log(log);
|
|
1653
1671
|
if (processedLog.rating === Rating.Manual) {
|
|
1654
|
-
throw new
|
|
1672
|
+
throw new FSRSValidationError("Cannot rollback a manual rating");
|
|
1655
1673
|
}
|
|
1656
1674
|
let last_due;
|
|
1657
1675
|
let last_review;
|
|
@@ -1683,11 +1701,7 @@ class FSRS extends FSRSAlgorithm {
|
|
|
1683
1701
|
state: processedLog.state,
|
|
1684
1702
|
last_review
|
|
1685
1703
|
};
|
|
1686
|
-
|
|
1687
|
-
return afterHandler(prevCard);
|
|
1688
|
-
} else {
|
|
1689
|
-
return prevCard;
|
|
1690
|
-
}
|
|
1704
|
+
return applyAfterHandler(prevCard, afterHandler);
|
|
1691
1705
|
}
|
|
1692
1706
|
/**
|
|
1693
1707
|
*
|
|
@@ -1770,11 +1784,7 @@ class FSRS extends FSRSAlgorithm {
|
|
|
1770
1784
|
last_review: processedCard.last_review
|
|
1771
1785
|
};
|
|
1772
1786
|
const recordLogItem = { card: forget_card, log: forget_log };
|
|
1773
|
-
|
|
1774
|
-
return afterHandler(recordLogItem);
|
|
1775
|
-
} else {
|
|
1776
|
-
return recordLogItem;
|
|
1777
|
-
}
|
|
1787
|
+
return applyAfterHandler(recordLogItem, afterHandler);
|
|
1778
1788
|
}
|
|
1779
1789
|
/**
|
|
1780
1790
|
* Reschedules the current card and returns the rescheduled collections and reschedule item.
|
|
@@ -1841,15 +1851,9 @@ class FSRS extends FSRSAlgorithm {
|
|
|
1841
1851
|
len ? collections[len - 1] : void 0,
|
|
1842
1852
|
updateMemoryState
|
|
1843
1853
|
);
|
|
1844
|
-
if (recordLogHandler && typeof recordLogHandler === "function") {
|
|
1845
|
-
return {
|
|
1846
|
-
collections: collections.map(recordLogHandler),
|
|
1847
|
-
reschedule_item: manual_item ? recordLogHandler(manual_item) : null
|
|
1848
|
-
};
|
|
1849
|
-
}
|
|
1850
1854
|
return {
|
|
1851
|
-
collections,
|
|
1852
|
-
reschedule_item: manual_item
|
|
1855
|
+
collections: typeof recordLogHandler === "function" ? collections.map(recordLogHandler) : collections,
|
|
1856
|
+
reschedule_item: manual_item ? applyAfterHandler(manual_item, recordLogHandler) : null
|
|
1853
1857
|
};
|
|
1854
1858
|
}
|
|
1855
1859
|
}
|