From 55c7ab529d6b6bc2aef7bcce13e7d1ee1d174d2c Mon Sep 17 00:00:00 2001 From: ishiko Date: Sat, 4 Nov 2023 20:53:27 +0800 Subject: [PATCH] Feat/grades (#43) * Feat/Grades * 3.0.6 --- README.md | 41 +++++++++++++++++++--------------------- __tests__/FSRSV4.test.ts | 8 +++----- __tests__/help.tsts.ts | 5 +++++ package.json | 2 +- src/fsrs/default.ts | 2 +- src/fsrs/help.ts | 2 ++ src/fsrs/index.ts | 1 + 7 files changed, 32 insertions(+), 29 deletions(-) create mode 100644 __tests__/help.tsts.ts diff --git a/README.md b/README.md index 3bc2012..5f7cfc9 100644 --- a/README.md +++ b/README.md @@ -21,7 +21,7 @@ cp .env.local.example .env.local # Example ```typescript -import {createEmptyCard, formatDate, fsrs, generatorParameters, Rating} from 'ts-fsrs'; +import {createEmptyCard, formatDate, fsrs, generatorParameters, Rating, Grades} from 'ts-fsrs'; const params = generatorParameters({ enable_fuzz: true }); const f = fsrs(params); @@ -30,28 +30,25 @@ const now = new Date('2022-2-2 10:00:00');// new Date(); const scheduling_cards = f.repeat(card, now); // console.log(scheduling_cards); -Object.keys(Rating) - .filter(key => !isNaN(Number(key))) - .map(key => Number(key) as Rating) // [Rating.Again, Rating.Hard, Rating.Good, Rating.Easy] - .forEach(grade => { - const { log, card } = scheduling_cards[grade]; - console.group(`${Rating[grade]}`); - console.table({ - [`card_${Rating[grade]}`]: { - ...card, - due: formatDate(card.due), - last_review: formatDate(card.last_review as Date), - }, - }); - console.table({ - [`log_${Rating[grade]}`]: { - ...log, - review: formatDate(log.review), - }, - }); - console.groupEnd(); - console.log('----------------------------------------------------------------'); +Grades.forEach(grade => { // [Rating.Again, Rating.Hard, Rating.Good, Rating.Easy] + const { log, card } = scheduling_cards[grade]; + console.group(`${Rating[grade]}`); + console.table({ + [`card_${Rating[grade]}`]: { + ...card, + due: formatDate(card.due), + last_review: formatDate(card.last_review as Date), + }, }); + console.table({ + [`log_${Rating[grade]}`]: { + ...log, + review: formatDate(log.review), + }, + }); + console.groupEnd(); + console.log('----------------------------------------------------------------'); +}); ``` > More examples refer to the [Example](https://github.com/ishiko732/ts-fsrs/blob/master/example/index.ts) diff --git a/__tests__/FSRSV4.test.ts b/__tests__/FSRSV4.test.ts index e38b86b..eebce69 100644 --- a/__tests__/FSRSV4.test.ts +++ b/__tests__/FSRSV4.test.ts @@ -4,16 +4,14 @@ import { generatorParameters, FSRS, createEmptyCard, + Grades, } from "../src/fsrs"; describe("initial FSRS V4", () => { const params = generatorParameters(); const f: FSRS = fsrs(params); - const Ratings = Object.keys(Rating) - .filter((key) => !isNaN(Number(key))) - .map((key) => Number(key) as Rating); it("initial stability ", () => { - Ratings.forEach((grade) => { + Grades.forEach((grade) => { const s = f.init_stability(grade); expect(s).toEqual(params.w[grade - 1]); }); @@ -26,7 +24,7 @@ describe("initial FSRS V4", () => { }); it("initial difficulty ", () => { - Ratings.forEach((grade) => { + Grades.forEach((grade) => { const s = f.init_difficulty(grade); expect(s).toEqual(params.w[4] - (grade - 3) * params.w[5]); }); diff --git a/__tests__/help.tsts.ts b/__tests__/help.tsts.ts new file mode 100644 index 0000000..0875100 --- /dev/null +++ b/__tests__/help.tsts.ts @@ -0,0 +1,5 @@ +import { Grades, Rating } from "../src/fsrs"; + +test("FSRS-Grades", () => { + expect(Grades).toStrictEqual([Rating.Again, Rating.Hard, Rating.Good, Rating.Easy]); +}); diff --git a/package.json b/package.json index 5a95f63..b807677 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "ts-fsrs", - "version": "3.0.5", + "version": "3.0.6", "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", diff --git a/src/fsrs/default.ts b/src/fsrs/default.ts index 1cb1d35..8eddf0c 100644 --- a/src/fsrs/default.ts +++ b/src/fsrs/default.ts @@ -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.5"; +export const FSRSVersion: string = "3.0.6"; export const generatorParameters = (props?: Partial): FSRSParameters => { return { diff --git a/src/fsrs/help.ts b/src/fsrs/help.ts index 7459625..09e3782 100644 --- a/src/fsrs/help.ts +++ b/src/fsrs/help.ts @@ -143,3 +143,5 @@ export function fixRating(value: unknown): Rating { } throw new Error(`Invalid rating:[${value}]`); } + +export const Grades = [Rating.Again, Rating.Hard, Rating.Good, Rating.Easy]; \ No newline at end of file diff --git a/src/fsrs/index.ts b/src/fsrs/index.ts index b329752..1d3688c 100644 --- a/src/fsrs/index.ts +++ b/src/fsrs/index.ts @@ -15,6 +15,7 @@ export { date_diff, formatDate, show_diff_message, + Grades } from "./help"; export type { int, double } from "./type";