Skip to content

Commit

Permalink
Feat/env (#30)
Browse files Browse the repository at this point in the history
* Feat/import .env.(local/production/development)

* Doc/add Environment Variables

* 2.2.0
  • Loading branch information
ishiko732 authored Oct 3, 2023
1 parent 3b1eeb8 commit 3881ec2
Show file tree
Hide file tree
Showing 8 changed files with 73 additions and 11 deletions.
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 6 additions & 0 deletions example/.env.local.example
Original file line number Diff line number Diff line change
@@ -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
4 changes: 3 additions & 1 deletion example/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {
fsrs, int,
fsrs, FSRSVersion, generatorParameters, int,
Rating,
State
} from 'ts-fsrs';
Expand Down Expand Up @@ -53,4 +53,6 @@ const test = () => {
generatorExample4().forEach(item => print_scheduling_card(item));
}

console.log(`FSRSVersion:${FSRSVersion}`)
console.log('defaultParams',generatorParameters())
test()
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand All @@ -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",
Expand Down
8 changes: 8 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

40 changes: 33 additions & 7 deletions src/fsrs/default.ts
Original file line number Diff line number Diff line change
@@ -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<FSRSParameters>) => {
return {
Expand Down
1 change: 1 addition & 0 deletions src/fsrs/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export {
generatorParameters,
createEmptyCard,
fsrs,
envParams
} from "./default";
export {
date_scheduler,
Expand Down
11 changes: 10 additions & 1 deletion src/fsrs/type.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
export type unit = "days" | "minutes";
export type int = number & { __int__: void };
export type double = number & { __double__: void };
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
}

0 comments on commit 3881ec2

Please sign in to comment.