81 lines
3.1 KiB
JavaScript
81 lines
3.1 KiB
JavaScript
// noinspection ES6ConvertRequireIntoImport - this is CJS, not MJS
|
|
|
|
const { it } = require("node:test");
|
|
const assert = require("node:assert/strict");
|
|
const { Timespan } = require("./timespan");
|
|
|
|
it("should be able to return zero", () => {
|
|
const ts = new Timespan();
|
|
assert.equal(ts.totalMillis, 0);
|
|
});
|
|
|
|
it("should be able to return timespan from milliseconds", () => {
|
|
const ts = Timespan.fromMillis(1000);
|
|
assert.equal(ts.totalMillis, 1000);
|
|
assert.equal(ts.totalSeconds, 1);
|
|
});
|
|
|
|
it("should be able to return timespan from seconds", () => {
|
|
const ts = Timespan.fromSeconds(60);
|
|
assert.equal(ts.totalMillis, 60000);
|
|
assert.equal(ts.totalSeconds, 60);
|
|
assert.equal(ts.totalMinutes, 1);
|
|
assert.equal(ts.minutes, 1);
|
|
assert.equal(ts.hours, 0);
|
|
assert.equal(ts.days, 0);
|
|
});
|
|
|
|
it("should be pure", () => {
|
|
const count = 1000;
|
|
const timestamps = [];
|
|
for (let i = 0; i < count; i++) {
|
|
timestamps.push(Timespan.fromMillis(8972347984));
|
|
for (const ts2 of timestamps) {
|
|
assert.equal(ts2.totalMillis, 8972347984);
|
|
assert.equal(ts2.totalSeconds, 8972347);
|
|
assert.equal(ts2.totalMinutes, 149539);
|
|
assert.equal(ts2.totalHours, 2492);
|
|
assert.equal(ts2.totalDays, 103);
|
|
assert.equal(ts2.totalWeeks, 14);
|
|
assert.equal(ts2.totalMonths, 3);
|
|
assert.equal(ts2.totalYears, 0);
|
|
|
|
assert.equal(ts2.millis, 984);
|
|
assert.equal(ts2.seconds, 7);
|
|
assert.equal(ts2.minutes, 19);
|
|
assert.equal(ts2.hours, 20);
|
|
assert.equal(ts2.days, 12);
|
|
assert.equal(ts2.weekDays, 5);
|
|
assert.equal(ts2.weeks, 1);
|
|
assert.equal(ts2.months, 3);
|
|
assert.equal(ts2.years, 0);
|
|
}
|
|
}
|
|
});
|
|
|
|
it("should be able to stringify", () => {
|
|
const ts = Timespan.fromMillis(8972347984);
|
|
assert.equal(ts.toString(), "3 months, 1 weeks, 5 days, 20 hours, 19 minutes, 7 seconds, 984 milliseconds");
|
|
assert.equal(ts.toString(true), "3 months, 1 weeks, 5 days, 20 hours, 19 minutes, 7 seconds, 984 milliseconds");
|
|
assert.equal(ts.toString(true, false), "3 months, 1 weeks, 5 days, 20 hours, 19 minutes, 7 seconds");
|
|
assert.equal(ts.toString(false), "3 months, 12 days, 20 hours, 19 minutes, 7 seconds, 984 milliseconds");
|
|
assert.equal(ts.toString(false, false), "3 months, 12 days, 20 hours, 19 minutes, 7 seconds");
|
|
});
|
|
|
|
it("should be able to shortStringify", () => {
|
|
const ts = Timespan.fromMillis(8972347984);
|
|
assert.equal(ts.toShortString(), "3mo1w5d20h19m7s984ms");
|
|
assert.equal(ts.toShortString(true), "3mo1w5d20h19m7s984ms");
|
|
assert.equal(ts.toShortString(true, false), "3mo1w5d20h19m7s");
|
|
assert.equal(ts.toShortString(false), "3mo12d20h19m7s984ms");
|
|
assert.equal(ts.toShortString(false, false), "3mo12d20h19m7s");
|
|
});
|
|
|
|
it("should be able to shortStringify with spaces", () => {
|
|
const ts = Timespan.fromMillis(8972347984);
|
|
assert.equal(ts.toShortString(undefined, undefined, true), "3mo 1w 5d 20h 19m 7s 984ms");
|
|
assert.equal(ts.toShortString(true, undefined, true), "3mo 1w 5d 20h 19m 7s 984ms");
|
|
assert.equal(ts.toShortString(true, false, true), "3mo 1w 5d 20h 19m 7s");
|
|
assert.equal(ts.toShortString(false, undefined, true), "3mo 12d 20h 19m 7s 984ms");
|
|
assert.equal(ts.toShortString(false, false, true), "3mo 12d 20h 19m 7s");
|
|
}); |