Skip to content

Commit

Permalink
Fix/if s<0.5, get_retrievability return NaN (#110)
Browse files Browse the repository at this point in the history
* Fix/if s<0.5,get_retrievability return NaN

* update test

* use s.toFixed(2)

* bump version to 4.1.1
  • Loading branch information
ishiko732 authored Aug 4, 2024
1 parent 49b9566 commit 45fdde1
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 7 deletions.
4 changes: 2 additions & 2 deletions __tests__/FSRSV5.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,8 +116,8 @@ describe('get retrievability', () => {
test('return retrievability percentage for review cards', () => {
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]
const r = [undefined, undefined, undefined, '90.26%']
const r_number = [undefined, undefined, undefined, 0.90260891]
Grades.forEach((grade, index) => {
expect(fsrs.get_retrievability(sc[grade].card, sc[grade].card.due)).toBe(
r[index]
Expand Down
38 changes: 36 additions & 2 deletions __tests__/impl/long-term_schduler.test.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
import {
CardInput,
createEmptyCard,
fsrs,
FSRSAlgorithm,
generatorParameters,
Grade,
Rating,
State,
} from '../../src/fsrs'
import LongTermScheduler from '../../src/fsrs/impl/long_term_schduler'

describe('Long-term schduler', () => {
const w = [
Expand Down Expand Up @@ -282,4 +281,39 @@ describe('Long-term schduler', () => {
'Relearning',
])
})

test('[Long-term]get_retrievability ', () => {
const f = fsrs({
w: [
0.4072, 1.1829, 3.1262, 15.4722, 7.2102, 0.5316, 1.0651, 0.0234, 1.616,
0.1544, 1.0824, 1.9813, 0.0953, 0.2975, 2.2042, 0.2407, 2.9466, 0.5034,
0.6567,
],
enable_short_term: false,
})
const now = '2024-08-03T18:15:34.500Z'
const view_date = '2024-08-03T18:25:34.500Z'
let card: CardInput = createEmptyCard(now)
card = f.repeat(card, now)[Rating.Again].card
let r = f.get_retrievability(card, view_date)
expect(r).toEqual('100.00%')

card = {
cid: 81,
due: '2024-08-04T18:15:34.500Z',
stability: 0.4072,
difficulty: 7.2102,
elapsed_days: 0,
scheduled_days: 1,
reps: 1,
lapses: 0,
state: 'Review',
last_review: '2024-08-03T18:15:34.500Z',
nid: 82,
suspended: false,
deleted: false,
} as CardInput
r = f.get_retrievability(card, view_date)
expect(r).toEqual('100.00%')
})
})
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.1.0",
"version": "4.1.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
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.1.0 using FSRS V5.0'
export const FSRSVersion: string = 'v4.1.1 using FSRS V5.0'

export const generatorParameters = (
props?: Partial<FSRSParameters>
Expand Down
2 changes: 1 addition & 1 deletion src/fsrs/fsrs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ export class FSRS extends FSRSAlgorithm {
return undefined
}
const t = Math.max(now.diff(processedCard.last_review as Date, 'days'), 0)
const r = this.forgetting_curve(t, Math.round(processedCard.stability))
const r = this.forgetting_curve(t, +processedCard.stability.toFixed(2))
return (format ? `${(r * 100).toFixed(2)}%` : r) as T extends true
? string
: number
Expand Down

0 comments on commit 45fdde1

Please sign in to comment.