From 5b7ee679f81a817bfbb6fa1c4a21f30ea76946e1 Mon Sep 17 00:00:00 2001 From: tony Date: Mon, 12 Aug 2024 00:07:40 +0300 Subject: [PATCH] replace external dependencies with standard library --- .gitignore | 2 ++ deno.json | 4 +--- deno.lock | 6 ------ src/cli.ts | 8 ++++---- src/cmd/cli/input.ts | 0 src/input.ts | 4 ++++ src/review.ts | 14 +++++++++++--- 7 files changed, 22 insertions(+), 16 deletions(-) create mode 100644 src/cmd/cli/input.ts create mode 100644 src/input.ts diff --git a/.gitignore b/.gitignore index bae6ffb..baf889c 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,4 @@ .idea/ cov_profile/ + +*.review.yaml diff --git a/deno.json b/deno.json index 0933dfa..b1a2669 100644 --- a/deno.json +++ b/deno.json @@ -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" } diff --git a/deno.lock b/deno.lock index 01f9622..9026e3c 100644 --- a/deno.lock +++ b/deno.lock @@ -16,7 +16,6 @@ "jsr:@std/yaml": "jsr:@std/yaml@0.224.3", "jsr:@std/yaml@^0.224.3": "jsr:@std/yaml@0.224.3", "npm:@types/node": "npm:@types/node@18.16.19", - "npm:dayjs@^1.11.12": "npm:dayjs@1.11.12", "npm:fastest-levenshtein@^1.0.16": "npm:fastest-levenshtein@1.0.16", "npm:supermemo@^2.0.17": "npm:supermemo@2.0.17" }, @@ -72,10 +71,6 @@ "integrity": "sha512-IXl7o+R9iti9eBW4Wg2hx1xQDig183jj7YLn8F7udNceyfkbn1ZxmzZXuak20gR40D7pIkIY1kYGx5VIGbaHKA==", "dependencies": {} }, - "dayjs@1.11.12": { - "integrity": "sha512-Rt2g+nTbLlDWZTwwrIXjy9MeiZmSDI375FvZs72ngxx8PDC6YXOeR3q5LAuPzjZQxhiWdRKac7RKV+YyQYfYIg==", - "dependencies": {} - }, "fastest-levenshtein@1.0.16": { "integrity": "sha512-eRnCtTTtGZFpQCwhJiUOuxPQWRXVKYDn0b2PeHfXL6/Zi53SLAzAHfVhVWK2AryC/WH05kGfxhFIPvTF0SXQzg==", "dependencies": {} @@ -233,7 +228,6 @@ "jsr:@std/fmt@^0.225.6", "jsr:@std/path@^1.0.2", "jsr:@std/yaml@^0.224.3", - "npm:dayjs@^1.11.12", "npm:fastest-levenshtein@^1.0.16", "npm:supermemo@^2.0.17" ] diff --git a/src/cli.ts b/src/cli.ts index 19867bf..f9cf90d 100644 --- a/src/cli.ts +++ b/src/cli.ts @@ -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, @@ -12,7 +13,6 @@ import { saveReviews, score2grade, } from "./review.ts"; -import { deriveReviewfile } from "./pathutil.ts"; const args = parseArgs(Deno.args) as { deck: string; @@ -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(); diff --git a/src/cmd/cli/input.ts b/src/cmd/cli/input.ts new file mode 100644 index 0000000..e69de29 diff --git a/src/input.ts b/src/input.ts new file mode 100644 index 0000000..e9dd21d --- /dev/null +++ b/src/input.ts @@ -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 }); diff --git a/src/review.ts b/src/review.ts index d4d0c53..c6263f7 100644 --- a/src/review.ts +++ b/src/review.ts @@ -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"; @@ -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(), }; } @@ -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 };