Skip to content

Commit

Permalink
Feat/use Reflect to set the FSRSParamters (#104)
Browse files Browse the repository at this point in the history
* Feat/use Reflect to set the FSRSParamters

* remove :FSRSAlgorithm

* bump version to 4.0.1
  • Loading branch information
ishiko732 authored Jul 27, 2024
1 parent 231539e commit d43b6b0
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 12 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": "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",
Expand Down
13 changes: 7 additions & 6 deletions src/fsrs/algorithm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,17 +72,19 @@ export class FSRSAlgorithm {
}

protected params_handler_proxy(): ProxyHandler<FSRSParameters> {
// 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
},
}
Expand All @@ -98,7 +100,6 @@ export class FSRSAlgorithm {
}
}


/**
* The formula used is :
* $$ S_0(G) = w_{G-1}$$
Expand Down
2 changes: 1 addition & 1 deletion src/fsrs/default.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<FSRSParameters>
Expand Down
11 changes: 7 additions & 4 deletions src/fsrs/fsrs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,18 +28,21 @@ export class FSRS extends FSRSAlgorithm {
}

protected override params_handler_proxy(): ProxyHandler<FSRSParameters> {
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)
)
} else if (prop === 'enable_short_term') {
_this.Schduler = value === true ? BasicScheduler : LongTermScheduler
}
// @ts-ignore
target[prop] = value
Reflect.set(target, prop, value)
return true
},
}
Expand Down

0 comments on commit d43b6b0

Please sign in to comment.