Skip to content

Commit

Permalink
pref/adjusting get_retrievability (#83)
Browse files Browse the repository at this point in the history
* pref/adjusting the return type in retrievability

* Number(r) -> r

* stability -> Math.round(stability)

Keep the synchronous code consistent:
https://github.com/open-spaced-repetition/fsrs4anki/blob/c6ce3f197fb403ccb7cb5290d5a59fd818ad11cb/fsrs4anki_scheduler.js#L138-L144

* _now -> now

* 3.5.7
  • Loading branch information
ishiko732 committed Apr 18, 2024
1 parent e046b55 commit 0fdaa11
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 15 deletions.
16 changes: 11 additions & 5 deletions __tests__/FSRSV4.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,11 +114,17 @@ describe("get retrievability", () => {
});

test("return retrievability percentage for review cards", () => {
const card = createEmptyCard();
const sc = fsrs.repeat(card, new Date());
const r = [undefined, undefined, undefined, "100.00%"];
Grades.forEach((grade,index) => {
expect(fsrs.get_retrievability(sc[grade].card, new Date())).toBe(r[index]);
const card = createEmptyCard("2023-12-01 04:00:00");
const sc = fsrs.repeat(card, "2023-12-01 04:05:00");
const r = [undefined, undefined, undefined, "90.00%"];
const r_number = [undefined, undefined, undefined, 0.9];
Grades.forEach((grade, index) => {
expect(fsrs.get_retrievability(sc[grade].card, sc[grade].card.due)).toBe(
r[index],
);
expect(
fsrs.get_retrievability(sc[grade].card, sc[grade].card.due, false),
).toBe(r_number[index]);
});
});
});
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.5.6",
"version": "3.5.7",
"description": "ts-fsrs is a ES modules package based on TypeScript, used to implement the Free Spaced Repetition Scheduler (FSRS) algorithm. It helps developers apply FSRS to their flashcard applications, there by improving the user learning experience.",
"main": "dist/index.cjs",
"umd": "dist/index.umd.js",
Expand Down
2 changes: 1 addition & 1 deletion src/fsrs/default.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export const default_w = [
];
export const default_enable_fuzz = false;

export const FSRSVersion: string = "3.5.6";
export const FSRSVersion: string = "3.5.7";

export const generatorParameters = (
props?: Partial<FSRSParameters>,
Expand Down
25 changes: 17 additions & 8 deletions src/fsrs/fsrs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -159,20 +159,29 @@ export class FSRS extends FSRSAlgorithm {
}
}

get_retrievability = (
/**
* Get the retrievability of the card
* @param card Card to be processed
* @param now Current time or scheduled time
* @param format default:true , Convert the result to another type. (Optional)
* @returns The retrievability of the card,if format is true, the result is a string, otherwise it is a number
*/
get_retrievability<T extends boolean>(
card: CardInput | Card,
now: Date,
): undefined | string => {
now: DateInput,
format: T = true as T,
): undefined | (T extends true ? string : number) {
const processedCard = this.preProcessCard(card);
now = this.preProcessDate(now);
if (processedCard.state !== State.Review) {
return undefined;
}
const t = Math.max(now.diff(processedCard.last_review as Date, "days"), 0);
return (
(this.forgetting_curve(t, processedCard.stability) * 100).toFixed(2) + "%"
);
};
const r = this.forgetting_curve(t, Math.round(processedCard.stability));
return (format ? `${(r * 100).toFixed(2)}%` : r) as T extends true
? string
: number;
}

/**
*
Expand Down Expand Up @@ -420,4 +429,4 @@ export class FSRS extends FSRSAlgorithm {
*/
export const fsrs = (params?: Partial<FSRSParameters>) => {
return new FSRS(params || {});
};
};

0 comments on commit 0fdaa11

Please sign in to comment.