Skip to content

Commit

Permalink
Fix/wrong data types (#33)
Browse files Browse the repository at this point in the history
* Fix/wrong Data Types in Models

* 3.0.2
  • Loading branch information
ishiko732 authored Oct 16, 2023
1 parent 4ca3f54 commit 9739999
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 5 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "ts-fsrs",
"version": "3.0.1",
"version": "3.0.2",
"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.",
"main": "dist/index.js",
"types": "dist/index.d.ts",
Expand Down
2 changes: 1 addition & 1 deletion src/fsrs/default.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export const default_w = envParams.FSRS_W || [
];
export const default_enable_fuzz = envParams.FSRS_ENABLE_FUZZ || false;

export const FSRSVersion: string = "3.0.1";
export const FSRSVersion: string = "3.0.2";

export const generatorParameters = (props?: Partial<FSRSParameters>) => {
return {
Expand Down
16 changes: 14 additions & 2 deletions src/fsrs/fsrs.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import pseudorandom from "seedrandom";
import { generatorParameters, SchedulingCard } from "./index";
import { fixDate } from "./help";
import { fixDate, fixState } from "./help";
import { FSRSParameters, Card, State, Rating } from "./models";
import type { int } from "./type";

Expand All @@ -17,12 +17,21 @@ export class FSRS {
this.intervalModifier = 9 * (1 / this.param.request_retention - 1);
}

repeat = (card: Card, now: Date) => {
preProcess(card: Card, now: Date) {
card = {
...card,
state: fixState(card.state),
due: fixDate(card.due) ,
last_review: card.last_review ? fixDate(card.last_review) : undefined,
};
now = fixDate(now);
return { card, now };
}

repeat = (card: Card, now: Date) => {
const process = this.preProcess(card, now);
card = process.card;
now = process.now;
card.elapsed_days =
card.state === State.New ? 0 : now.diff(card.last_review as Date, "days"); //相距时间
card.last_review = now; // 上次复习时间
Expand Down Expand Up @@ -73,6 +82,9 @@ export class FSRS {
};

get_retrievability = (card: Card, now: Date): undefined | string => {
const process = this.preProcess(card, now);
card = process.card;
now = process.now;
if (card.state !== State.Review) {
return undefined;
}
Expand Down
24 changes: 23 additions & 1 deletion src/fsrs/help.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type {int,unit} from "./type";
import type { int, unit } from "./type";
import { Rating, State } from "./models";

declare global {
export interface Date {
Expand Down Expand Up @@ -42,6 +43,9 @@ export function date_scheduler(now: Date, t: number, isDay?: boolean): Date {
}

export function date_diff(now: Date, pre: Date, unit: unit): number {
if (!now || !pre) {
throw new Error("Invalid date");
}
const diff = now.getTime() - pre.getTime();
let r = 0;
switch (unit) {
Expand Down Expand Up @@ -114,3 +118,21 @@ export function fixDate(value: unknown) {
}
throw new Error(`Invalid date:[${value}]`);
}

export function fixState(value: unknown): State {
if (typeof value === "string") {
return State[value as keyof typeof State];
} else if (typeof value === "number") {
return value as State;
}
throw new Error(`Invalid state:[${value}]`);
}

export function fixRating(value: unknown): Rating {
if (typeof value === "string") {
return Rating[value as keyof typeof Rating];
} else if (typeof value === "number") {
return value as Rating;
}
throw new Error(`Invalid rating:[${value}]`);
}

0 comments on commit 9739999

Please sign in to comment.