Skip to content

Commit

Permalink
Fix/date is not a function (#68)
Browse files Browse the repository at this point in the history
* Fix/date is not a function

* Test/date real type is string or number

* 3.3.1
  • Loading branch information
ishiko732 authored Feb 5, 2024
1 parent 2771535 commit 342027a
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 12 deletions.
20 changes: 20 additions & 0 deletions __tests__/help.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down Expand Up @@ -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", () => {
Expand Down Expand Up @@ -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", () => {
Expand Down
12 changes: 11 additions & 1 deletion __tests__/show_diff_message.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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");
});
});

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");
})
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
2 changes: 1 addition & 1 deletion src/fsrs/default.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<FSRSParameters>,
Expand Down
20 changes: 11 additions & 9 deletions src/fsrs/help.ts
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down Expand Up @@ -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":
Expand All @@ -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();
Expand All @@ -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 {
Expand Down

0 comments on commit 342027a

Please sign in to comment.