utilium 3.7.0 → 3.8.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.
@@ -0,0 +1,5 @@
1
+ export declare function dateRange(date: Date): string;
2
+ export declare function bytes(bytes: bigint | number): string;
3
+ export declare function ms(time: number): string;
4
+ /** Formats a short duration into colon-separated values */
5
+ export declare function duration(seconds: number): string;
package/dist/format.js ADDED
@@ -0,0 +1,33 @@
1
+ // SPDX-License-Identifier: LGPL-3.0-or-later
2
+ // Copyright (c) 2026 James Prevett
3
+ export function dateRange(date) {
4
+ const rawDays = (date.getTime() - Date.now()) / (24 * 3600_000);
5
+ const daysCount = Math.abs(rawDays);
6
+ const daysText = Math.round(daysCount);
7
+ const plural = daysCount == 1 ? '' : 's';
8
+ return `${date.toLocaleString()} (${rawDays >= 0 ? `in ${daysText} day${plural}` : `${daysText} day${plural} ago`})`;
9
+ }
10
+ export function bytes(bytes) {
11
+ if (typeof bytes == 'number') {
12
+ bytes = BigInt(bytes);
13
+ }
14
+ const units = ['B', 'KB', 'MB', 'GB', 'TB'];
15
+ const i = bytes == 0n ? 0 : Math.floor((bytes.toString(10).length - 1) / 3);
16
+ const value = bytes == 0n ? 0 : Number(bytes) / Math.pow(1000, i);
17
+ return `${Number.isInteger(value) ? value : value.toFixed(2)} ${units[i]}`;
18
+ }
19
+ export function ms(time) {
20
+ return time > 5000 ? (time / 1000).toFixed(2) + 's' : time + 'ms';
21
+ }
22
+ /** Formats a short duration into colon-separated values */
23
+ export function duration(seconds) {
24
+ if (!Number.isFinite(seconds))
25
+ return '--:--';
26
+ seconds = Math.floor(seconds);
27
+ let minutes = Math.floor(seconds / 60);
28
+ seconds %= 60;
29
+ const hours = Math.floor(minutes / 60);
30
+ minutes %= 60;
31
+ const secondsStr = ':' + seconds.toString().padStart(2, '0');
32
+ return hours ? `${hours}:${minutes.toString().padStart(2, '0')}${secondsStr}` : minutes + secondsStr;
33
+ }
package/dist/index.d.ts CHANGED
@@ -4,6 +4,7 @@ export * as cache from './cache.js';
4
4
  export * from './checksum.js';
5
5
  export * from './color.js';
6
6
  export * as diff from './diff.js';
7
+ export * as format from './format.js';
7
8
  export * from './list.js';
8
9
  export * from './misc.js';
9
10
  export * from './numbers.js';
package/dist/index.js CHANGED
@@ -7,6 +7,7 @@ export * as cache from './cache.js';
7
7
  export * from './checksum.js';
8
8
  export * from './color.js';
9
9
  export * as diff from './diff.js';
10
+ export * as format from './format.js';
10
11
  export * from './list.js';
11
12
  export * from './misc.js';
12
13
  export * from './numbers.js';
package/dist/objects.d.ts CHANGED
@@ -11,7 +11,7 @@ export type Omit<T, K extends PropertyKey> = {
11
11
  };
12
12
  export declare function omit<T extends object, K extends keyof T>(object: T, ...keys: readonly K[]): Omit<T, K>;
13
13
  export declare function omit<T extends object, K extends keyof T>(object: T, ...keys: readonly (readonly K[])[]): Omit<T, K>;
14
- export declare function assignWithDefaults<To extends Record<keyof any, any>, From extends Partial<To>>(to: To, from: From, defaults?: Partial<To>): void;
14
+ export declare function assignWithDefaults<To extends Record<keyof From, any>, From extends Record<string, any>>(to: To, from: From, defaults?: Partial<From>): void;
15
15
  /**
16
16
  * Returns whether `value` is not a primitive.
17
17
  *
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "utilium",
3
- "version": "3.7.0",
3
+ "version": "3.8.0",
4
4
  "description": "Typescript utilities",
5
5
  "funding": {
6
6
  "type": "individual",
@@ -68,5 +68,6 @@
68
68
  },
69
69
  "allowScripts": {
70
70
  "esbuild": true
71
- }
71
+ },
72
+ "sideEffects": false
72
73
  }