@kyvrixon/utils 0.0.2 → 0.0.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.js +2 -1
- package/dist/lib/formatSeconds.js +99 -0
- package/dist/types/index.d.ts +2 -1
- package/dist/types/index.d.ts.map +1 -1
- package/dist/types/lib/Logger.d.ts +1 -1
- package/dist/types/lib/Logger.d.ts.map +1 -1
- package/dist/types/lib/formatSeconds.d.ts +9 -0
- package/dist/types/lib/formatSeconds.d.ts.map +1 -0
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -1 +1,2 @@
|
|
|
1
|
-
export * from "./lib/
|
|
1
|
+
export * from "./lib/logger";
|
|
2
|
+
export * from "./lib/formatSeconds";
|
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
const UNITS = {
|
|
2
|
+
y: { label: "year", short: "y", ms: 0 },
|
|
3
|
+
mo: { label: "month", short: "mo", ms: 0 },
|
|
4
|
+
w: { label: "week", short: "w", ms: 7 * 24 * 3600 * 1000 },
|
|
5
|
+
d: { label: "day", short: "d", ms: 24 * 3600 * 1000 },
|
|
6
|
+
h: { label: "hour", short: "h", ms: 3600 * 1000 },
|
|
7
|
+
m: { label: "minute", short: "m", ms: 60 * 1000 },
|
|
8
|
+
s: { label: "second", short: "s", ms: 1000 },
|
|
9
|
+
ms: { label: "millisecond", short: "ms", ms: 1 },
|
|
10
|
+
};
|
|
11
|
+
const ALL_UNITS_ORDER = [
|
|
12
|
+
"y",
|
|
13
|
+
"mo",
|
|
14
|
+
"w",
|
|
15
|
+
"d",
|
|
16
|
+
"h",
|
|
17
|
+
"m",
|
|
18
|
+
"s",
|
|
19
|
+
"ms",
|
|
20
|
+
];
|
|
21
|
+
export const formatSeconds = (seconds, options = {}) => {
|
|
22
|
+
const includeZeroUnits = options.includeZeroUnits ?? false;
|
|
23
|
+
const onlyUnits = options.onlyUnits ?? [];
|
|
24
|
+
const format = options.format ?? "long";
|
|
25
|
+
const customFormatter = options.customFormatter;
|
|
26
|
+
const totalMs = Math.max(0, Number(seconds) * 1000);
|
|
27
|
+
const unitsToDisplay = ALL_UNITS_ORDER.filter((u) => onlyUnits.length ? onlyUnits.includes(u) : true);
|
|
28
|
+
if (totalMs === 0) {
|
|
29
|
+
const zeroParts = [];
|
|
30
|
+
for (const u of unitsToDisplay) {
|
|
31
|
+
if (format === "short") {
|
|
32
|
+
zeroParts.push(`0${UNITS[u].short}`);
|
|
33
|
+
}
|
|
34
|
+
else {
|
|
35
|
+
zeroParts.push(`0 ${UNITS[u].label}`);
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
return zeroParts.join(format === "short" ? " " : ", ");
|
|
39
|
+
}
|
|
40
|
+
const now = new Date();
|
|
41
|
+
const end = new Date(now.getTime() + totalMs);
|
|
42
|
+
let years = 0;
|
|
43
|
+
if (unitsToDisplay.includes("y") || unitsToDisplay.includes("mo")) {
|
|
44
|
+
years = end.getFullYear() - now.getFullYear();
|
|
45
|
+
}
|
|
46
|
+
let months = 0;
|
|
47
|
+
if (unitsToDisplay.includes("mo")) {
|
|
48
|
+
months = end.getMonth() - now.getMonth();
|
|
49
|
+
if (years < 0)
|
|
50
|
+
months += 12;
|
|
51
|
+
}
|
|
52
|
+
const remainingMs = end.getTime() -
|
|
53
|
+
new Date(now.getFullYear() + years, now.getMonth() + months, now.getDate(), now.getHours(), now.getMinutes(), now.getSeconds(), now.getMilliseconds()).getTime();
|
|
54
|
+
const diff = {
|
|
55
|
+
y: years,
|
|
56
|
+
mo: months,
|
|
57
|
+
w: Math.floor(remainingMs / UNITS.w.ms),
|
|
58
|
+
d: Math.floor((remainingMs % UNITS.w.ms) / UNITS.d.ms),
|
|
59
|
+
h: Math.floor((remainingMs % UNITS.d.ms) / UNITS.h.ms),
|
|
60
|
+
m: Math.floor((remainingMs % UNITS.h.ms) / UNITS.m.ms),
|
|
61
|
+
s: Math.floor((remainingMs % UNITS.m.ms) / UNITS.s.ms),
|
|
62
|
+
ms: remainingMs % 1000,
|
|
63
|
+
};
|
|
64
|
+
const showZeros = includeZeroUnits || onlyUnits.length > 0;
|
|
65
|
+
const parts = [];
|
|
66
|
+
for (const unit of unitsToDisplay) {
|
|
67
|
+
const value = diff[unit] ?? 0;
|
|
68
|
+
if (value || showZeros) {
|
|
69
|
+
let label = "";
|
|
70
|
+
if (format === "short") {
|
|
71
|
+
label = UNITS[unit].short;
|
|
72
|
+
}
|
|
73
|
+
else {
|
|
74
|
+
if (value === 1) {
|
|
75
|
+
label = UNITS[unit].label;
|
|
76
|
+
}
|
|
77
|
+
else {
|
|
78
|
+
label = `${UNITS[unit].label}s`;
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
if (customFormatter) {
|
|
82
|
+
parts.push(customFormatter(unit, value, label));
|
|
83
|
+
}
|
|
84
|
+
else {
|
|
85
|
+
if (format === "short") {
|
|
86
|
+
parts.push(`${value}${label}`);
|
|
87
|
+
}
|
|
88
|
+
else {
|
|
89
|
+
parts.push(`${value} ${label}`);
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
if (format === "long" && parts.length > 1) {
|
|
95
|
+
const last = parts.pop();
|
|
96
|
+
return `${parts.join(", ")} and ${last}`;
|
|
97
|
+
}
|
|
98
|
+
return parts.join(format === "short" ? " " : ",");
|
|
99
|
+
};
|
package/dist/types/index.d.ts
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../index.ts"],"names":[],"mappings":"AAAA,cAAc,cAAc,CAAC"}
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../index.ts"],"names":[],"mappings":"AAAA,cAAc,cAAc,CAAC;AAC7B,cAAc,qBAAqB,CAAC"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"
|
|
1
|
+
{"version":3,"file":"logger.d.ts","sourceRoot":"","sources":["../../../lib/logger.ts"],"names":[],"mappings":"AAaA;;GAEG;AACH,qBAAa,MAAM;IAClB,OAAO,CAAC,QAAQ,CAAC,MAAM,CAKrB;IAEF,OAAO,CAAC,QAAQ,CAAC,UAAU,CAQzB;IAEF,OAAO,CAAC,YAAY;IAOpB,OAAO,CAAC,aAAa;IAYrB,OAAO,CAAC,aAAa;IAqBrB,OAAO,CAAC,GAAG;IAOX;;;;OAIG;IACI,KAAK,CAAC,CAAC,EAAE,OAAO;IAIvB;;;;OAIG;IACI,KAAK,CAAC,CAAC,EAAE,OAAO;IAIvB;;;;;;OAMG;IACI,KAAK,CAAC,CAAC,EAAE,OAAO,EAAE,CAAC,CAAC,EAAE,KAAK,EAAE,CAAC,CAAC,EAAE,OAAO;IAI/C;;;;OAIG;IACI,KAAK,CAAC,CAAC,EAAE,OAAO;IAIhB,OAAO,CAAC,IAAI,EAAE,MAAM,GAAG,IAAI;CAIlC"}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
type TimeUnitTypes = "y" | "mo" | "w" | "d" | "h" | "m" | "s" | "ms";
|
|
2
|
+
export declare const formatSeconds: (seconds: number, options?: {
|
|
3
|
+
includeZeroUnits?: boolean;
|
|
4
|
+
onlyUnits?: Array<TimeUnitTypes>;
|
|
5
|
+
format?: "long" | "short";
|
|
6
|
+
customFormatter?: (unit: TimeUnitTypes, value: number, label: string) => string;
|
|
7
|
+
}) => string;
|
|
8
|
+
export {};
|
|
9
|
+
//# sourceMappingURL=formatSeconds.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"formatSeconds.d.ts","sourceRoot":"","sources":["../../../lib/formatSeconds.ts"],"names":[],"mappings":"AAAA,KAAK,aAAa,GAAG,GAAG,GAAG,IAAI,GAAG,GAAG,GAAG,GAAG,GAAG,GAAG,GAAG,GAAG,GAAG,GAAG,GAAG,IAAI,CAAC;AA2BrE,eAAO,MAAM,aAAa,GACzB,SAAS,MAAM,EACf,UAAS;IACR,gBAAgB,CAAC,EAAE,OAAO,CAAC;IAC3B,SAAS,CAAC,EAAE,KAAK,CAAC,aAAa,CAAC,CAAC;IACjC,MAAM,CAAC,EAAE,MAAM,GAAG,OAAO,CAAC;IAC1B,eAAe,CAAC,EAAE,CACjB,IAAI,EAAE,aAAa,EACnB,KAAK,EAAE,MAAM,EACb,KAAK,EAAE,MAAM,KACT,MAAM,CAAC;CACP,KACJ,MA+FF,CAAC"}
|