From 3881ec2d91befeb6ee2696903eb3cff986eb8091 Mon Sep 17 00:00:00 2001 From: ishiko Date: Tue, 3 Oct 2023 16:09:59 +1000 Subject: [PATCH] Feat/env (#30) * Feat/import .env.(local/production/development) * Doc/add Environment Variables * 2.2.0 --- README.md | 9 +++++++++ example/.env.local.example | 6 ++++++ example/index.ts | 4 +++- package.json | 5 +++-- pnpm-lock.yaml | 8 ++++++++ src/fsrs/default.ts | 40 +++++++++++++++++++++++++++++++------- src/fsrs/index.ts | 1 + src/fsrs/type.ts | 11 ++++++++++- 8 files changed, 73 insertions(+), 11 deletions(-) create mode 100644 example/.env.local.example diff --git a/README.md b/README.md index 115e97a..796073a 100644 --- a/README.md +++ b/README.md @@ -9,6 +9,15 @@ developers apply FSRS to their flashcard applications, there by improving the us npm install ts-fsrs ``` +# Environment Variables +If you need to customize default parameters, you can modify the values using `.env`/`.env.local`/`.env.production`/`.env.development`. + +Copy the [.env.local.example](./example/.env.local.example) file in this directory to .env.local (which will be ignored by Git): + +```bash +cp .env.local.example .env.local +``` + # Example ```typescript diff --git a/example/.env.local.example b/example/.env.local.example new file mode 100644 index 0000000..13bfc3b --- /dev/null +++ b/example/.env.local.example @@ -0,0 +1,6 @@ +FSRS_REQUEST_RETENTION=0.8 +FSRS_MAXIMUM_INTERVAL=36500 +FSRS_EASY_BOUND=1.3 +FSRS_HARD_FACTOR=1.2 +FSRS_W='[1, 1, 5, -0.5, -0.5, 0.2, 1.4, -0.12, 0.8, 2, -0.2, 0.2, 1]' +FSRS_ENABLE_FUZZ=true \ No newline at end of file diff --git a/example/index.ts b/example/index.ts index 4d0f37e..21fa709 100644 --- a/example/index.ts +++ b/example/index.ts @@ -1,5 +1,5 @@ import { - fsrs, int, + fsrs, FSRSVersion, generatorParameters, int, Rating, State } from 'ts-fsrs'; @@ -53,4 +53,6 @@ const test = () => { generatorExample4().forEach(item => print_scheduling_card(item)); } +console.log(`FSRSVersion:${FSRSVersion}`) +console.log('defaultParams',generatorParameters()) test() \ No newline at end of file diff --git a/package.json b/package.json index e6da2ba..d8c1403 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "ts-fsrs", - "version": "2.1.1", + "version": "2.2.0", "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", @@ -10,11 +10,12 @@ "FSRS" ], "dependencies": { + "dotenv": "^16.3.1", "seedrandom": "^3.0.5" }, "devDependencies": { - "@types/node": "^20.6.2", "@types/jest": "^29.5.5", + "@types/node": "^20.6.2", "@types/seedrandom": "^3.0.5", "@typescript-eslint/eslint-plugin": "^6.7.0", "@typescript-eslint/parser": "^6.7.0", diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 5f9aabf..35acd98 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -5,6 +5,9 @@ settings: excludeLinksFromLockfile: false dependencies: + dotenv: + specifier: ^16.3.1 + version: 16.3.1 seedrandom: specifier: ^3.0.5 version: 3.0.5 @@ -1390,6 +1393,11 @@ packages: esutils: 2.0.3 dev: true + /dotenv@16.3.1: + resolution: {integrity: sha512-IPzF4w4/Rd94bA9imS68tZBaYyBWSCE47V1RGuMrB94iyTOIEwRmVL2x/4An+6mETpLrKJ5hQkB8W4kFAadeIQ==} + engines: {node: '>=12'} + dev: false + /electron-to-chromium@1.4.523: resolution: {integrity: sha512-9AreocSUWnzNtvLcbpng6N+GkXnCcBR80IQkxRC9Dfdyg4gaWNUPBujAHUpKkiUkoSoR9UlhA4zD/IgBklmhzg==} dev: true diff --git a/src/fsrs/default.ts b/src/fsrs/default.ts index 2723a0e..518a218 100644 --- a/src/fsrs/default.ts +++ b/src/fsrs/default.ts @@ -1,15 +1,41 @@ import { Card, FSRSParameters, State, FSRS } from "./index"; +import dotenv from "dotenv"; +import { EnvParams } from "./type"; -export const default_request_retention = 0.9; -export const default_maximum_interval = 36500; -export const default_easy_bonus = 1.3; -export const default_hard_factor = 1.2; -export const default_w = [ +dotenv.config({ path: `./.env.local` }); +dotenv.config({ path: `./.env.production` }); +dotenv.config({ path: `./.env.` }); +dotenv.config({ path: `./.env.development` }); + +export const envParams: EnvParams = { + FSRS_REQUEST_RETENTION: Number(process.env.FSRS_REQUEST_RETENTION), + FSRS_MAXIMUM_INTERVAL: Number(process.env.FSRS_MAXIMUM_INTERVAL), + FSRS_EASY_BOUND: Number(process.env.FSRS_EASY_BOUND), + FSRS_HARD_FACTOR: Number(process.env.FSRS_HARD_FACTOR), + FSRS_W: process.env.FSRS_W + ? JSON.parse(process.env.FSRS_W as string) + : undefined, + FSRS_ENABLE_FUZZ: Boolean(process.env.FSRS_ENABLE_FUZZ), +}; + +export const default_request_retention = !isNaN(envParams.FSRS_REQUEST_RETENTION) + ? envParams.FSRS_REQUEST_RETENTION + : 0.9; +export const default_maximum_interval = !isNaN(envParams.FSRS_MAXIMUM_INTERVAL) + ? envParams.FSRS_MAXIMUM_INTERVAL + : 36500; +export const default_easy_bonus = !isNaN(envParams.FSRS_EASY_BOUND) + ? envParams.FSRS_EASY_BOUND + : 1.3; +export const default_hard_factor = !isNaN(envParams.FSRS_HARD_FACTOR) + ? envParams.FSRS_HARD_FACTOR + : 1.2; +export const default_w = envParams.FSRS_W || [ 1, 1, 5, -0.5, -0.5, 0.2, 1.4, -0.12, 0.8, 2, -0.2, 0.2, 1, ]; -export const default_enable_fuzz = false; +export const default_enable_fuzz = envParams.FSRS_ENABLE_FUZZ || false; -export const FSRSVersion: string = "2.1.1"; +export const FSRSVersion: string = "2.2.0"; export const generatorParameters = (props?: Partial) => { return { diff --git a/src/fsrs/index.ts b/src/fsrs/index.ts index 17c6843..facdd19 100644 --- a/src/fsrs/index.ts +++ b/src/fsrs/index.ts @@ -10,6 +10,7 @@ export { generatorParameters, createEmptyCard, fsrs, + envParams } from "./default"; export { date_scheduler, diff --git a/src/fsrs/type.ts b/src/fsrs/type.ts index dcd24cb..cbba460 100644 --- a/src/fsrs/type.ts +++ b/src/fsrs/type.ts @@ -1,3 +1,12 @@ export type unit = "days" | "minutes"; export type int = number & { __int__: void }; -export type double = number & { __double__: void }; \ No newline at end of file +export type double = number & { __double__: void }; + +export interface EnvParams{ + FSRS_REQUEST_RETENTION:number, + FSRS_MAXIMUM_INTERVAL:number, + FSRS_EASY_BOUND:number, + FSRS_HARD_FACTOR:number, + FSRS_W?:number[], + FSRS_ENABLE_FUZZ?:boolean +} \ No newline at end of file