@dorsk/tsumikit 0.15.0 → 0.16.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.
@@ -32,6 +32,7 @@
32
32
  value,
33
33
  mode = 'datetime',
34
34
  utc = false,
35
+ precision = false,
35
36
  details = true,
36
37
  selectable = false,
37
38
  mono = false,
@@ -49,6 +50,9 @@
49
50
  * (zoneless). Handy for calendar-date fields, where a local midnight-UTC
50
51
  * value would otherwise show the wrong day. */
51
52
  utc?: boolean;
53
+ /** Relative mode only: render two units ("1d 15h", "1h 55m") instead of a
54
+ * single rounded unit. Default false. */
55
+ precision?: boolean;
52
56
  /** Click to open the details popover (UTC / local / relative / zone /
53
57
  * epoch). Default true. When false, renders as a bare inline <time>. */
54
58
  details?: boolean;
@@ -83,7 +87,7 @@
83
87
  });
84
88
 
85
89
  const date = $derived(toDate(value));
86
- const label = $derived(formatTimestamp(value, current, now, utc));
90
+ const label = $derived(formatTimestamp(value, current, now, utc, precision));
87
91
  // datetime= wants a valid ISO string; omit it entirely on bad input.
88
92
  const machine = $derived(date ? toISO(date) : undefined);
89
93
  const showPopover = $derived(details || selectable);
@@ -105,7 +109,7 @@
105
109
  ? [
106
110
  { k: 'UTC', v: toISO(date) },
107
111
  { k: 'Local', v: toLocale(date, 'datetime') },
108
- { k: 'Relative', v: relativeTime(date, now) },
112
+ { k: 'Relative', v: relativeTime(date, now, true) },
109
113
  { k: 'Time zone', v: localTimeZone() },
110
114
  { k: 'Unix', v: String(toEpochSeconds(date)) }
111
115
  ]
