utilium 3.9.0 → 3.9.1

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.
Files changed (2) hide show
  1. package/dist/format.js +12 -5
  2. package/package.json +1 -1
package/dist/format.js CHANGED
@@ -1,11 +1,18 @@
1
1
  // SPDX-License-Identifier: LGPL-3.0-or-later
2
2
  // Copyright (c) 2026 James Prevett
3
+ /** Lengths are in milliseconds */
4
+ const rangeUnits = [
5
+ ['day', 24 * 3600_000],
6
+ ['hour', 3600_000],
7
+ ['minute', 60_000],
8
+ ];
3
9
  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`})`;
10
+ const offset = date.getTime() - Date.now();
11
+ const distance = Math.abs(offset);
12
+ const [unit, length] = rangeUnits.find(([, length]) => distance >= length) ?? rangeUnits.at(-1);
13
+ const count = Math.round(distance / length);
14
+ const text = `${count} ${unit}${count == 1 ? '' : 's'}`;
15
+ return `${date.toLocaleString()} (${offset >= 0 ? `in ${text}` : `${text} ago`})`;
9
16
  }
10
17
  export function bytes(bytes) {
11
18
  if (typeof bytes == 'number') {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "utilium",
3
- "version": "3.9.0",
3
+ "version": "3.9.1",
4
4
  "description": "Typescript utilities",
5
5
  "funding": {
6
6
  "type": "individual",