diff --git a/__tests__/help.test.ts b/__tests__/help.test.ts index 5369314..5cec4e9 100644 --- a/__tests__/help.test.ts +++ b/__tests__/help.test.ts @@ -23,6 +23,8 @@ test("Date.prototype.format", () => { const last_review = new Date(2022, 11, 29, 12, 30, 0, 0); expect(now.format()).toEqual("2022-12-30 12:30:00"); expect(formatDate(now)).toEqual("2022-12-30 12:30:00"); + expect(formatDate(now.getTime())).toEqual("2022-12-30 12:30:00"); + expect(formatDate(now.toUTCString())).toEqual("2022-12-30 12:30:00"); const TIMEUNITFORMAT_TEST = ["秒", "分", "时", "天", "月", "年"]; expect(now.dueFormat(last_review)).toEqual("1"); expect(now.dueFormat(last_review, true)).toEqual("1day"); @@ -61,6 +63,15 @@ describe("date_scheduler", () => { expect(date_scheduler(now, t, true)).toEqual(expected); }); + + test("Date data real type is string/number", () => { + const now = "2023-01-01T12:00:00Z"; + const t = 2; + const expected = new Date("2023-01-03T12:00:00Z"); + + expect(date_scheduler(now, t, true)).toEqual(expected); + }); + }); describe("date_diff", () => { @@ -97,6 +108,15 @@ describe("date_diff", () => { const expected = -30; expect(date_diff(now, pre, unit)).toBe(expected); }); + + test("Date data real type is string/number", () => { + const now = "2023-11-25T12:30:00Z"; + const pre = new Date("2023-11-25T12:00:00Z").getTime(); + const unit = "minutes"; + const expected = 30; + expect(date_diff(now, pre, unit)).toBe(expected); + }); + }); describe("fixDate", () => { diff --git a/__tests__/show_diff_message.ts b/__tests__/show_diff_message.ts index b117363..112196b 100644 --- a/__tests__/show_diff_message.ts +++ b/__tests__/show_diff_message.ts @@ -134,4 +134,14 @@ test("wrong timeUnit length", () => { expect(show_diff_message(t2, t1, true)).toEqual("1year"); expect(fixDate(t2).dueFormat(fixDate(t1),true,TIMEUNITFORMAT_TEST)).not.toEqual("1年"); expect(fixDate(t2).dueFormat(fixDate(t1),true,TIMEUNITFORMAT_TEST)).toEqual("1year"); -}); \ No newline at end of file +}); + +test("Date data real type is string/number", ()=>{ + const TIMEUNITFORMAT_TEST = ["年"]; + const t1 = new Date(); + const t2 = new Date(t1.getTime() + 1000 * 60 * 60 * 24 * 31 * 13).toDateString(); + expect(show_diff_message(t2, t1.getTime())).toBe("1"); + expect(show_diff_message(t2, t1.toUTCString(), true)).toEqual("1year"); + expect(fixDate(t2).dueFormat(fixDate(t1),true,TIMEUNITFORMAT_TEST)).not.toEqual("1年"); + expect(fixDate(t2).dueFormat(fixDate(t1),true,TIMEUNITFORMAT_TEST)).toEqual("1year"); +}) \ No newline at end of file diff --git a/package.json b/package.json index 1b12b3f..5f55c47 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "ts-fsrs", - "version": "3.3.0", + "version": "3.3.1", "description": "ts-fsrs is a ES modules package based on TypeScript, used to implement the Free Spaced Repetition Scheduler (FSRS) algorithm. It helps developers apply FSRS to their flashcard applications, there by improving the user learning experience.", "main": "dist/index.cjs", "module": "dist/index.mjs", diff --git a/src/fsrs/default.ts b/src/fsrs/default.ts index d081d69..54b3e52 100644 --- a/src/fsrs/default.ts +++ b/src/fsrs/default.ts @@ -9,7 +9,7 @@ export const default_w = [ ]; export const default_enable_fuzz = false; -export const FSRSVersion: string = "3.3.0"; +export const FSRSVersion: string = "3.3.1"; export const generatorParameters = ( props?: Partial, diff --git a/src/fsrs/help.ts b/src/fsrs/help.ts index 315af74..7cebbcf 100644 --- a/src/fsrs/help.ts +++ b/src/fsrs/help.ts @@ -1,5 +1,6 @@ import type { int, unit } from "./type"; -import {Grade, Rating, State} from "./models"; +import type { DateInput, Grade } from './models'; +import { Rating, State } from './models'; declare global { export interface Date { @@ -41,19 +42,19 @@ Date.prototype.dueFormat = function (last_review: Date, unit?: boolean,timeUnit? * @param isDay (可选)是否按天数单位进行偏移,默认为 false,表示按分钟单位计算偏移 * @returns 偏移后的日期和时间对象 */ -export function date_scheduler(now: Date, t: number, isDay?: boolean): Date { +export function date_scheduler(now: DateInput, t: number, isDay?: boolean): Date { return new Date( isDay - ? now.getTime() + t * 24 * 60 * 60 * 1000 - : now.getTime() + t * 60 * 1000, + ? fixDate(now).getTime() + t * 24 * 60 * 60 * 1000 + : fixDate(now).getTime() + t * 60 * 1000, ); } -export function date_diff(now: Date, pre: Date, unit: unit): number { +export function date_diff(now: DateInput, pre: DateInput, unit: unit): number { if (!now || !pre) { throw new Error("Invalid date"); } - const diff = now.getTime() - pre.getTime(); + const diff = fixDate(now).getTime() - fixDate(pre).getTime(); let r = 0; switch (unit) { case "days": @@ -66,7 +67,8 @@ export function date_diff(now: Date, pre: Date, unit: unit): number { return r; } -export function formatDate(date: Date): string { +export function formatDate(dateInput: DateInput): string { + const date = fixDate(dateInput); const year: number = date.getFullYear(); const month: number = date.getMonth() + 1; const day: number = date.getDate(); @@ -87,8 +89,8 @@ const TIMEUNIT = [60, 60, 24, 31, 12]; const TIMEUNITFORMAT = ["second", "min", "hour", "day", "month", "year"]; export function show_diff_message( - due: Date, - last_review: Date, + due: DateInput, + last_review: DateInput, unit?: boolean, timeUnit: string[] = TIMEUNITFORMAT, ): string {