import InWords from "./in_words.ts";
interface KeyList { d: string; h: string; m: string; s: string; ms: string; us: string; ns: string;}const keyList: KeyList = { d: "days", h: "hours", m: "minutes", s: "seconds", ms: "milliseconds", us: "microseconds", ns: "nanoseconds",};
export type DurationKeys = "d" | "h" | "m" | "s" | "ms" | "us" | "ns";
export interface KeyValue { type: DurationKeys; value: number;}
export interface DurationObj { raw: number; d: number; h: number; m: number; s: number; ms: number; us: number; ns: number;}
const BaseDurationObj: DurationObj = { raw: 0, d: 0, h: 0, m: 0, s: 0, ms: 0, us: 0, ns: 0,};
export class Duration { raw: number; d: number; h: number; m: number; s: number; ms: number; us: number; ns: number; constructor(timestamp: number = Duration.getCurrentDuration()) { if (timestamp < 0) timestamp = 0; timestamp = Number(timestamp); this.raw = timestamp; this.d = Math.trunc(timestamp / 86400000); this.h = Math.trunc(timestamp / 3600000) % 24; this.m = Math.trunc(timestamp / 60000) % 60; this.s = Math.trunc(timestamp / 1000) % 60; this.ms = Math.trunc(timestamp) % 1000; this.us = Math.trunc(timestamp * 1000) % 1000; this.ns = Math.trunc(timestamp * 1000000) % 1000; } get array(): KeyValue[] { return [ { type: "d", value: this.d }, { type: "h", value: this.h }, { type: "m", value: this.m }, { type: "s", value: this.s }, { type: "ms", value: this.ms }, { type: "us", value: this.us }, { type: "ns", value: this.ns }, ]; } get µs(): number { return this.us; } get json(): DurationObj { return this.array.reduce( (acc, stuff) => ((acc[stuff.type] = stuff.value), acc), BaseDurationObj, ); } addDays(n: number): Duration { this.d += n; return this.reload(); } addHours(n: number): Duration { this.h += n; return this.reload(); } addMinutes(n: number): Duration { this.m += n; return this.reload(); } addSeconds(n: number): Duration { this.s += n; return this.reload(); } addMilliseconds(n: number): Duration { this.ms += n; return this.reload(); } addMicroseconds(n: number): Duration { this.us += n; return this.reload(); } addNanoseconds(n: number): Duration { this.ns += n; return this.reload(); } clone(): Duration { return new Duration(this.raw); } getSimpleFormattedDuration(): string { return this.toString(); } getFormattedDurationArray(): string[] { return this.array.map((x) => ["ms", "us", "ns"].includes(x.type) ? addZero(x.value, 3) : addZero(x.value, 2) ); } reload(): Duration { const ts = this.d * 8.64e7 + this.h * 3600000 + this.m * 60000 + this.s * 1000 + this.ms + (this.us / 1000) + (this.ns / 1000000); if (ts === this.raw) return this; const newDuration = new Duration(ts); this.d = newDuration.d; this.h = newDuration.h; this.m = newDuration.m; this.s = newDuration.s; this.ms = newDuration.ms; this.ns = newDuration.ns; this.us = newDuration.us; this.raw = newDuration.raw; return this; } setDays(n: number): Duration { this.d = n; return this.reload(); } setHours(n: number): Duration { this.h = n; return this.reload(); } setMinutes(n: number): Duration { this.m = n; return this.reload(); } setSeconds(n: number): Duration { this.s = n; return this.reload(); } setMilliseconds(n: number): Duration { this.ms = n; return this.reload(); } setMicroseconds(n: number): Duration { this.us = n; return this.reload(); } setNanoseconds(n: number): Duration { this.ns = n; return this.reload(); } toDescriptiveString(values: string[] | null = []): string { if (!Array.isArray(values) || values.length == 0) { return `${ this.array.map((x) => `${x.value} ${keyList[x.type]}`).join(", ") }`; } return `${ this.array .filter((x) => values.includes(x.type)) .map((x) => `${x.value} ${keyList[x.type]}`) .join(", ") }`; } toJSON(): DurationObj { return this.json; } toShortString(values: string[] | null = []): string { if (!Array.isArray(values) || values.length == 0) { return `${this.array.map((x) => `${x.value}${x.type}`).join(" ")}`; } return `${ this.array .filter((x) => values.includes(x.type)) .map((x) => `${x.value}${x.type}`) .join(" ") }`; } toString(): string { return `${this.getFormattedDurationArray().join(":")}`; } toTimeString( fromT: DurationKeys = "d", toT: DurationKeys = "ns", ): string { if ( typeof fromT !== "string" || typeof toT !== "string" || !Object.prototype.hasOwnProperty.call(keyList, fromT.toLowerCase()) || !Object.prototype.hasOwnProperty.call(keyList, toT.toLowerCase()) ) { return this.getSimpleFormattedDuration(); } const durations = this.getFormattedDurationArray(); const listOfKeys = Object.keys(keyList); return durations.slice( listOfKeys.indexOf(fromT), listOfKeys.indexOf(toT) + 1, ).join(":"); } toWordString(values: string[] | null = []): string { if (!Array.isArray(values) || values.length === 0) { return `${ this.array .map((x) => `${InWords(x.value).trim()} ${keyList[x.type]}`) .join(", ") }`; } if (values.length > 0) { return `${ this.array .filter((x) => values.includes(x.type)) .map((x) => `${InWords(x.value).trim()} ${keyList[x.type]}`) .join(", ") }`; } return `${ this.array .map((x) => `${InWords(x.value).trim()} ${keyList[x.type]}`) .join(", ") }`; }
valueOf(): number { return this.raw; } static between( duration1: string | number | Duration, duration2: string | number | Duration | undefined | null, ): Duration { let myDuration1: Duration, myDuration2: Duration; if (duration1 instanceof Duration) myDuration1 = duration1; else if (typeof duration1 === "string") { if (isNaN(+duration1)) myDuration1 = Duration.fromString(duration1); else myDuration1 = new Duration(+duration1); } else if (typeof duration1 === "number") { myDuration1 = new Duration(duration1); } else myDuration1 = new Duration(); if (duration2 instanceof Duration) myDuration2 = duration2; else if (typeof duration2 === "string") { if (isNaN(+duration2)) myDuration2 = Duration.fromString(duration2); else myDuration2 = new Duration(+duration2); } else if (typeof duration2 === "number") { myDuration2 = new Duration(duration2); } else myDuration2 = new Duration();
return new Duration( myDuration1.raw > myDuration2.raw ? myDuration1.raw - myDuration2.raw : myDuration2.raw - myDuration1.raw, ); } static fromString(str: string, doNotParse = false): Duration { const { raw, d, h, m, s, ms, ns, us } = Duration.readString(str); const ts = raw;
const newDuration = new Duration(ts); if (doNotParse) { newDuration.d = d; newDuration.h = h; newDuration.m = m; newDuration.s = s; newDuration.ms = ms; newDuration.ns = ns; newDuration.us = us; } return newDuration; } static getCurrentDuration(): number { return Date.now() - new Date().setHours(0, 0, 0, 0); } static readString(str: string): DurationObj { str = str.replace(/\s\s/g, ""); const days = matchUnit(str, "d") || matchUnit(str, "days") || matchUnit(str, "day"); const hours = matchUnit(str, "h") || matchUnit(str, "hours") || matchUnit(str, "hour"); const minutes = matchUnit(str, "m") || matchUnit(str, "min") || matchUnit(str, "minute") || matchUnit(str, "mins") || matchUnit(str, "minutes"); const seconds = matchUnit(str, "s") || matchUnit(str, "sec") || matchUnit(str, "second") || matchUnit(str, "secs") || matchUnit(str, "seconds"); const milliseconds = matchUnit(str, "ms") || matchUnit(str, "millisecond") || matchUnit(str, "milliseconds"); const nanoseconds = matchUnit(str, "ns") || matchUnit(str, "nanosecond") || matchUnit(str, "nanoseconds"); const microseconds = matchUnit(str, "µs") || matchUnit(str, "microsecond") || matchUnit(str, "microseconds"); matchUnit(str, "us"); return { raw: days * 8.64e7 + hours * 3600000 + minutes * 60000 + seconds * 1000 + milliseconds + (microseconds / 1000) + (nanoseconds / 1000000), d: days, h: hours, m: minutes, s: seconds, ms: milliseconds, ns: nanoseconds, us: microseconds, }; } static since(when: number | Date): Duration { return Duration.between( when instanceof Date ? when.getTime() : when, Date.now(), ); }}
export function matchUnit(str: string, t: string): number { const reg = new RegExp(`(\\d+)\\s?${t}(?:[^a-z]|$)`, "i"); const matched = reg.exec(str); if (!matched) return 0; return parseInt(matched[1].replace(t, ""));}
export function addZero(num: number, digits = 3): string { const arr = new Array(digits).fill(0); return `${arr.join("").slice(0, 0 - num.toString().length)}${num}`;}
export default Duration;