Anki/ts/lib/tslib/time.test.ts
user1823 e546c6d11f
Improve natural unit conversion for a time b/w 360 to 365 days (#3901)
* Improve natural unit conversion for a time b/w 360 to 365 days

Previously, 363 days would be converted to 12.1 months, which is quite confusing because
- a user would think that if the value is more than 12 months, why it isn't displayed in years
- the value is actually less than a year, which is counterintuitive as 12.1 m suggests a value more than a year.

* precise

* Update time.ts to match timespan.rs

* Add another test

* Use average duration of a month instead

* Update time.ts

* Update test_schedv3.py

* Update time.test.ts
2025-04-13 14:26:34 +10:00

30 lines
1.4 KiB
TypeScript

// Copyright: Ankitects Pty Ltd and contributors
// License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
import { expect, test } from "vitest";
import { naturalUnit, naturalWholeUnit, TimespanUnit } from "./time";
test("natural unit", () => {
expect(naturalUnit(5)).toBe(TimespanUnit.Seconds);
expect(naturalUnit(59)).toBe(TimespanUnit.Seconds);
expect(naturalUnit(60)).toBe(TimespanUnit.Minutes);
expect(naturalUnit(60 * 60 - 1)).toBe(TimespanUnit.Minutes);
expect(naturalUnit(60 * 60)).toBe(TimespanUnit.Hours);
expect(naturalUnit(60 * 60 * 24)).toBe(TimespanUnit.Days);
expect(naturalUnit(60 * 60 * 24 * 31)).toBe(TimespanUnit.Months);
});
test("natural whole unit", () => {
expect(naturalWholeUnit(5)).toBe(TimespanUnit.Seconds);
expect(naturalWholeUnit(59)).toBe(TimespanUnit.Seconds);
expect(naturalWholeUnit(60)).toBe(TimespanUnit.Minutes);
expect(naturalWholeUnit(61)).toBe(TimespanUnit.Seconds);
expect(naturalWholeUnit(90)).toBe(TimespanUnit.Seconds);
expect(naturalWholeUnit(60 * 60 - 1)).toBe(TimespanUnit.Seconds);
expect(naturalWholeUnit(60 * 60 + 1)).toBe(TimespanUnit.Seconds);
expect(naturalWholeUnit(60 * 60)).toBe(TimespanUnit.Hours);
expect(naturalWholeUnit(24 * 60 * 60 - 1)).toBe(TimespanUnit.Seconds);
expect(naturalWholeUnit(24 * 60 * 60 + 1)).toBe(TimespanUnit.Seconds);
expect(naturalWholeUnit(24 * 60 * 60)).toBe(TimespanUnit.Days);
});