utilium 3.9.1 → 3.11.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,3 +1,5 @@
1
+ /** Describes how far `date` is from `relativeTo`, e.g. `3 hours ago` or `in 2 days`. */
2
+ export declare function relativeToDate(date: Date | number, relativeTo?: Date | number): string;
1
3
  export declare function dateRange(date: Date): string;
2
4
  export declare function bytes(bytes: bigint | number): string;
3
5
  export declare function ms(time: number): string;
package/dist/format.js CHANGED
@@ -6,13 +6,17 @@ const rangeUnits = [
6
6
  ['hour', 3600_000],
7
7
  ['minute', 60_000],
8
8
  ];
9
- export function dateRange(date) {
10
- const offset = date.getTime() - Date.now();
9
+ /** Describes how far `date` is from `relativeTo`, e.g. `3 hours ago` or `in 2 days`. */
10
+ export function relativeToDate(date, relativeTo = Date.now()) {
11
+ const offset = Number(date) - Number(relativeTo);
11
12
  const distance = Math.abs(offset);
12
13
  const [unit, length] = rangeUnits.find(([, length]) => distance >= length) ?? rangeUnits.at(-1);
13
14
  const count = Math.round(distance / length);
14
15
  const text = `${count} ${unit}${count == 1 ? '' : 's'}`;
15
- return `${date.toLocaleString()} (${offset >= 0 ? `in ${text}` : `${text} ago`})`;
16
+ return offset >= 0 ? `in ${text}` : `${text} ago`;
17
+ }
18
+ export function dateRange(date) {
19
+ return `${date.toLocaleString()} (${relativeToDate(date)})`;
16
20
  }
17
21
  export function bytes(bytes) {
18
22
  if (typeof bytes == 'number') {
package/dist/fs.d.ts CHANGED
@@ -23,6 +23,11 @@ export interface JSONFileMapOptions {
23
23
  * Should an invalid JSON file be overwritten
24
24
  */
25
25
  overwrite_invalid: boolean;
26
+ /**
27
+ * When set, format the JSON nicely.
28
+ * @default false
29
+ */
30
+ pretty?: boolean;
26
31
  }
27
32
  /**
28
33
  * A Map overlaying a JSON file
package/dist/fs.js CHANGED
@@ -75,7 +75,7 @@ export class JSONFileMap extends FileMap {
75
75
  if (!fs.existsSync(this.path)) {
76
76
  fs.writeFileSync(this.path, '{}');
77
77
  }
78
- const content = JSON.stringify(Object.fromEntries(map));
78
+ const content = JSON.stringify(Object.fromEntries(map), null, this.options.pretty ? '\t' : undefined);
79
79
  fs.writeFileSync(this.path, content);
80
80
  }
81
81
  clear() {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "utilium",
3
- "version": "3.9.1",
3
+ "version": "3.11.0",
4
4
  "description": "Typescript utilities",
5
5
  "funding": {
6
6
  "type": "individual",