ts-fsrs 2.0.3 → 2.1.1

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/LICENSE CHANGED
@@ -1,21 +1,21 @@
1
- MIT License
2
-
3
- Copyright (c) 2023 ishiko
4
-
5
- Permission is hereby granted, free of charge, to any person obtaining a copy
6
- of this software and associated documentation files (the "Software"), to deal
7
- in the Software without restriction, including without limitation the rights
8
- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
- copies of the Software, and to permit persons to whom the Software is
10
- furnished to do so, subject to the following conditions:
11
-
12
- The above copyright notice and this permission notice shall be included in all
13
- copies or substantial portions of the Software.
14
-
15
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
- SOFTWARE.
1
+ MIT License
2
+
3
+ Copyright (c) 2023 ishiko
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md CHANGED
@@ -1,46 +1,46 @@
1
- # About The
2
-
3
- ts-fsrs is a TypeScript package used to implement the [Free Spaced Repetition Scheduler (FSRS) algorithm](https://github.com/open-spaced-repetition/free-spaced-repetition-scheduler). It helps
4
- developers apply FSRS to their flashcard applications, there by improving the user learning experience.
5
-
6
- # Usage
7
-
8
- ```
9
- npm install ts-fsrs
10
- ```
11
-
12
- # Example
13
-
14
- ```typescript
15
- import {createEmptyCard, formatDate, fsrs, generatorParameters, Rating} from 'ts-fsrs';
16
-
17
- const params = generatorParameters({ enable_fuzz: true });
18
- const f = fsrs(params);
19
- const card = createEmptyCard(new Date('2022-2-1 10:00:00'));// createEmptyCard();
20
- const now = new Date('2022-2-2 10:00:00');// new Date();
21
- const scheduling_cards = f.repeat(card, now);
22
-
23
- // console.log(scheduling_cards);
24
- Object.keys(Rating).filter(key => typeof Rating[key as any] === 'number').forEach(key => {
25
- // @ts-ignore
26
- const { log, card } = scheduling_cards[Rating[key]];
27
- console.group(`${key}`);
28
- console.table({
29
- [`card_${key}`]: {
30
- ...card,
31
- due: formatDate(card.due),
32
- last_review: formatDate(card.last_review),
33
- },
34
- });
35
- console.table({
36
- [`log_${key}`]: {
37
- ...log,
38
- review: formatDate(log.review),
39
- },
40
- });
41
- console.groupEnd();
42
- console.log('----------------------------------------------------------------');
43
- });
44
- ```
45
-
46
- > More examples refer to the [Example](https://github.com/ishiko732/ts-fsrs/blob/master/test/index.ts)
1
+ # About The
2
+
3
+ ts-fsrs is a TypeScript package used to implement the [Free Spaced Repetition Scheduler (FSRS) algorithm](https://github.com/open-spaced-repetition/free-spaced-repetition-scheduler). It helps
4
+ developers apply FSRS to their flashcard applications, there by improving the user learning experience.
5
+
6
+ # Usage
7
+
8
+ ```
9
+ npm install ts-fsrs
10
+ ```
11
+
12
+ # Example
13
+
14
+ ```typescript
15
+ import {createEmptyCard, formatDate, fsrs, generatorParameters, Rating} from 'ts-fsrs';
16
+
17
+ const params = generatorParameters({ enable_fuzz: true });
18
+ const f = fsrs(params);
19
+ const card = createEmptyCard(new Date('2022-2-1 10:00:00'));// createEmptyCard();
20
+ const now = new Date('2022-2-2 10:00:00');// new Date();
21
+ const scheduling_cards = f.repeat(card, now);
22
+
23
+ // console.log(scheduling_cards);
24
+ Object.keys(Rating).filter(key => typeof Rating[key as any] === 'number').forEach(key => {
25
+ // @ts-ignore
26
+ const { log, card } = scheduling_cards[Rating[key]];
27
+ console.group(`${key}`);
28
+ console.table({
29
+ [`card_${key}`]: {
30
+ ...card,
31
+ due: formatDate(card.due),
32
+ last_review: formatDate(card.last_review),
33
+ },
34
+ });
35
+ console.table({
36
+ [`log_${key}`]: {
37
+ ...log,
38
+ review: formatDate(log.review),
39
+ },
40
+ });
41
+ console.groupEnd();
42
+ console.log('----------------------------------------------------------------');
43
+ });
44
+ ```
45
+
46
+ > More examples refer to the [Example](https://github.com/ishiko732/ts-fsrs/blob/master/example/index.ts)
@@ -1,5 +1,5 @@
1
1
  import { Card, FSRSParameters, Rating, SchedulingCard, State } from "./index";
2
- import { int } from './help';
2
+ import { int } from "./help";
3
3
  export default class FSRS {
4
4
  private param;
5
5
  private readonly intervalModifier;
@@ -5,61 +5,70 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
6
  const seedrandom_1 = __importDefault(require("seedrandom"));
7
7
  const index_1 = require("./index");
8
+ const help_1 = require("./help");
8
9
  class FSRS {
10
+ param;
11
+ intervalModifier;
12
+ seed;
9
13
  constructor(param) {
10
- this.repeat = (card, now) => {
11
- card = Object.assign({}, card);
12
- now = new Date(now.getTime());
13
- card.elapsed_days = card.state === index_1.State.New ? 0 : now.diff(card.last_review, "days"); //相距时间
14
- card.last_review = now; // 上次复习时间
15
- card.reps += 1;
16
- const s = new index_1.SchedulingCard(card);
17
- s.update_state(card.state);
18
- this.seed = String(card.last_review.getTime()) + String(card.elapsed_days);
19
- let easy_interval, good_interval, hard_interval;
20
- switch (card.state) {
21
- case index_1.State.New:
22
- this.init_ds(s);
23
- s.again.due = now.scheduler(1);
24
- s.hard.due = now.scheduler(5);
25
- s.good.due = now.scheduler(10);
26
- easy_interval = this.next_interval(s.easy.stability * this.param.easy_bonus);
27
- s.easy.scheduled_days = easy_interval;
28
- s.easy.due = now.scheduler(easy_interval, true);
29
- break;
30
- case index_1.State.Learning:
31
- case index_1.State.Relearning:
32
- hard_interval = 0;
33
- good_interval = this.next_interval(s.good.stability);
34
- easy_interval = Math.max(this.next_interval(s.easy.stability * this.param.easy_bonus), good_interval + 1);
35
- s.schedule(now, hard_interval, good_interval, easy_interval);
36
- break;
37
- case index_1.State.Review:
38
- const interval = card.elapsed_days;
39
- const last_d = card.difficulty;
40
- const last_s = card.stability;
41
- const retrievability = this.current_retrievability(interval, last_s);
42
- this.next_ds(s, last_d, last_s, retrievability);
43
- hard_interval = this.next_interval(last_s * this.param.hard_factor);
44
- good_interval = this.next_interval(s.good.stability);
45
- hard_interval = Math.min(hard_interval, good_interval);
46
- good_interval = Math.max(good_interval, hard_interval + 1);
47
- easy_interval = Math.max(this.next_interval(s.easy.stability * this.param.easy_bonus), good_interval + 1);
48
- s.schedule(now, hard_interval, good_interval, easy_interval);
49
- break;
50
- }
51
- return s.record_log(card, now);
52
- };
53
- this.get_retrievability = (card, now) => {
54
- if (card.state !== index_1.State.Review) {
55
- return undefined;
56
- }
57
- const t = Math.max(now.diff(card.last_review, "days"), 0);
58
- return (this.current_retrievability(t, card.stability) * 100).toFixed(2) + '%';
59
- };
60
14
  this.param = param || (0, index_1.generatorParameters)();
61
- this.intervalModifier = Math.log(this.param.request_retention) / Math.log(0.9);
15
+ this.intervalModifier =
16
+ Math.log(this.param.request_retention) / Math.log(0.9);
62
17
  }
18
+ repeat = (card, now) => {
19
+ card = {
20
+ ...card,
21
+ };
22
+ now = new Date((0, help_1.fixDate)(now));
23
+ card.elapsed_days =
24
+ card.state === index_1.State.New ? 0 : now.diff(card.last_review, "days"); //相距时间
25
+ card.last_review = now; // 上次复习时间
26
+ card.reps += 1;
27
+ const s = new index_1.SchedulingCard(card);
28
+ s.update_state(card.state);
29
+ this.seed = String(card.last_review.getTime()) + String(card.elapsed_days);
30
+ let easy_interval, good_interval, hard_interval;
31
+ switch (card.state) {
32
+ case index_1.State.New:
33
+ this.init_ds(s);
34
+ s.again.due = now.scheduler(1);
35
+ s.hard.due = now.scheduler(5);
36
+ s.good.due = now.scheduler(10);
37
+ easy_interval = this.next_interval(s.easy.stability * this.param.easy_bonus);
38
+ s.easy.scheduled_days = easy_interval;
39
+ s.easy.due = now.scheduler(easy_interval, true);
40
+ break;
41
+ case index_1.State.Learning:
42
+ case index_1.State.Relearning:
43
+ hard_interval = 0;
44
+ good_interval = this.next_interval(s.good.stability);
45
+ easy_interval = Math.max(this.next_interval(s.easy.stability * this.param.easy_bonus), good_interval + 1);
46
+ s.schedule(now, hard_interval, good_interval, easy_interval);
47
+ break;
48
+ case index_1.State.Review: {
49
+ const interval = card.elapsed_days;
50
+ const last_d = card.difficulty;
51
+ const last_s = card.stability;
52
+ const retrievability = this.current_retrievability(interval, last_s);
53
+ this.next_ds(s, last_d, last_s, retrievability);
54
+ hard_interval = this.next_interval(last_s * this.param.hard_factor);
55
+ good_interval = this.next_interval(s.good.stability);
56
+ hard_interval = Math.min(hard_interval, good_interval);
57
+ good_interval = Math.max(good_interval, hard_interval + 1);
58
+ easy_interval = Math.max(this.next_interval(s.easy.stability * this.param.easy_bonus), good_interval + 1);
59
+ s.schedule(now, hard_interval, good_interval, easy_interval);
60
+ break;
61
+ }
62
+ }
63
+ return s.record_log(card, now);
64
+ };
65
+ get_retrievability = (card, now) => {
66
+ if (card.state !== index_1.State.Review) {
67
+ return undefined;
68
+ }
69
+ const t = Math.max(now.diff(card.last_review, "days"), 0);
70
+ return ((this.current_retrievability(t, card.stability) * 100).toFixed(2) + "%");
71
+ };
63
72
  init_ds(s) {
64
73
  s.again.difficulty = this.init_difficulty(index_1.Rating.Again);
65
74
  s.again.stability = this.init_stability(index_1.Rating.Again);
@@ -159,10 +168,12 @@ class FSRS {
159
168
  * @return S^\prime_r new stability after recall
160
169
  */
161
170
  next_recall_stability(d, s, r) {
162
- return s * (1 + Math.exp(this.param.w[6]) *
163
- (11 - d) *
164
- Math.pow(s, this.param.w[7]) *
165
- (Math.exp((1 - r) * this.param.w[8]) - 1));
171
+ return (s *
172
+ (1 +
173
+ Math.exp(this.param.w[6]) *
174
+ (11 - d) *
175
+ Math.pow(s, this.param.w[7]) *
176
+ (Math.exp((1 - r) * this.param.w[8]) - 1)));
166
177
  }
167
178
  /**
168
179
  * The formula used is :
@@ -173,7 +184,10 @@ class FSRS {
173
184
  * @return S^\prime_f new stability after forgetting
174
185
  */
175
186
  next_forget_stability(d, s, r) {
176
- return this.param.w[9] * Math.pow(d, this.param.w[10]) * Math.pow(s, this.param.w[11]) * Math.exp((1 - r) * this.param.w[12]);
187
+ return (this.param.w[9] *
188
+ Math.pow(d, this.param.w[10]) *
189
+ Math.pow(s, this.param.w[11]) *
190
+ Math.exp((1 - r) * this.param.w[12]));
177
191
  }
178
192
  /**
179
193
  * The formula used is :
@@ -183,7 +197,7 @@ class FSRS {
183
197
  * @return r Retrievability (probability of recall)
184
198
  */
185
199
  current_retrievability(t, s) {
186
- return Math.exp(Math.log(0.9) * t / s);
200
+ return Math.exp((Math.log(0.9) * t) / s);
187
201
  }
188
202
  }
189
203
  exports.default = FSRS;
@@ -0,0 +1,2 @@
1
+ declare const FSRSVersion: string;
2
+ export default FSRSVersion;
@@ -0,0 +1,7 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ // eslint-disable-next-line @typescript-eslint/ban-ts-comment
4
+ // @ts-ignore
5
+ const package_json_1 = require("../../package.json");
6
+ const FSRSVersion = package_json_1.version;
7
+ exports.default = FSRSVersion;
@@ -1,4 +1,4 @@
1
- export type unit = 'days' | 'minutes';
1
+ export type unit = "days" | "minutes";
2
2
  export type int = number & {
3
3
  __int__: void;
4
4
  };
@@ -16,4 +16,5 @@ declare global {
16
16
  export declare function date_scheduler(now: Date, t: number, isDay?: boolean): Date;
17
17
  export declare function date_diff(now: Date, pre: Date, unit: unit): number;
18
18
  export declare function formatDate(date: Date): string;
19
- export declare function show_diff_message(due: Date, last_review: Date, unit?: boolean): string;
19
+ export declare function show_diff_message(due: Date, last_review: Date, unit?: boolean, timeUnit?: string[]): string;
20
+ export declare function fixDate(value: unknown): Date;
package/dist/help.js ADDED
@@ -0,0 +1,94 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.fixDate = exports.show_diff_message = exports.formatDate = exports.date_diff = exports.date_scheduler = void 0;
4
+ Date.prototype.scheduler = function (t, isDay) {
5
+ return date_scheduler(this, t, isDay);
6
+ };
7
+ /**
8
+ * 当前时间与之前的时间差值
9
+ * @param pre 比当前时间还要之前
10
+ * @param unit 单位: days | minutes
11
+ */
12
+ Date.prototype.diff = function (pre, unit) {
13
+ return date_diff(this, pre, unit);
14
+ };
15
+ Date.prototype.format = function () {
16
+ return formatDate(this);
17
+ };
18
+ Date.prototype.dueFormat = function (last_review, unit) {
19
+ return show_diff_message(this, last_review, unit);
20
+ };
21
+ function date_scheduler(now, t, isDay) {
22
+ return new Date(isDay
23
+ ? now.getTime() + t * 24 * 60 * 60 * 1000
24
+ : now.getTime() + t * 60 * 1000);
25
+ }
26
+ exports.date_scheduler = date_scheduler;
27
+ function date_diff(now, pre, unit) {
28
+ const diff = now.getTime() - pre.getTime();
29
+ let r = 0;
30
+ switch (unit) {
31
+ case "days":
32
+ r = Math.floor(diff / (24 * 60 * 60 * 1000));
33
+ break;
34
+ case "minutes":
35
+ r = Math.floor(diff / (60 * 1000));
36
+ break;
37
+ }
38
+ return r;
39
+ }
40
+ exports.date_diff = date_diff;
41
+ function formatDate(date) {
42
+ const year = date.getFullYear();
43
+ const month = date.getMonth() + 1;
44
+ const day = date.getDate();
45
+ const hours = date.getHours();
46
+ const minutes = date.getMinutes();
47
+ const seconds = date.getSeconds();
48
+ return `${year}-${padZero(month)}-${padZero(day)} ${padZero(hours)}:${padZero(minutes)}:${padZero(seconds)}`;
49
+ }
50
+ exports.formatDate = formatDate;
51
+ function padZero(num) {
52
+ return num < 10 ? `0${num}` : `${num}`;
53
+ }
54
+ const TIMEUNIT = [60, 60, 24, 31, 12];
55
+ const TIMEUNITFORMAT = ["second", "min", "hour", "day", "month", "year"];
56
+ function show_diff_message(due, last_review, unit, timeUnit = TIMEUNITFORMAT) {
57
+ due = fixDate(due);
58
+ last_review = fixDate(last_review);
59
+ if (timeUnit.length !== TIMEUNITFORMAT.length) {
60
+ timeUnit = TIMEUNITFORMAT;
61
+ }
62
+ let diff = due.getTime() - last_review.getTime();
63
+ let i;
64
+ diff /= 1000;
65
+ for (i = 0; i < TIMEUNIT.length; i++) {
66
+ if (diff < TIMEUNIT[i]) {
67
+ break;
68
+ }
69
+ else {
70
+ diff /= TIMEUNIT[i];
71
+ }
72
+ }
73
+ return `${Math.floor(diff)}${unit ? timeUnit[i] : ""}`;
74
+ }
75
+ exports.show_diff_message = show_diff_message;
76
+ function fixDate(value) {
77
+ if (typeof value === "object" && value instanceof Date) {
78
+ return value;
79
+ }
80
+ else if (typeof value === "string") {
81
+ const timestamp = Date.parse(value);
82
+ if (!isNaN(timestamp)) {
83
+ return new Date(timestamp);
84
+ }
85
+ else {
86
+ throw new Error(`Invalid date:[${value}]`);
87
+ }
88
+ }
89
+ else if (typeof value === "number") {
90
+ return new Date(value);
91
+ }
92
+ throw new Error(`Invalid date:[${value}]`);
93
+ }
94
+ exports.fixDate = fixDate;
@@ -1,6 +1,7 @@
1
1
  import FSRS from "./fsrs";
2
2
  import { Card, default_easy_bonus, default_enable_fuzz, default_hard_factor, default_maximum_interval, default_request_retention, default_w, FSRSParameters, Rating, RatingType, ReviewLog, SchedulingLog, State, StateType } from "./models";
3
3
  import { SchedulingCard } from "./scheduler";
4
+ import FSRSVersion from "./fsrs_version";
4
5
  declare const fsrs: (param?: FSRSParameters) => FSRS;
5
6
  declare const createEmptyCard: (now?: Date) => Card;
6
7
  declare const generatorParameters: (props?: {
@@ -18,8 +19,8 @@ declare const generatorParameters: (props?: {
18
19
  w: number[];
19
20
  enable_fuzz: boolean;
20
21
  };
21
- declare const FSRS_Version = "2.0.3";
22
- export { fsrs, FSRS_Version, State, Rating, SchedulingCard, createEmptyCard, generatorParameters };
23
- export type { StateType, RatingType, ReviewLog, Card, SchedulingLog, FSRSParameters };
24
- export { default_request_retention, default_maximum_interval, default_easy_bonus, default_hard_factor, default_w, default_enable_fuzz };
25
- export { date_scheduler, date_diff, formatDate, show_diff_message } from './help';
22
+ export { fsrs, FSRSVersion, State, Rating, SchedulingCard, createEmptyCard, generatorParameters, };
23
+ export type { StateType, RatingType, ReviewLog, Card, SchedulingLog, FSRSParameters, };
24
+ export { default_request_retention, default_maximum_interval, default_easy_bonus, default_hard_factor, default_w, default_enable_fuzz, };
25
+ export { date_scheduler, date_diff, formatDate, show_diff_message, } from "./help";
26
+ export type { int, double } from "./help";
@@ -3,7 +3,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
3
3
  return (mod && mod.__esModule) ? mod : { "default": mod };
4
4
  };
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.show_diff_message = exports.formatDate = exports.date_diff = exports.date_scheduler = exports.default_enable_fuzz = exports.default_w = exports.default_hard_factor = exports.default_easy_bonus = exports.default_maximum_interval = exports.default_request_retention = exports.generatorParameters = exports.createEmptyCard = exports.SchedulingCard = exports.Rating = exports.State = exports.FSRS_Version = exports.fsrs = void 0;
6
+ exports.show_diff_message = exports.formatDate = exports.date_diff = exports.date_scheduler = exports.default_enable_fuzz = exports.default_w = exports.default_hard_factor = exports.default_easy_bonus = exports.default_maximum_interval = exports.default_request_retention = exports.generatorParameters = exports.createEmptyCard = exports.SchedulingCard = exports.Rating = exports.State = exports.FSRSVersion = exports.fsrs = void 0;
7
7
  const fsrs_1 = __importDefault(require("./fsrs"));
8
8
  const models_1 = require("./models");
9
9
  Object.defineProperty(exports, "default_easy_bonus", { enumerable: true, get: function () { return models_1.default_easy_bonus; } });
@@ -16,6 +16,8 @@ Object.defineProperty(exports, "Rating", { enumerable: true, get: function () {
16
16
  Object.defineProperty(exports, "State", { enumerable: true, get: function () { return models_1.State; } });
17
17
  const scheduler_1 = require("./scheduler");
18
18
  Object.defineProperty(exports, "SchedulingCard", { enumerable: true, get: function () { return scheduler_1.SchedulingCard; } });
19
+ const fsrs_version_1 = __importDefault(require("./fsrs_version"));
20
+ exports.FSRSVersion = fsrs_version_1.default;
19
21
  const fsrs = (param) => {
20
22
  return new fsrs_1.default(param);
21
23
  };
@@ -44,7 +46,7 @@ const generatorParameters = (props) => {
44
46
  enable_fuzz: models_1.default_enable_fuzz,
45
47
  };
46
48
  }
47
- const { w, request_retention, hard_factor, maximum_interval, enable_fuzz, easy_bonus } = props;
49
+ const { w, request_retention, hard_factor, maximum_interval, enable_fuzz, easy_bonus, } = props;
48
50
  return {
49
51
  request_retention: request_retention || models_1.default_request_retention,
50
52
  maximum_interval: maximum_interval || models_1.default_maximum_interval,
@@ -55,8 +57,6 @@ const generatorParameters = (props) => {
55
57
  };
56
58
  };
57
59
  exports.generatorParameters = generatorParameters;
58
- const FSRS_Version = "2.0.3";
59
- exports.FSRS_Version = FSRS_Version;
60
60
  var help_1 = require("./help");
61
61
  Object.defineProperty(exports, "date_scheduler", { enumerable: true, get: function () { return help_1.date_scheduler; } });
62
62
  Object.defineProperty(exports, "date_diff", { enumerable: true, get: function () { return help_1.date_diff; } });
@@ -19,5 +19,7 @@ exports.default_request_retention = 0.9;
19
19
  exports.default_maximum_interval = 36500;
20
20
  exports.default_easy_bonus = 1.3;
21
21
  exports.default_hard_factor = 1.2;
22
- exports.default_w = [1, 1, 5, -0.5, -0.5, 0.2, 1.4, -0.12, 0.8, 2, -0.2, 0.2, 1];
22
+ exports.default_w = [
23
+ 1, 1, 5, -0.5, -0.5, 0.2, 1.4, -0.12, 0.8, 2, -0.2, 0.2, 1,
24
+ ];
23
25
  exports.default_enable_fuzz = false;
@@ -1,4 +1,4 @@
1
- import { Card, Rating, State } from './models';
1
+ import { Card, Rating, State } from "./models";
2
2
  export declare class SchedulingCard {
3
3
  again: Card;
4
4
  hard: Card;
@@ -4,8 +4,14 @@ exports.SchedulingCard = void 0;
4
4
  const models_1 = require("./models");
5
5
  const help_1 = require("./help");
6
6
  class SchedulingCard {
7
+ again;
8
+ hard;
9
+ good;
10
+ easy;
7
11
  copy(card) {
8
- return Object.assign({}, card);
12
+ return {
13
+ ...card,
14
+ };
9
15
  }
10
16
  constructor(card) {
11
17
  this.again = this.copy(card);
@@ -42,7 +48,10 @@ class SchedulingCard {
42
48
  this.good.scheduled_days = good_interval;
43
49
  this.easy.scheduled_days = easy_interval;
44
50
  this.again.due = (0, help_1.date_scheduler)(now, 5);
45
- this.hard.due = hard_interval > 0 ? (0, help_1.date_scheduler)(now, hard_interval, true) : (0, help_1.date_scheduler)(now, 10);
51
+ this.hard.due =
52
+ hard_interval > 0
53
+ ? (0, help_1.date_scheduler)(now, hard_interval, true)
54
+ : (0, help_1.date_scheduler)(now, 10);
46
55
  this.good.due = (0, help_1.date_scheduler)(now, good_interval, true);
47
56
  this.easy.due = (0, help_1.date_scheduler)(now, easy_interval, true);
48
57
  return this;
@@ -56,8 +65,8 @@ class SchedulingCard {
56
65
  state: card.state,
57
66
  elapsed_days: this.again.scheduled_days,
58
67
  scheduled_days: card.elapsed_days,
59
- review: now
60
- }
68
+ review: now,
69
+ },
61
70
  },
62
71
  [models_1.Rating.Hard]: {
63
72
  card: this.hard,
@@ -66,8 +75,8 @@ class SchedulingCard {
66
75
  state: card.state,
67
76
  elapsed_days: this.hard.scheduled_days,
68
77
  scheduled_days: card.elapsed_days,
69
- review: now
70
- }
78
+ review: now,
79
+ },
71
80
  },
72
81
  [models_1.Rating.Good]: {
73
82
  card: this.good,
@@ -76,8 +85,8 @@ class SchedulingCard {
76
85
  state: card.state,
77
86
  elapsed_days: this.good.scheduled_days,
78
87
  scheduled_days: card.elapsed_days,
79
- review: now
80
- }
88
+ review: now,
89
+ },
81
90
  },
82
91
  [models_1.Rating.Easy]: {
83
92
  card: this.easy,
@@ -86,9 +95,9 @@ class SchedulingCard {
86
95
  state: card.state,
87
96
  elapsed_days: this.easy.scheduled_days,
88
97
  scheduled_days: card.elapsed_days,
89
- review: now
90
- }
91
- }
98
+ review: now,
99
+ },
100
+ },
92
101
  };
93
102
  }
94
103
  }
package/package.json CHANGED
@@ -1,43 +1,54 @@
1
- {
2
- "name": "ts-fsrs",
3
- "version": "2.0.3",
4
- "description": "ts-fsrs is a TypeScript package used to implement the Free Spaced Repetition Scheduler (FSRS) algorithm. It helps developers apply FSRS to their flashcard applications, thereby improving the user learning experience.",
5
- "main": "lib/index.js",
6
- "types": "lib/index.d.ts",
7
- "keywords": [
8
- "SuperMemo",
9
- "Anki",
10
- "FSRS"
11
- ],
12
- "dependencies": {
13
- "seedrandom": "^3.0.5"
14
- },
15
- "devDependencies": {
16
- "@types/seedrandom": "^3.0.5",
17
- "eslint": "^8.44.0",
18
- "typescript": "^5.1.6"
19
- },
20
- "scripts": {
21
- "test": "ts-node test/index.ts",
22
- "build": "tsc",
23
- "test_publish": "yalc publish",
24
- "major": "npm version major",
25
- "minor": "npm version minor",
26
- "patch": "npm version patch"
27
- },
28
- "author": "ishiko",
29
- "license": "MIT",
30
- "files": [
31
- "lib/**/*",
32
- "README.md",
33
- "LICENSE"
34
- ],
35
- "repository": {
36
- "type": "git",
37
- "url": "git+https://github.com/ishiko732/ts-fsrs.git"
38
- },
39
- "bugs": {
40
- "url": "https://github.com/ishiko732/ts-fsrs/issues"
41
- },
42
- "homepage": "https://github.com/ishiko732/ts-fsrs#readme"
43
- }
1
+ {
2
+ "name": "ts-fsrs",
3
+ "version": "2.1.1",
4
+ "description": "ts-fsrs is a TypeScript package used to implement the Free Spaced Repetition Scheduler (FSRS) algorithm. It helps developers apply FSRS to their flashcard applications, thereby improving the user learning experience.",
5
+ "main": "dist/index.js",
6
+ "types": "dist/index.d.ts",
7
+ "keywords": [
8
+ "SuperMemo",
9
+ "Anki",
10
+ "FSRS"
11
+ ],
12
+ "dependencies": {
13
+ "seedrandom": "^3.0.5"
14
+ },
15
+ "devDependencies": {
16
+ "@types/node": "^20.6.2",
17
+ "@types/jest": "^29.5.5",
18
+ "@types/seedrandom": "^3.0.5",
19
+ "@typescript-eslint/eslint-plugin": "^6.7.0",
20
+ "@typescript-eslint/parser": "^6.7.0",
21
+ "eslint": "^8.44.0",
22
+ "eslint-config-prettier": "^9.0.0",
23
+ "jest": "^29.7.0",
24
+ "prettier": "^3.0.3",
25
+ "ts-jest": "^29.1.1",
26
+ "ts-node": "^10.9.1",
27
+ "typescript": "^5.1.6"
28
+ },
29
+ "scripts": {
30
+ "lint": "eslint --fix src/ && prettier --write src/",
31
+ "dev": "ts-node src/fsrs/index.ts",
32
+ "test": "jest --passWithNoTests",
33
+ "build": "tsc --project ./tsconfig.json",
34
+ "major": "npm version major",
35
+ "minor": "npm version minor",
36
+ "patch": "npm version patch",
37
+ "test_publish": "yalc publish"
38
+ },
39
+ "author": "ishiko",
40
+ "license": "MIT",
41
+ "files": [
42
+ "dist/**/*",
43
+ "README.md",
44
+ "LICENSE"
45
+ ],
46
+ "repository": {
47
+ "type": "git",
48
+ "url": "git+https://github.com/ishiko732/ts-fsrs.git"
49
+ },
50
+ "bugs": {
51
+ "url": "https://github.com/ishiko732/ts-fsrs/issues"
52
+ },
53
+ "homepage": "https://github.com/ishiko732/ts-fsrs#readme"
54
+ }
package/lib/help.js DELETED
@@ -1,70 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.show_diff_message = exports.formatDate = exports.date_diff = exports.date_scheduler = void 0;
4
- Date.prototype.scheduler = function (t, isDay) {
5
- return date_scheduler(this, t, isDay);
6
- };
7
- /**
8
- * 当前时间与之前的时间差值
9
- * @param pre 比当前时间还要之前
10
- * @param unit 单位: days | minutes
11
- */
12
- Date.prototype.diff = function (pre, unit) {
13
- return date_diff(this, pre, unit);
14
- };
15
- Date.prototype.format = function () {
16
- return formatDate(this);
17
- };
18
- Date.prototype.dueFormat = function (last_review, unit) {
19
- return show_diff_message(this, last_review, unit);
20
- };
21
- function date_scheduler(now, t, isDay) {
22
- return new Date(isDay ? now.getTime() + t * 24 * 60 * 60 * 1000 : now.getTime() + t * 60 * 1000);
23
- }
24
- exports.date_scheduler = date_scheduler;
25
- function date_diff(now, pre, unit) {
26
- const diff = now.getTime() - pre.getTime();
27
- let r = 0;
28
- switch (unit) {
29
- case 'days':
30
- r = Math.floor((diff) / (24 * 60 * 60 * 1000));
31
- break;
32
- case 'minutes':
33
- r = Math.floor((diff) / (60 * 1000));
34
- break;
35
- }
36
- return r;
37
- }
38
- exports.date_diff = date_diff;
39
- function formatDate(date) {
40
- const year = date.getFullYear();
41
- const month = date.getMonth() + 1;
42
- const day = date.getDate();
43
- const hours = date.getHours();
44
- const minutes = date.getMinutes();
45
- const seconds = date.getSeconds();
46
- return `${year}-${padZero(month)}-${padZero(day)} ${padZero(hours)}:${padZero(minutes)}:${padZero(seconds)}`;
47
- }
48
- exports.formatDate = formatDate;
49
- function padZero(num) {
50
- return num < 10 ? `0${num}` : `${num}`;
51
- }
52
- function show_diff_message(due, last_review, unit) {
53
- const diff = due.getTime() - last_review.getTime();
54
- if (diff < 1000 * 60 * 60) { // 小于1小时
55
- return unit ? Math.floor((diff) / (60 * 1000)) + 'min' : String(Math.floor((diff) / (60 * 1000)));
56
- }
57
- else if (diff >= 1000 * 60 * 60 && diff < 1000 * 60 * 60 * 24) { // 大于1小时,小于1天
58
- return unit ? Math.floor((diff) / (60 * 60 * 1000)) + 'hour' : String(Math.floor((diff) / (60 * 60 * 1000)));
59
- }
60
- else if (diff >= 1000 * 60 * 60 * 24 && diff < 1000 * 60 * 60 * 24 * 31) { // 大于1天,小于31天
61
- return unit ? Math.floor((diff) / (24 * 60 * 60 * 1000)) + 'day' : String(Math.floor((diff) / (24 * 60 * 60 * 1000 * 1000)));
62
- }
63
- else if (diff >= 1000 * 60 * 60 * 24 * 31 && diff < 1000 * 60 * 60 * 24 * 365) { // 大于31天,小于365天
64
- return unit ? ((diff) / (30 * 24 * 60 * 60 * 1000)).toFixed(2) + 'month' : ((diff) / (30 * 24 * 60 * 60 * 1000)).toFixed(2);
65
- }
66
- else { // 大于365天
67
- return unit ? ((diff) / (365 * 24 * 60 * 60 * 1000)).toFixed(2) + 'year' : ((diff) / (365 * 24 * 60 * 60 * 1000)).toFixed(2);
68
- }
69
- }
70
- exports.show_diff_message = show_diff_message;
File without changes