utilium 3.9.1 → 3.10.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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "utilium",
3
- "version": "3.9.1",
3
+ "version": "3.10.0",
4
4
  "description": "Typescript utilities",
5
5
  "funding": {
6
6
  "type": "individual",