@kyvrixon/utils 0.0.1 → 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/README.md CHANGED
@@ -1 +1,7 @@
1
- General utility files I use for alot of projects! Does not include any larger scale items, just the little things
1
+ # Utils
2
+
3
+ General utility files I use for alot of projects! Does not include any larger scale items, just the little things
4
+
5
+ ```bash
6
+ bun install @kyvrixon/utils@latest
7
+ ```
package/dist/index.js CHANGED
@@ -1 +1,2 @@
1
- export * from "./lib/Logger";
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
+ };
@@ -1,2 +1,3 @@
1
- export * from "./lib/Logger";
1
+ export * from "./lib/logger";
2
+ export * from "./lib/formatSeconds";
2
3
  //# sourceMappingURL=index.d.ts.map
@@ -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"}
@@ -36,4 +36,4 @@ export declare class Logger {
36
36
  debug(m: unknown): void;
37
37
  divider(text: string): void;
38
38
  }
39
- //# sourceMappingURL=Logger.d.ts.map
39
+ //# sourceMappingURL=logger.d.ts.map
@@ -1 +1 @@
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"}
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"}
package/package.json CHANGED
@@ -1,13 +1,14 @@
1
1
  {
2
2
  "name": "@kyvrixon/utils",
3
- "module": "index.js",
4
- "version": "0.0.1",
3
+ "main": "./dist/index.js",
4
+ "version": "0.0.3",
5
5
  "type": "module",
6
6
  "private": false,
7
7
  "files": [
8
8
  "dist"
9
9
  ],
10
10
  "packageManager": "bun@1.3.6",
11
+ "types": "./dist/types/index.d.ts",
11
12
  "devDependencies": {
12
13
  "@types/bun": "1.3.6"
13
14
  },
@@ -16,5 +17,11 @@
16
17
  },
17
18
  "dependencies": {
18
19
  "chalk": "^5.6.2"
20
+ },
21
+ "exports": {
22
+ ".": {
23
+ "types": "./dist/types/index.d.ts",
24
+ "import": "./dist/index.js"
25
+ }
19
26
  }
20
27
  }