@@ -12,6 +12,9 @@ type $$ComponentProps = {
12
12
  * (zoneless). Handy for calendar-date fields, where a local midnight-UTC
13
13
  * value would otherwise show the wrong day. */
14
14
  utc?: boolean;
15
+ /** Relative mode only: render two units ("1d 15h", "1h 55m") instead of a
16
+ * single rounded unit. Default false. */
17
+ precision?: boolean;
15
18
  /** Click to open the details popover (UTC / local / relative / zone /
16
19
  * epoch). Default true. When false, renders as a bare inline <time>. */
17
20
  details?: boolean;
@@ -28,11 +28,17 @@ export declare function localTimeZone(): string;
28
28
  * "3m ago", "2h ago", "5d ago" — past-relative, coarsening as it ages and
29
29
  * falling back to a locale date once past ~30 days. Future instants read
30
30
  * "in 3m" etc. `now` is injectable so callers (and tests) control the clock.
31
+ *
32
+ * The single-unit form **rounds to the nearest** unit (not truncating), so an
33
+ * instant 1d 15h away reads "in 2d" rather than the misleading "in 1d", and the
34
+ * rounding carries across boundaries (23h40m → "1d"). Pass `precision` for the
35
+ * two-unit form — "1d 15h", "1h 55m" — which keeps the coarse unit exact and
36
+ * shows the remainder instead of rounding it away.
31
37
  */
32
- export declare function relativeTime(value: TimeInput | null | undefined, now?: number): string;
38
+ export declare function relativeTime(value: TimeInput | null | undefined, now?: number, precision?: boolean): string;
33
39
  /**
34
40
  * Render a date in the chosen inline mode. `utc` selects UTC over the viewer's
35
41
  * zone for the date/time/datetime modes (ignored by `iso`, always UTC, and
36
42
  * `relative`, zoneless). Returns '' for unparseable input.
37
43
  */
38
- export declare function formatTimestamp(value: TimeInput | null | undefined, mode: TimestampMode, now?: number, utc?: boolean): string;
44
+ export declare function formatTimestamp(value: TimeInput | null | undefined, mode: TimestampMode, now?: number, utc?: boolean, precision?: boolean): string;
package/dist/timestamp.js CHANGED
@@ -49,12 +49,21 @@ export function toEpochSeconds(d) {
49
49
  export function localTimeZone() {
50
50
  return Intl.DateTimeFormat().resolvedOptions().timeZone;
51
51
  }
52
+ const MIN = 60;
53
+ const HOUR = 60 * MIN;
54
+ const DAY = 24 * HOUR;
52
55
  /**
53
56
  * "3m ago", "2h ago", "5d ago" — past-relative, coarsening as it ages and
54
57
  * falling back to a locale date once past ~30 days. Future instants read
55
58
  * "in 3m" etc. `now` is injectable so callers (and tests) control the clock.
59
+ *
60
+ * The single-unit form **rounds to the nearest** unit (not truncating), so an
61
+ * instant 1d 15h away reads "in 2d" rather than the misleading "in 1d", and the
62
+ * rounding carries across boundaries (23h40m → "1d"). Pass `precision` for the
63
+ * two-unit form — "1d 15h", "1h 55m" — which keeps the coarse unit exact and
64
+ * shows the remainder instead of rounding it away.
56
65
  */
57
- export function relativeTime(value, now = Date.now()) {
66
+ export function relativeTime(value, now = Date.now(), precision = false) {
58
67
  const d = toDate(value);
59
68
  if (!d)
60
69
  return '';
@@ -62,15 +71,32 @@ export function relativeTime(value, now = Date.now()) {
62
71
  const future = deltaMs < 0;
63
72
  const secs = Math.floor(Math.abs(deltaMs) / 1000);
64
73
  const suffix = (s) => (future ? `in ${s}` : `${s} ago`);
65
- if (secs < 60)
74
+ if (secs < MIN)
66
75
  return suffix(`${secs}s`);
67
- const mins = Math.floor(secs / 60);
76
+ if (secs >= 30 * DAY)
77
+ return d.toLocaleDateString();
78
+ if (precision) {
79
+ if (secs < HOUR) {
80
+ const m = Math.floor(secs / MIN);
81
+ const s = secs % MIN;
82
+ return suffix(s ? `${m}m ${s}s` : `${m}m`);
83
+ }
84
+ if (secs < DAY) {
85
+ const h = Math.floor(secs / HOUR);
86
+ const m = Math.floor((secs % HOUR) / MIN);
87
+ return suffix(m ? `${h}h ${m}m` : `${h}h`);
88
+ }
89
+ const days = Math.floor(secs / DAY);
90
+ const h = Math.floor((secs % DAY) / HOUR);
91
+ return suffix(h ? `${days}d ${h}h` : `${days}d`);
92
+ }
93
+ const mins = Math.round(secs / MIN);
68
94
  if (mins < 60)
69
95
  return suffix(`${mins}m`);
70
- const hrs = Math.floor(mins / 60);
96
+ const hrs = Math.round(secs / HOUR);
71
97
  if (hrs < 24)
72
98
  return suffix(`${hrs}h`);
73
- const days = Math.floor(hrs / 24);
99
+ const days = Math.round(secs / DAY);
74
100
  if (days < 30)
75
101
  return suffix(`${days}d`);
76
102
  return d.toLocaleDateString();
@@ -80,7 +106,7 @@ export function relativeTime(value, now = Date.now()) {
80
106
  * zone for the date/time/datetime modes (ignored by `iso`, always UTC, and
81
107
  * `relative`, zoneless). Returns '' for unparseable input.
82
108
  */
83
- export function formatTimestamp(value, mode, now, utc = false) {
109
+ export function formatTimestamp(value, mode, now, utc = false, precision = false) {
84
110
  const d = toDate(value);
85
111
  if (!d)
86
112
  return '';
@@ -92,6 +118,6 @@ export function formatTimestamp(value, mode, now, utc = false) {
92
118
  case 'datetime':
93
119
  return toLocale(d, mode, utc);
94
120
  case 'relative':
95
- return relativeTime(d, now);
121
+ return relativeTime(d, now, precision);
96
122
  }
97
123
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dorsk/tsumikit",
3
- "version": "0.15.0",
3
+ "version": "0.16.0",
4
4
  "description": "Minimal, dependency-free Svelte 5 + pure-CSS UI kit. Token-driven atoms, molecules & layouts with theming out of the box.",
5
5
  "type": "module",
6
6
  "license": "MIT",