Skip to content

Commit

Permalink
replace external dependencies with standard library
Browse files Browse the repository at this point in the history
  • Loading branch information
ayakovlenko committed Aug 11, 2024
1 parent 40ebe86 commit 5b7ee67
Show file tree
Hide file tree
Showing 7 changed files with 22 additions and 16 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
.idea/
cov_profile/

*.review.yaml
4 changes: 1 addition & 3 deletions deno.json
Original file line number Diff line number Diff line change
@@ -1,19 +1,17 @@
{
"tasks": {
"run": "deno run --allow-read --allow-write --allow-env=NODE_ENV src/cli.ts",
"run": "deno run --allow-read --allow-write --allow-env=NODE_ENV,TERM src/cli.ts",
"migrate": "deno run --allow-read --allow-write src/tools/migrate.ts",
"test": "./scripts/test.sh",
"test-lcov": "./scripts/test-lcov.sh"
},
"imports": {
"@cliffy/prompt": "https://deno.land/x/cliffy@v0.25.2/prompt/input.ts",
"@std/assert": "jsr:@std/assert@^1.0.1",
"@std/cli": "jsr:@std/cli@^1.0.0",
"@std/csv": "jsr:@std/csv@^0.224.3",
"@std/fmt": "jsr:@std/fmt@^0.225.6",
"@std/path": "jsr:@std/path@^1.0.2",
"@std/yaml": "jsr:@std/yaml@^0.224.3",
"dayjs": "npm:dayjs@^1.11.12",
"fastest-levenshtein": "npm:fastest-levenshtein@^1.0.16",
"supermemo": "npm:supermemo@^2.0.17"
}
Expand Down
6 changes: 0 additions & 6 deletions deno.lock

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

8 changes: 4 additions & 4 deletions src/cli.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { Input } from "@cliffy/prompt";
import { parseArgs } from "@std/cli/parse-args";
import { green, red, yellow } from "@std/fmt/colors";
import { loadDeck } from "./deck.ts";
import { shuffle } from "./collections.ts";
import { loadDeck } from "./deck.ts";
import { Input } from "./input.ts";
import { ratio } from "./levenshtein.ts";
import { deriveReviewfile } from "./pathutil.ts";
import {
loadReviews,
newReviewItem,
Expand All @@ -12,7 +13,6 @@ import {
saveReviews,
score2grade,
} from "./review.ts";
import { deriveReviewfile } from "./pathutil.ts";

const args = parseArgs(Deno.args) as {
deck: string;
Expand Down Expand Up @@ -91,7 +91,7 @@ console.log("To review:", dueDateItems.length);
for (const review of dueDateItems) {
console.log(review.front);

let answer: string = await Input.prompt("");
let answer: string = await Input.question("> ");

// trip and replace all multi-space characters with just one
answer = answer.replaceAll(" +", " ").trim();
Expand Down
Empty file added src/cmd/cli/input.ts
Empty file.
4 changes: 4 additions & 0 deletions src/input.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import * as readline from "node:readline/promises";
import { stdin as input, stdout as output } from "node:process";

export const Input = readline.createInterface({ input, output });
14 changes: 11 additions & 3 deletions src/review.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import dayjs from "dayjs";
import { supermemo, SuperMemoGrade, SuperMemoItem } from "supermemo";
import { Card } from "./card.ts";
import { parse as parseYaml, stringify as stringifyYaml } from "@std/yaml";
Expand Down Expand Up @@ -42,12 +41,14 @@ async function saveReviews(
}

function newReviewItem(flashcard: Card): ReviewItem {
const now = new Date(Date.now());

return {
...flashcard,
interval: 0,
repetition: 0,
efactor: 2.5,
dueDate: dayjs(Date.now()).toISOString(),
dueDate: now.toISOString(),
};
}

Expand Down Expand Up @@ -78,11 +79,18 @@ function score2grade(score: number): SuperMemoGrade {
function practice(reviewItem: ReviewItem, grade: SuperMemoGrade): ReviewItem {
const { interval, repetition, efactor } = supermemo(reviewItem, grade);

const dueDate = dayjs(Date.now()).add(interval, "day").toISOString();
const now = new Date(Date.now());

const dueDate = addDays(now, interval).toISOString();

return { ...reviewItem, interval, repetition, efactor, dueDate };
}

function addDays(date: Date, days: number): Date {
date.setDate(date.getDate() + days);
return date;
}

export type { ReviewItem };

export { loadReviews, newReviewItem, practice, saveReviews, score2grade };

0 comments on commit 5b7ee67

Please sign in to comment.