utilium 3.12.0 → 3.13.0

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/format.d.ts CHANGED
@@ -1,5 +1,11 @@
1
1
  /** Describes how far `date` is from `relativeTo`, e.g. `3 hours ago` or `in 2 days`. */
2
2
  export declare function relativeToDate(date: Date | number, relativeTo?: Date | number): string;
3
+ /**
4
+ * Describes a length of time using its largest units, e.g. `1 hour 5 minutes`.
5
+ * @param milliseconds The length of time
6
+ * @param precision How many units to use at most
7
+ */
8
+ export declare function timeSpan(milliseconds: number, precision?: number): string;
3
9
  export declare function dateRange(date: Date): string;
4
10
  export declare function bytes(bytes: bigint | number): string;
5
11
  export declare function ms(time: number): string;
package/dist/format.js CHANGED
@@ -15,6 +15,27 @@ export function relativeToDate(date, relativeTo = Date.now()) {
15
15
  const text = `${count} ${unit}${count == 1 ? '' : 's'}`;
16
16
  return offset >= 0 ? `in ${text}` : `${text} ago`;
17
17
  }
18
+ /** Lengths are in milliseconds */
19
+ const spanUnits = [...rangeUnits, ['second', 1000]];
20
+ /**
21
+ * Describes a length of time using its largest units, e.g. `1 hour 5 minutes`.
22
+ * @param milliseconds The length of time
23
+ * @param precision How many units to use at most
24
+ */
25
+ export function timeSpan(milliseconds, precision = 2) {
26
+ const distance = Math.abs(milliseconds);
27
+ const largest = Math.max(spanUnits.findIndex(([, length]) => distance >= length), 0);
28
+ const smallest = Math.min(largest + precision - 1, spanUnits.length - 1);
29
+ let rest = Math.round(distance / spanUnits[smallest][1]) * spanUnits[smallest][1];
30
+ const parts = [];
31
+ for (const [unit, length] of spanUnits.slice(largest, smallest + 1)) {
32
+ const count = Math.floor(rest / length);
33
+ rest -= count * length;
34
+ if (count)
35
+ parts.push(`${count} ${unit}${count == 1 ? '' : 's'}`);
36
+ }
37
+ return parts.join(' ') || '0 seconds';
38
+ }
18
39
  export function dateRange(date) {
19
40
  return `${date.toLocaleString()} (${relativeToDate(date)})`;
20
41
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "utilium",
3
- "version": "3.12.0",
3
+ "version": "3.13.0",
4
4
  "description": "Typescript utilities",
5
5
  "funding": {
6
6
  "type": "individual",
@@ -26,7 +26,7 @@
26
26
  "format:check": "prettier --check .",
27
27
  "format": "prettier --write .",
28
28
  "lint": "tsc --noEmit && eslint src",
29
- "test": "tsx --test test/**/*.test.ts",
29
+ "test": "node --test test/**/*.test.ts",
30
30
  "build": "tsc -b",
31
31
  "prepublishOnly": "npx tsc"
32
32
  },
@@ -46,7 +46,6 @@
46
46
  "eslint": "^10.1.0",
47
47
  "globals": "^17.8.0",
48
48
  "prettier": "^3.2.5",
49
- "tsx": "^4.19.1",
50
49
  "typedoc": "^0.28.18",
51
50
  "typescript": "^6.0.0",
52
51
  "typescript-eslint": "^8.58.0"