From d43b6b006e24875550ca3ce842958598fb2c8679 Mon Sep 17 00:00:00 2001 From: ishiko Date: Sat, 27 Jul 2024 23:33:32 +0800 Subject: [PATCH] Feat/use `Reflect` to set the `FSRSParamters` (#104) * Feat/use Reflect to set the FSRSParamters * remove :FSRSAlgorithm * bump version to 4.0.1 --- package.json | 2 +- src/fsrs/algorithm.ts | 13 +++++++------ src/fsrs/default.ts | 2 +- src/fsrs/fsrs.ts | 11 +++++++---- 4 files changed, 16 insertions(+), 12 deletions(-) diff --git a/package.json b/package.json index d7d9f08..c1867a1 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "ts-fsrs", - "version": "4.0.0", + "version": "4.0.1", "description": "ts-fsrs is a versatile package based on TypeScript that supports ES modules, CommonJS, and UMD. It implements the Free Spaced Repetition Scheduler (FSRS) algorithm, enabling developers to integrate FSRS into their flashcard applications to enhance the user learning experience.", "main": "dist/index.cjs", "umd": "dist/index.umd.js", diff --git a/src/fsrs/algorithm.ts b/src/fsrs/algorithm.ts index 3cefc3f..0ad0ee6 100644 --- a/src/fsrs/algorithm.ts +++ b/src/fsrs/algorithm.ts @@ -72,17 +72,19 @@ export class FSRSAlgorithm { } protected params_handler_proxy(): ProxyHandler { - // eslint-disable-next-line @typescript-eslint/no-this-alias - const _this: FSRSAlgorithm = this + const _this = this satisfies FSRSAlgorithm return { - set: function (target, prop, value) { + set: function ( + target: FSRSParameters, + prop: keyof FSRSParameters, + value: FSRSParameters[keyof FSRSParameters] + ) { if (prop === 'request_retention' && Number.isFinite(value)) { _this.intervalModifier = _this.calculate_interval_modifier( Number(value) ) } - // @ts-ignore - target[prop] = value + Reflect.set(target, prop, value) return true }, } @@ -98,7 +100,6 @@ export class FSRSAlgorithm { } } - /** * The formula used is : * $$ S_0(G) = w_{G-1}$$ diff --git a/src/fsrs/default.ts b/src/fsrs/default.ts index 797585f..70d06ec 100644 --- a/src/fsrs/default.ts +++ b/src/fsrs/default.ts @@ -11,7 +11,7 @@ export const default_w = [ export const default_enable_fuzz = false export const defualt_enable_short_term = true -export const FSRSVersion: string = 'v4.0.0 using FSRS V5.0' +export const FSRSVersion: string = 'v4.0.1 using FSRS V5.0' export const generatorParameters = ( props?: Partial diff --git a/src/fsrs/fsrs.ts b/src/fsrs/fsrs.ts index efbc5f6..e1b8864 100644 --- a/src/fsrs/fsrs.ts +++ b/src/fsrs/fsrs.ts @@ -28,9 +28,13 @@ export class FSRS extends FSRSAlgorithm { } protected override params_handler_proxy(): ProxyHandler { - const _this: FSRS = this satisfies FSRS + const _this = this satisfies FSRS return { - set: function (target, prop, value) { + set: function ( + target: FSRSParameters, + prop: keyof FSRSParameters, + value: FSRSParameters[keyof FSRSParameters] + ) { if (prop === 'request_retention' && Number.isFinite(value)) { _this.intervalModifier = _this.calculate_interval_modifier( Number(value) @@ -38,8 +42,7 @@ export class FSRS extends FSRSAlgorithm { } else if (prop === 'enable_short_term') { _this.Schduler = value === true ? BasicScheduler : LongTermScheduler } - // @ts-ignore - target[prop] = value + Reflect.set(target, prop, value) return true }, }