@dtjv/sm-2

An implementation of sm-2 algorithm


Keywords
supermemo, sm2, space repetition
License
MIT
Install
npm install @dtjv/sm-2@2.1.0

Documentation

SM-2

An implementation of the SM-2 algorithm written in TypeScript.

Install

$ npm install @dtjv/sm-2

Usage

CommonJS

// example.js
const { sm2, SuperMemoQuality, SuperMemoItemDefaults } = require('@dtjv/sm-2')

ESM

// example.mjs
import SM2 from '@dtjv/sm-2'
const { sm2, SuperMemoQuality, SuperMemoItemDefaults } = SM2

TypeScript

// example.ts
import { sm2, SuperMemoQuality, SuperMemoItemDefaults } from '@dtjv/sm-2'
import type { SuperMemoItem } from '@dtjv/sm-2'

Example

For more examples, see test file.

// example.ts

import { sm2, SuperMemoQuality, SuperMemoItemDefaults } from '@dtjv/sm-2'
import type { SuperMemoItem } from '@dtjv/sm-2'

interface Card extends SuperMemoItem {
  term: string
  definition: string
}

const card: Card = {
  term: '🍩',
  definition: '😋😋😋',
  ...SuperMemoItemDefaults, // adds read-only default values
}

const newCard = sm2<Card>(card, SuperMemoQuality.PASS_WITH_PERFECT_RECALL)
console.log(newCard)
/*
 * expect `newCard` to be:
 *
 * {
 *   term: '🍩',
 *   definition: '😋😋😋',
 *   rep: 1,
 *   repInterval: 1,
 *   easyFactor: 2.6,
 * }
 */

Types

interface SuperMemoItem {
  readonly rep: number
  readonly repInterval: number
  readonly easyFactor: number
}

Constants

enum SuperMemoQuality {
  FAIL_WITH_TOTAL_BLACKOUT,
  FAIL_BUT_FAMILIAR,
  FAIL_BUT_EASY,
  PASS_WITH_DIFFICULTY,
  PASS_WITH_HESITATION,
  PASS_WITH_PERFECT_RECALL,
}

API

sm2(item, grade)

Returns a new item with updated SM-2 fields. Throws an error for invalid parameters.

item

Type: SuperMemoItem
Required

The item to compute new SM-2 values.

grade

Type: SuperMemoQuality
Required

The quality grade used to compute new SM-2 values.

Author

Reference

License

MIT License