basalt-ui 2.13.3 → 2.15.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/CHANGELOG.md +66 -0
- package/MIGRATING.md +42 -12
- package/agent/agents/basalt-design-reviewer.md +83 -0
- package/agent/rules/basalt-batteries.md +1 -2
- package/agent/rules/basalt-charts.md +1 -1
- package/agent/rules/basalt-controls.md +3 -3
- package/agent/rules/basalt-mantine.md +1 -2
- package/agent/rules/basalt-state.md +1 -2
- package/agent/rules/basalt-tokens.md +1 -3
- package/agent/skills/basalt-design/SKILL.md +26 -8
- package/agent/templates/AGENTS-block.md.tpl +7 -0
- package/configs/oxlint-plugin.js +39 -2
- package/configs/oxlint-plugin.test.ts +48 -0
- package/dist/agent/chat/thread-feed-row.d.ts.map +1 -1
- package/dist/agent/chat/thread-feed-row.js +2 -2
- package/dist/agent/chat/thread-message.d.ts.map +1 -1
- package/dist/agent/chat/thread-message.js +2 -2
- package/dist/agent/chat/thread-outcome-card.js +2 -2
- package/dist/charts/DualPanel.d.ts.map +1 -1
- package/dist/charts/DualPanel.js +1 -0
- package/dist/charts/compile.d.ts.map +1 -1
- package/dist/charts/compile.js +3 -2
- package/dist/charts/cursor.d.ts +10 -6
- package/dist/charts/cursor.d.ts.map +1 -1
- package/dist/charts/cursor.js +20 -13
- package/dist/charts/legend.d.ts +7 -6
- package/dist/charts/legend.d.ts.map +1 -1
- package/dist/charts/legend.js +3 -3
- package/dist/charts/panel.d.ts +2 -0
- package/dist/charts/panel.d.ts.map +1 -1
- package/dist/charts/panel.js +1 -0
- package/dist/cli/board-freshness.d.ts +25 -0
- package/dist/cli/board-freshness.d.ts.map +1 -0
- package/dist/cli/board-freshness.js +88 -0
- package/dist/cli/doctor.d.ts +2 -1
- package/dist/cli/doctor.d.ts.map +1 -1
- package/dist/cli/doctor.js +3 -1
- package/dist/cli/help.d.ts.map +1 -1
- package/dist/cli/help.js +9 -3
- package/dist/cli/index.d.ts +15 -1
- package/dist/cli/index.d.ts.map +1 -1
- package/dist/cli/index.js +57 -7
- package/dist/cli/init.d.ts.map +1 -1
- package/dist/cli/init.js +6 -1
- package/dist/cli/shot/board.d.ts +5 -0
- package/dist/cli/shot/board.d.ts.map +1 -0
- package/dist/cli/shot/board.js +96 -0
- package/dist/cli/shot/capture.d.ts +59 -0
- package/dist/cli/shot/capture.d.ts.map +1 -0
- package/dist/cli/shot/capture.js +473 -0
- package/dist/cli/shot/index.d.ts +45 -0
- package/dist/cli/shot/index.d.ts.map +1 -0
- package/dist/cli/shot/index.js +289 -0
- package/dist/cli/shot/measure.d.ts +28 -0
- package/dist/cli/shot/measure.d.ts.map +1 -0
- package/dist/cli/shot/measure.js +176 -0
- package/dist/cli/shot/playwright.d.ts +101 -0
- package/dist/cli/shot/playwright.d.ts.map +1 -0
- package/dist/cli/shot/playwright.js +60 -0
- package/dist/cli/shot/serve.d.ts +6 -0
- package/dist/cli/shot/serve.d.ts.map +1 -0
- package/dist/cli/shot/serve.js +33 -0
- package/dist/cli/shot/types.d.ts +97 -0
- package/dist/cli/shot/types.d.ts.map +1 -0
- package/dist/cli/shot/types.js +17 -0
- package/dist/cli/sync.d.ts.map +1 -1
- package/dist/cli/sync.js +2 -1
- package/dist/commands/notifications/center.d.ts.map +1 -1
- package/dist/commands/notifications/center.js +2 -11
- package/dist/controls/control-name.d.ts +27 -0
- package/dist/controls/control-name.d.ts.map +1 -0
- package/dist/controls/control-name.js +11 -0
- package/dist/controls/enum-filter.d.ts +3 -6
- package/dist/controls/enum-filter.d.ts.map +1 -1
- package/dist/controls/enum-filter.js +8 -4
- package/dist/controls/filter-base.d.ts +30 -0
- package/dist/controls/filter-base.d.ts.map +1 -0
- package/dist/controls/filter-base.js +0 -0
- package/dist/controls/filter-pill.d.ts +3 -1
- package/dist/controls/filter-pill.d.ts.map +1 -1
- package/dist/controls/filter-pill.js +2 -0
- package/dist/controls/filter-sheet.d.ts +0 -32
- package/dist/controls/filter-sheet.d.ts.map +1 -1
- package/dist/controls/filter-sheet.js +1 -28
- package/dist/controls/glyphs.d.ts +12 -0
- package/dist/controls/glyphs.d.ts.map +1 -1
- package/dist/controls/glyphs.js +23 -2
- package/dist/controls/index.d.ts +2 -1
- package/dist/controls/index.d.ts.map +1 -1
- package/dist/controls/multi-select-filter.d.ts +8 -3
- package/dist/controls/multi-select-filter.d.ts.map +1 -1
- package/dist/controls/multi-select-filter.js +27 -10
- package/dist/controls/number-filter.d.ts +3 -12
- package/dist/controls/number-filter.d.ts.map +1 -1
- package/dist/controls/number-filter.js +17 -6
- package/dist/controls/panel-row.d.ts +5 -8
- package/dist/controls/panel-row.d.ts.map +1 -1
- package/dist/controls/panel-row.js +3 -0
- package/dist/controls/range-filter.d.ts +14 -14
- package/dist/controls/range-filter.d.ts.map +1 -1
- package/dist/controls/range-filter.js +18 -41
- package/dist/controls/search-filter.d.ts +5 -2
- package/dist/controls/search-filter.d.ts.map +1 -1
- package/dist/controls/search-filter.js +74 -40
- package/dist/controls/select-filter.d.ts +2 -15
- package/dist/controls/select-filter.d.ts.map +1 -1
- package/dist/controls/sync-button.d.ts +0 -6
- package/dist/controls/sync-button.d.ts.map +1 -1
- package/dist/controls/sync-button.js +6 -36
- package/dist/controls/toggle-filter.d.ts +2 -2
- package/dist/controls/toggle-filter.d.ts.map +1 -1
- package/dist/controls/toggle-filter.js +3 -2
- package/dist/controls/view-tabs.d.ts +3 -4
- package/dist/controls/view-tabs.d.ts.map +1 -1
- package/dist/controls/view-tabs.js +24 -5
- package/dist/format/date.d.ts +43 -10
- package/dist/format/date.d.ts.map +1 -1
- package/dist/format/date.js +109 -18
- package/dist/format/index.d.ts +12 -11
- package/dist/format/index.d.ts.map +1 -1
- package/dist/format/index.js +11 -2
- package/dist/format/measure.d.ts +9 -9
- package/dist/format/measure.d.ts.map +1 -1
- package/dist/format/measure.js +19 -8
- package/dist/format/number.d.ts +35 -27
- package/dist/format/number.d.ts.map +1 -1
- package/dist/format/number.js +41 -20
- package/dist/format/shared.d.ts +38 -7
- package/dist/format/shared.d.ts.map +1 -1
- package/dist/format/shared.js +61 -13
- package/dist/provider/index.d.ts +13 -1
- package/dist/provider/index.d.ts.map +1 -1
- package/dist/provider/index.js +8 -0
- package/dist/surfaces.d.ts +2 -0
- package/dist/surfaces.d.ts.map +1 -1
- package/dist/surfaces.js +2 -0
- package/llms.txt +35 -14
- package/package.json +1 -1
- package/src/agent/chat/thread-feed-row.tsx +2 -2
- package/src/agent/chat/thread-message.tsx +2 -2
- package/src/agent/chat/thread-outcome-card.tsx +2 -2
- package/src/charts/DualPanel.tsx +1 -0
- package/src/charts/compile.ts +3 -2
- package/src/charts/cursor.ts +44 -27
- package/src/charts/legend.tsx +11 -8
- package/src/charts/panel.tsx +3 -0
- package/src/cli/board-freshness.ts +132 -0
- package/src/cli/doctor.ts +10 -1
- package/src/cli/help.ts +14 -2
- package/src/cli/index.ts +80 -10
- package/src/cli/init.ts +8 -1
- package/src/cli/shot/board.ts +154 -0
- package/src/cli/shot/capture.ts +650 -0
- package/src/cli/shot/index.ts +362 -0
- package/src/cli/shot/measure.ts +225 -0
- package/src/cli/shot/playwright.ts +152 -0
- package/src/cli/shot/serve.ts +35 -0
- package/src/cli/shot/types.ts +91 -0
- package/src/cli/sync.ts +2 -1
- package/src/commands/notifications/center.tsx +2 -14
- package/src/controls/control-name.ts +33 -0
- package/src/controls/enum-filter.tsx +29 -27
- package/src/controls/filter-base.ts +31 -0
- package/src/controls/filter-pill.tsx +4 -0
- package/src/controls/filter-sheet.tsx +0 -56
- package/src/controls/glyphs.tsx +45 -2
- package/src/controls/index.ts +2 -1
- package/src/controls/multi-select-filter.tsx +67 -43
- package/src/controls/number-filter.tsx +47 -47
- package/src/controls/panel-row.tsx +7 -8
- package/src/controls/range-filter.tsx +54 -78
- package/src/controls/search-filter.tsx +107 -54
- package/src/controls/select-filter.tsx +2 -18
- package/src/controls/sync-button.tsx +7 -40
- package/src/controls/toggle-filter.tsx +9 -7
- package/src/controls/view-tabs.tsx +41 -21
- package/src/format/date.ts +189 -36
- package/src/format/index.ts +12 -11
- package/src/format/measure.ts +29 -18
- package/src/format/number.ts +76 -37
- package/src/format/shared.ts +116 -25
- package/src/provider/index.tsx +23 -0
- package/src/surfaces.ts +5 -0
- package/dist/agent/chat/relative-time.d.ts +0 -27
- package/dist/agent/chat/relative-time.d.ts.map +0 -1
- package/dist/agent/chat/relative-time.js +0 -27
- package/src/agent/chat/relative-time.ts +0 -55
package/src/format/date.ts
CHANGED
|
@@ -1,11 +1,44 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* Date/time/duration formatting (`
|
|
2
|
+
* Date/time/duration formatting (`date`/`time`/`dateTime`/`weekday`/`relativeTime`/`duration`/`durationClock`),
|
|
3
3
|
* seeded from argo consumers (`features/{walking-pad,reading,astro-window,garmin-health}/{formatters,format,
|
|
4
4
|
* formulas}.ts` — read those files' own JSDoc for the exact call sites this generalizes).
|
|
5
5
|
*/
|
|
6
|
-
import { dateFormat, NON_FINITE, toDate } from './shared'
|
|
6
|
+
import { dateFormat, NON_FINITE, numberFormat, toDate } from './shared'
|
|
7
7
|
import type { LocaleOption } from './shared'
|
|
8
8
|
|
|
9
|
+
type DateInput = Date | number | string | null | undefined
|
|
10
|
+
|
|
11
|
+
const CALENDAR_DATE = /^(\d{4})-(\d{2})-(\d{2})$/
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* A plain `YYYY-MM-DD` is a calendar date, not an instant. Returns `undefined` when `value` is not
|
|
15
|
+
* one, `null` when it is shaped like one but names no real day (`2026-02-31`), else midnight of
|
|
16
|
+
* that day — in UTC or in the local zone. `setFullYear` (not the `Date` constructor) so years
|
|
17
|
+
* 0–99 are not remapped to 19xx; the round-trip check rejects the overflow `Date` would roll over.
|
|
18
|
+
*/
|
|
19
|
+
function calendarDate(value: string, utc: boolean): Date | null | undefined {
|
|
20
|
+
const match = CALENDAR_DATE.exec(value)
|
|
21
|
+
if (match === null) return undefined
|
|
22
|
+
const year = Number(match[1])
|
|
23
|
+
const month = Number(match[2]) - 1
|
|
24
|
+
const day = Number(match[3])
|
|
25
|
+
const result = new Date(0)
|
|
26
|
+
if (utc) {
|
|
27
|
+
result.setUTCFullYear(year, month, day)
|
|
28
|
+
result.setUTCHours(0, 0, 0, 0)
|
|
29
|
+
const ok =
|
|
30
|
+
result.getUTCFullYear() === year &&
|
|
31
|
+
result.getUTCMonth() === month &&
|
|
32
|
+
result.getUTCDate() === day
|
|
33
|
+
return ok ? result : null
|
|
34
|
+
}
|
|
35
|
+
result.setFullYear(year, month, day)
|
|
36
|
+
result.setHours(0, 0, 0, 0)
|
|
37
|
+
const ok =
|
|
38
|
+
result.getFullYear() === year && result.getMonth() === month && result.getDate() === day
|
|
39
|
+
return ok ? result : null
|
|
40
|
+
}
|
|
41
|
+
|
|
9
42
|
export type WeekdayOptions = LocaleOption & {
|
|
10
43
|
/** `Intl.DateTimeFormat`'s own weekday widths. Default `'short'` (`"Mon"`). */
|
|
11
44
|
format?: 'short' | 'long' | 'narrow'
|
|
@@ -13,26 +46,30 @@ export type WeekdayOptions = LocaleOption & {
|
|
|
13
46
|
|
|
14
47
|
/** Weekday name — seeded from `astro-window/formulas.ts#fmtWeekday`. A plain `YYYY-MM-DD` string
|
|
15
48
|
* is parsed as a LOCAL calendar date (never UTC), matching that seed's own rationale: the weekday
|
|
16
|
-
* must not shift a day depending on the reader's timezone. A non-finite/unparseable input
|
|
17
|
-
* {@link NON_FINITE}. */
|
|
49
|
+
* must not shift a day depending on the reader's timezone. A non-finite/unparseable input (or a
|
|
50
|
+
* day that does not exist, `2026-02-31`) prints {@link NON_FINITE}. */
|
|
18
51
|
export function weekday(value: Date | number | string, options: WeekdayOptions = {}): string {
|
|
19
|
-
const
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
const [y, m, d] = value.split('-').map(Number)
|
|
23
|
-
return new Date(y ?? 1970, (m ?? 1) - 1, d ?? 1)
|
|
24
|
-
})()
|
|
25
|
-
: toDate(value)
|
|
26
|
-
if (date === null) return NON_FINITE
|
|
52
|
+
const parsed = typeof value === 'string' ? calendarDate(value, false) : undefined
|
|
53
|
+
const resolved = parsed === undefined ? toDate(value) : parsed
|
|
54
|
+
if (resolved === null) return NON_FINITE
|
|
27
55
|
const { locale, format = 'short' } = options
|
|
28
|
-
return dateFormat(locale, { weekday: format }).format(
|
|
56
|
+
return dateFormat(locale, { weekday: format }).format(resolved)
|
|
29
57
|
}
|
|
30
58
|
|
|
31
59
|
export type RelativeTimeOptions = LocaleOption & {
|
|
60
|
+
/** `Intl.RelativeTimeFormat` width. Default `'long'` (`"3 hours ago"`); `'short'` is `"3 hr. ago"`. */
|
|
61
|
+
style?: 'long' | 'short' | 'narrow'
|
|
62
|
+
/** `'auto'` (default) uses idioms (`"yesterday"`, `"now"`); `'always'` keeps the number. */
|
|
63
|
+
numeric?: 'auto' | 'always'
|
|
32
64
|
/** The reference "now" instant, epoch ms. Default `Date.now()` — mainly for tests. */
|
|
33
65
|
now?: number
|
|
34
66
|
}
|
|
35
67
|
|
|
68
|
+
const MINUTE_MS = 60_000
|
|
69
|
+
|
|
70
|
+
/** Largest first. The unit is picked by magnitude; the count is rounded on the absolute value
|
|
71
|
+
* (so +/- are symmetric) and the unit is promoted only once the rounded count fills the next
|
|
72
|
+
* larger unit — 45 minutes stays `45 minutes`, 59.6 minutes becomes `1 hour`. */
|
|
36
73
|
const RELATIVE_TIME_UNITS: readonly {
|
|
37
74
|
readonly unit: Intl.RelativeTimeFormatUnit
|
|
38
75
|
readonly ms: number
|
|
@@ -42,42 +79,108 @@ const RELATIVE_TIME_UNITS: readonly {
|
|
|
42
79
|
{ unit: 'week', ms: 604_800_000 },
|
|
43
80
|
{ unit: 'day', ms: 86_400_000 },
|
|
44
81
|
{ unit: 'hour', ms: 3_600_000 },
|
|
45
|
-
{ unit: 'minute', ms:
|
|
82
|
+
{ unit: 'minute', ms: MINUTE_MS },
|
|
46
83
|
]
|
|
47
84
|
|
|
85
|
+
/** `format(-0, …)` reads "0 seconds ago"; `format(0, …)` reads "in 0 seconds". */
|
|
86
|
+
const NEGATIVE_ZERO = -0
|
|
87
|
+
|
|
48
88
|
const relativeTimeFormatterCache = new Map<string, Intl.RelativeTimeFormat>()
|
|
49
89
|
|
|
50
|
-
function relativeTimeFormat(
|
|
51
|
-
|
|
90
|
+
function relativeTimeFormat(
|
|
91
|
+
locale: string | undefined,
|
|
92
|
+
style: 'long' | 'short' | 'narrow',
|
|
93
|
+
numeric: 'auto' | 'always',
|
|
94
|
+
): Intl.RelativeTimeFormat {
|
|
95
|
+
const key = `${locale ?? ''}|${style}|${numeric}`
|
|
52
96
|
const cached = relativeTimeFormatterCache.get(key)
|
|
53
97
|
if (cached !== undefined) return cached
|
|
54
|
-
const created = new Intl.RelativeTimeFormat(locale, { numeric
|
|
98
|
+
const created = new Intl.RelativeTimeFormat(locale, { style, numeric })
|
|
55
99
|
relativeTimeFormatterCache.set(key, created)
|
|
56
100
|
return created
|
|
57
101
|
}
|
|
58
102
|
|
|
59
103
|
/**
|
|
60
|
-
*
|
|
61
|
-
* `
|
|
62
|
-
*
|
|
63
|
-
* `
|
|
64
|
-
*
|
|
65
|
-
*
|
|
66
|
-
* render-path code where one bad timestamp must not blank a whole list.
|
|
104
|
+
* THE relative-time formatter on `Intl.RelativeTimeFormat` — `"3 hours ago"`, `"in 2 days"`,
|
|
105
|
+
* `"now"`. Under a minute is `format(0, 'second')`, so "now" is the locale's own word. The count is
|
|
106
|
+
* rounded before the unit is chosen, so 59.98 minutes reads `"1 hour ago"`, never
|
|
107
|
+
* `"60 minutes ago"`. Accepts epoch ms, `Date` or an ISO string; `null`/`undefined`/unparseable
|
|
108
|
+
* prints {@link NON_FINITE} (`Intl` throws a `RangeError` on non-finite input, and this is
|
|
109
|
+
* render-path code where one bad timestamp must not blank a list).
|
|
67
110
|
*/
|
|
68
|
-
export function relativeTime(
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
)
|
|
72
|
-
const
|
|
73
|
-
const timestamp =
|
|
74
|
-
value instanceof Date ? value.getTime() : typeof value === 'number' ? value : Date.parse(value)
|
|
75
|
-
if (!Number.isFinite(timestamp)) return NON_FINITE
|
|
111
|
+
export function relativeTime(value: DateInput, options: RelativeTimeOptions = {}): string {
|
|
112
|
+
const { locale, style = 'long', numeric = 'auto', now = Date.now() } = options
|
|
113
|
+
const timestamp = value === null || value === undefined ? undefined : toDate(value)?.getTime()
|
|
114
|
+
if (timestamp === undefined) return NON_FINITE
|
|
115
|
+
const formatter = relativeTimeFormat(locale, style, numeric)
|
|
76
116
|
const diffMs = timestamp - now
|
|
77
|
-
const
|
|
78
|
-
|
|
79
|
-
const
|
|
80
|
-
|
|
117
|
+
const magnitude = Math.abs(diffMs)
|
|
118
|
+
const sign = diffMs < 0 ? -1 : 1
|
|
119
|
+
const index = RELATIVE_TIME_UNITS.findIndex(({ ms }) => magnitude >= ms)
|
|
120
|
+
const picked = RELATIVE_TIME_UNITS[index]
|
|
121
|
+
if (picked !== undefined) {
|
|
122
|
+
const larger = RELATIVE_TIME_UNITS[index - 1]
|
|
123
|
+
const unit =
|
|
124
|
+
larger !== undefined && Math.round(magnitude / picked.ms) * picked.ms >= larger.ms
|
|
125
|
+
? larger
|
|
126
|
+
: picked
|
|
127
|
+
return formatter.format(sign * Math.round(magnitude / unit.ms), unit.unit)
|
|
128
|
+
}
|
|
129
|
+
return formatter.format(diffMs < 0 ? NEGATIVE_ZERO : 0, 'second')
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
export type DateOptions = LocaleOption & {
|
|
133
|
+
/** `Intl.DateTimeFormat` `dateStyle`/`timeStyle` width. Default `'medium'`. */
|
|
134
|
+
format?: 'short' | 'medium' | 'long'
|
|
135
|
+
/** IANA zone. Default `undefined` — passed straight through to `Intl`. Ignored for a plain
|
|
136
|
+
* `YYYY-MM-DD` string, which is a calendar date and prints the same day in every zone. */
|
|
137
|
+
timeZone?: string
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
/** Resolves `value` to a `Date` plus the zone to format it in. A plain `YYYY-MM-DD` is pinned to
|
|
141
|
+
* UTC midnight and formatted in UTC; anything unrepresentable is `null`. */
|
|
142
|
+
function resolveDateInput(
|
|
143
|
+
value: DateInput,
|
|
144
|
+
timeZone: string | undefined,
|
|
145
|
+
): { date: Date; timeZone: string | undefined } | null {
|
|
146
|
+
if (value === null || value === undefined) return null
|
|
147
|
+
const calendar = typeof value === 'string' ? calendarDate(value, true) : undefined
|
|
148
|
+
if (calendar !== undefined) return calendar === null ? null : { date: calendar, timeZone: 'UTC' }
|
|
149
|
+
const parsed = toDate(value)
|
|
150
|
+
return parsed === null ? null : { date: parsed, timeZone }
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
/** Calendar date — `"Sep 2, 2026"`. `null`/`undefined`/unparseable prints {@link NON_FINITE}. */
|
|
154
|
+
export function date(value: DateInput, options: DateOptions = {}): string {
|
|
155
|
+
const resolved = resolveDateInput(value, options.timeZone)
|
|
156
|
+
if (resolved === null) return NON_FINITE
|
|
157
|
+
const { locale, format = 'medium' } = options
|
|
158
|
+
return dateFormat(locale, { dateStyle: format, timeZone: resolved.timeZone }).format(
|
|
159
|
+
resolved.date,
|
|
160
|
+
)
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
/** Time of day — `"3:04:05 PM"` (`format: 'short'`: `"3:04 PM"`). A plain `YYYY-MM-DD` string reads as midnight UTC. */
|
|
164
|
+
export function time(value: DateInput, options: DateOptions = {}): string {
|
|
165
|
+
const resolved = resolveDateInput(value, options.timeZone)
|
|
166
|
+
if (resolved === null) return NON_FINITE
|
|
167
|
+
const { locale, format = 'medium' } = options
|
|
168
|
+
return dateFormat(locale, { timeStyle: format, timeZone: resolved.timeZone }).format(
|
|
169
|
+
resolved.date,
|
|
170
|
+
)
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
/** Date and time — `"Sep 2, 2026, 3:04:05 PM"` (`format: 'short'`: `"9/2/26, 3:04 PM"`; the date/time
|
|
174
|
+
* separator is the runtime's). */
|
|
175
|
+
export function dateTime(value: DateInput, options: DateOptions = {}): string {
|
|
176
|
+
const resolved = resolveDateInput(value, options.timeZone)
|
|
177
|
+
if (resolved === null) return NON_FINITE
|
|
178
|
+
const { locale, format = 'medium' } = options
|
|
179
|
+
return dateFormat(locale, {
|
|
180
|
+
dateStyle: format,
|
|
181
|
+
timeStyle: format,
|
|
182
|
+
timeZone: resolved.timeZone,
|
|
183
|
+
}).format(resolved.date)
|
|
81
184
|
}
|
|
82
185
|
|
|
83
186
|
/** Clock-style duration — `"1:02:03"`, `"04:12"`. Seeded from `walking-pad/
|
|
@@ -92,3 +195,53 @@ export function durationClock(seconds: number): string {
|
|
|
92
195
|
const ss = String(s).padStart(2, '0')
|
|
93
196
|
return h > 0 ? `${h}:${mm}:${ss}` : `${mm}:${ss}`
|
|
94
197
|
}
|
|
198
|
+
|
|
199
|
+
export type DurationOptions = LocaleOption & {
|
|
200
|
+
/** `'clock'` (default) `"1:02:03"`; `'compact'` `"1h 02m 03s"`; `'long'` `"1 hour 2 minutes 3 seconds"`. */
|
|
201
|
+
style?: 'clock' | 'compact' | 'long'
|
|
202
|
+
/** Largest unit `'compact'`/`'long'` break down to. Default `'hour'` (90 000 s is `"25h"`). */
|
|
203
|
+
largest?: 'day' | 'hour' | 'minute'
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
const DURATION_UNITS = [
|
|
207
|
+
{ unit: 'day', short: 'd', seconds: 86_400 },
|
|
208
|
+
{ unit: 'hour', short: 'h', seconds: 3_600 },
|
|
209
|
+
{ unit: 'minute', short: 'm', seconds: 60 },
|
|
210
|
+
{ unit: 'second', short: 's', seconds: 1 },
|
|
211
|
+
] as const
|
|
212
|
+
|
|
213
|
+
/**
|
|
214
|
+
* THE duration formatter. `'clock'` is {@link durationClock}; `'compact'` and `'long'` trim leading
|
|
215
|
+
* and trailing zero units but keep a zero between two non-zero ones (`"1h 00m 03s"`); zero is
|
|
216
|
+
* `"0s"` / `"0 seconds"`. `'long'` labels through `Intl.NumberFormat` unit style (locale-aware) —
|
|
217
|
+
* `Intl.DurationFormat` is not in the pinned TS lib. `null`/`undefined`/non-finite prints
|
|
218
|
+
* {@link NON_FINITE}; negatives clamp to zero.
|
|
219
|
+
*/
|
|
220
|
+
export function duration(
|
|
221
|
+
seconds: number | null | undefined,
|
|
222
|
+
options: DurationOptions = {},
|
|
223
|
+
): string {
|
|
224
|
+
if (seconds === null || seconds === undefined || !Number.isFinite(seconds)) return NON_FINITE
|
|
225
|
+
const { style = 'clock', largest = 'hour', locale } = options
|
|
226
|
+
if (style === 'clock') return durationClock(seconds)
|
|
227
|
+
const total = Math.max(0, Math.round(seconds))
|
|
228
|
+
const shown = DURATION_UNITS.slice(DURATION_UNITS.findIndex(({ unit }) => unit === largest))
|
|
229
|
+
const parts = shown.map(({ unit, short, seconds: size }, i) => {
|
|
230
|
+
const count = Math.floor(total / size)
|
|
231
|
+
const upper = shown[i - 1]?.seconds
|
|
232
|
+
return { unit, short, value: upper === undefined ? count : count % (upper / size) }
|
|
233
|
+
})
|
|
234
|
+
const firstNonZero = parts.findIndex(({ value }) => value > 0)
|
|
235
|
+
const start = firstNonZero === -1 ? parts.length - 1 : firstNonZero
|
|
236
|
+
const end = firstNonZero === -1 ? start : parts.findLastIndex(({ value }) => value > 0)
|
|
237
|
+
return parts
|
|
238
|
+
.slice(start, end + 1)
|
|
239
|
+
.map(({ unit, short, value }, i) => {
|
|
240
|
+
if (style === 'long') {
|
|
241
|
+
return numberFormat(locale, { style: 'unit', unit, unitDisplay: 'long' }).format(value)
|
|
242
|
+
}
|
|
243
|
+
const digits = i === 0 ? String(value) : String(value).padStart(2, '0')
|
|
244
|
+
return `${digits}${short}`
|
|
245
|
+
})
|
|
246
|
+
.join(' ')
|
|
247
|
+
}
|
package/src/format/index.ts
CHANGED
|
@@ -1,23 +1,24 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* `basalt-ui/format` — Mantine-free, React-free `Intl`-backed formatting
|
|
3
|
-
*
|
|
4
|
-
* (`km`/`kcal`)
|
|
5
|
-
* lines across four duration spellings) and the 1.x chart layer's own formatters (2.0's charts
|
|
6
|
-
* import the internals directly).
|
|
2
|
+
* `basalt-ui/format` — Mantine-free, React-free `Intl`-backed formatting: numbers, percentages,
|
|
3
|
+
* money, bytes and signed deltas, dates and durations, and the two measurement units argo needed
|
|
4
|
+
* (`km`/`kcal`).
|
|
7
5
|
*
|
|
8
|
-
* Every formatter takes `(value, opts?)
|
|
9
|
-
*
|
|
10
|
-
*
|
|
6
|
+
* Every formatter takes `(value, opts?)`. `locale` defaults to what `defineFormat` (or
|
|
7
|
+
* the provider's locale and time zone props) set, else the runtime's locale. That default is
|
|
8
|
+
* ONE value per process; pass `locale` per call to override it. Number formatters accept
|
|
9
|
+
* `null | undefined`, and every non-finite/unparseable input prints the same {@link NON_FINITE}
|
|
10
|
+
* em dash rather than `"NaN"` or throwing.
|
|
11
11
|
*
|
|
12
12
|
* @example
|
|
13
13
|
* import { money, percent, durationClock, weekday, relativeTime } from 'basalt-ui/format'
|
|
14
14
|
*
|
|
15
|
-
* money(1234, { currency: 'USD' }) // "$1,234"
|
|
15
|
+
* money(1234, { currency: 'USD' }) // "$1,234.00"
|
|
16
16
|
* percent(0.42) // "42%"
|
|
17
17
|
* durationClock(3_723) // "1:02:03"
|
|
18
18
|
* weekday('2026-09-02') // "Wed"
|
|
19
19
|
* relativeTime(Date.now() - 3_600_000) // "1 hour ago"
|
|
20
20
|
*/
|
|
21
|
-
export { integer, money, percent } from './number'
|
|
22
|
-
export { durationClock, relativeTime, weekday } from './date'
|
|
21
|
+
export { bytes, compact, delta, integer, money, percent } from './number'
|
|
22
|
+
export { date, dateTime, duration, durationClock, relativeTime, time, weekday } from './date'
|
|
23
23
|
export { kcal, km } from './measure'
|
|
24
|
+
export { defineFormat } from './shared'
|
package/src/format/measure.ts
CHANGED
|
@@ -1,31 +1,42 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* Distance/energy formatting
|
|
2
|
+
* Distance/energy formatting. Both accept `null | undefined` and print {@link NON_FINITE} for it.
|
|
3
3
|
*/
|
|
4
|
-
import { NON_FINITE } from './shared'
|
|
4
|
+
import { isFiniteNumber, NON_FINITE, numberFormat, scaleThousands } from './shared'
|
|
5
|
+
import type { LocaleOption, Numeric } from './shared'
|
|
5
6
|
|
|
6
|
-
export type KmOptions = {
|
|
7
|
+
export type KmOptions = LocaleOption & {
|
|
7
8
|
/** Fraction digits. Default 2 (`"5.30 km"`). */
|
|
8
9
|
digits?: number
|
|
9
10
|
}
|
|
10
11
|
|
|
11
|
-
/** Meters as kilometers — `"5.30 km"
|
|
12
|
-
export function km(meters:
|
|
13
|
-
if (!
|
|
14
|
-
const { digits = 2 } = options
|
|
15
|
-
return
|
|
12
|
+
/** Meters as kilometers — `"5.30 km"` (`"5,30 km"` in `de`). */
|
|
13
|
+
export function km(meters: Numeric, options: KmOptions = {}): string {
|
|
14
|
+
if (!isFiniteNumber(meters)) return NON_FINITE
|
|
15
|
+
const { digits = 2, locale } = options
|
|
16
|
+
return numberFormat(locale, {
|
|
17
|
+
signDisplay: 'negative',
|
|
18
|
+
style: 'unit',
|
|
19
|
+
unit: 'kilometer',
|
|
20
|
+
unitDisplay: 'short',
|
|
21
|
+
minimumFractionDigits: digits,
|
|
22
|
+
maximumFractionDigits: digits,
|
|
23
|
+
}).format(meters / 1000)
|
|
16
24
|
}
|
|
17
25
|
|
|
18
|
-
export type KcalOptions = {
|
|
19
|
-
/**
|
|
20
|
-
* whole number, matching the seed (`formatKcal`). */
|
|
26
|
+
export type KcalOptions = LocaleOption & {
|
|
27
|
+
/** Max fraction digits once scaled to thousands. Default 2. Below 1000 always whole. */
|
|
21
28
|
digits?: number
|
|
22
29
|
}
|
|
23
30
|
|
|
24
|
-
/** Kilocalories
|
|
25
|
-
*
|
|
26
|
-
export function kcal(value:
|
|
27
|
-
if (!
|
|
28
|
-
const { digits = 2 } = options
|
|
29
|
-
|
|
30
|
-
|
|
31
|
+
/** Kilocalories — `"842 kcal"`, and `"1.24k kcal"` from 1000 up. The unit string is always
|
|
32
|
+
* `kcal`. */
|
|
33
|
+
export function kcal(value: Numeric, options: KcalOptions = {}): string {
|
|
34
|
+
if (!isFiniteNumber(value)) return NON_FINITE
|
|
35
|
+
const { digits = 2, locale } = options
|
|
36
|
+
const { scaled, unit } = scaleThousands(value, digits, 1)
|
|
37
|
+
const text = numberFormat(locale, {
|
|
38
|
+
signDisplay: 'negative',
|
|
39
|
+
maximumFractionDigits: unit === 0 ? 0 : digits,
|
|
40
|
+
}).format(scaled)
|
|
41
|
+
return `${text}${unit === 0 ? '' : 'k'} kcal`
|
|
31
42
|
}
|
package/src/format/number.ts
CHANGED
|
@@ -1,14 +1,11 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Number formatting — `Intl.NumberFormat` with the arguments already decided, so a caller states
|
|
3
3
|
* WHAT a number is rather than how to punctuate it. One implementation: the chart layer imports
|
|
4
|
-
* `
|
|
5
|
-
*
|
|
6
|
-
* `money`/`percent`/`integer`/`compact`/`deltaPct` are the argo-facing names (seeded from
|
|
7
|
-
* `features/*\/{formatters,formulas}.ts`); `fmtCurrency`/`fmtPercent`/`fmtInt`/`fmtCompact` are the
|
|
8
|
-
* original chart-formatter names, internal since 2.0 and delegated to by the public names.
|
|
4
|
+
* `compact` from here for its default axis format. Every formatter accepts `null | undefined` and
|
|
5
|
+
* prints {@link NON_FINITE} for it (as for `NaN` / `±Infinity`).
|
|
9
6
|
*/
|
|
10
|
-
import { NON_FINITE, numberFormat } from './shared'
|
|
11
|
-
import type { LocaleOption } from './shared'
|
|
7
|
+
import { isFiniteNumber, NON_FINITE, numberFormat, scaleThousands } from './shared'
|
|
8
|
+
import type { LocaleOption, Numeric } from './shared'
|
|
12
9
|
|
|
13
10
|
export type { LocaleOption }
|
|
14
11
|
export { NON_FINITE }
|
|
@@ -17,23 +14,27 @@ export { NON_FINITE }
|
|
|
17
14
|
* prefix that happens to contain one (`HK$`) survives the lowercasing. */
|
|
18
15
|
const COMPACT_K = /(\d\s?)K/
|
|
19
16
|
|
|
20
|
-
|
|
21
|
-
|
|
17
|
+
/** `negative` hides the sign of `-0` and of a negative that rounds to zero (`-0.4` → `0`). */
|
|
18
|
+
const SIGN: Intl.NumberFormatOptions = { signDisplay: 'negative' }
|
|
19
|
+
|
|
20
|
+
export type CompactOptions = LocaleOption & {
|
|
21
|
+
/** Fraction digits on the compacted number. Default 1 (`1.2k`, `3.4M`). */
|
|
22
22
|
digits?: number
|
|
23
23
|
}
|
|
24
24
|
|
|
25
25
|
/**
|
|
26
26
|
* Compact notation: `1.2k`, `3.4M`, `-820`. The axis formatter for any count whose range spans
|
|
27
|
-
* more than three digits.
|
|
27
|
+
* more than three digits.
|
|
28
28
|
*
|
|
29
29
|
* Note `Intl`'s compact notation is LOCALE text, not a suffix table: `en` gives `1.2K`, and this
|
|
30
30
|
* lowercases the `K` to match the package's own numeral style while leaving every other locale's
|
|
31
31
|
* word (`1,2 Mio.`) untouched.
|
|
32
32
|
*/
|
|
33
|
-
export function
|
|
34
|
-
if (!
|
|
33
|
+
export function compact(value: Numeric, options: CompactOptions = {}): string {
|
|
34
|
+
if (!isFiniteNumber(value)) return NON_FINITE
|
|
35
35
|
const { digits = 1, locale } = options
|
|
36
36
|
return numberFormat(locale, {
|
|
37
|
+
...SIGN,
|
|
37
38
|
notation: 'compact',
|
|
38
39
|
compactDisplay: 'short',
|
|
39
40
|
maximumFractionDigits: digits,
|
|
@@ -42,7 +43,7 @@ export function fmtCompact(value: number, options: FmtCompactOptions = {}): stri
|
|
|
42
43
|
.replace(COMPACT_K, '$1k')
|
|
43
44
|
}
|
|
44
45
|
|
|
45
|
-
export type
|
|
46
|
+
export type PercentOptions = LocaleOption & {
|
|
46
47
|
/** Fraction digits. Default 0 (`42%`). */
|
|
47
48
|
digits?: number
|
|
48
49
|
/**
|
|
@@ -52,55 +53,93 @@ export type FmtPercentOptions = LocaleOption & {
|
|
|
52
53
|
input?: 'ratio' | 'percent'
|
|
53
54
|
}
|
|
54
55
|
|
|
55
|
-
/** Percentages, from either a ratio or an already-scaled percentage.
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
if (!Number.isFinite(value)) return NON_FINITE
|
|
56
|
+
/** Percentages, from either a ratio or an already-scaled percentage. */
|
|
57
|
+
export function percent(value: Numeric, options: PercentOptions = {}): string {
|
|
58
|
+
if (!isFiniteNumber(value)) return NON_FINITE
|
|
59
59
|
const { digits = 0, input = 'ratio', locale } = options
|
|
60
60
|
return numberFormat(locale, {
|
|
61
|
+
...SIGN,
|
|
61
62
|
style: 'percent',
|
|
62
63
|
minimumFractionDigits: digits,
|
|
63
64
|
maximumFractionDigits: digits,
|
|
64
65
|
}).format(input === 'percent' ? value / 100 : value)
|
|
65
66
|
}
|
|
66
67
|
|
|
67
|
-
|
|
68
|
-
export const percent = fmtPercent
|
|
69
|
-
|
|
70
|
-
export type FmtCurrencyOptions = LocaleOption & {
|
|
68
|
+
export type MoneyOptions = LocaleOption & {
|
|
71
69
|
/** ISO 4217 code — required. There is no default currency. */
|
|
72
70
|
currency: string
|
|
73
71
|
/** Compact notation for the amount (`$1.2k`). Default false. */
|
|
74
72
|
compact?: boolean
|
|
75
|
-
/**
|
|
73
|
+
/**
|
|
74
|
+
* Fraction digits. Unset → the currency's own default (USD 2, JPY 0) — or, compacted, up to 1.
|
|
75
|
+
* An explicit number (`0`) pins min and max: an opt-in to rounding away the cents.
|
|
76
|
+
*/
|
|
76
77
|
digits?: number
|
|
77
78
|
}
|
|
78
79
|
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
80
|
+
function fractionDigitOptions(
|
|
81
|
+
isCompact: boolean,
|
|
82
|
+
digits: number | undefined,
|
|
83
|
+
): Intl.NumberFormatOptions {
|
|
84
|
+
if (isCompact) return { minimumFractionDigits: 0, maximumFractionDigits: digits ?? 1 }
|
|
85
|
+
if (digits === undefined) return {}
|
|
86
|
+
return { minimumFractionDigits: digits, maximumFractionDigits: digits }
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
/** Money. `money(12.5, { currency: 'USD' })` → `$12.50`. */
|
|
90
|
+
export function money(value: Numeric, options: MoneyOptions): string {
|
|
91
|
+
if (!isFiniteNumber(value)) return NON_FINITE
|
|
83
92
|
const { currency, compact: isCompact = false, digits, locale } = options
|
|
84
|
-
const fractionDigits = digits ?? (isCompact ? 1 : 0)
|
|
85
93
|
return numberFormat(locale, {
|
|
94
|
+
...SIGN,
|
|
86
95
|
style: 'currency',
|
|
87
96
|
currency,
|
|
88
97
|
...(isCompact && { notation: 'compact', compactDisplay: 'short' }),
|
|
89
|
-
|
|
90
|
-
maximumFractionDigits: fractionDigits,
|
|
98
|
+
...fractionDigitOptions(isCompact, digits),
|
|
91
99
|
})
|
|
92
100
|
.format(value)
|
|
93
101
|
.replace(COMPACT_K, '$1k')
|
|
94
102
|
}
|
|
95
103
|
|
|
96
|
-
/**
|
|
97
|
-
export
|
|
104
|
+
/** Grouped integer: `12,480`. Rounds. */
|
|
105
|
+
export function integer(value: Numeric, options: LocaleOption = {}): string {
|
|
106
|
+
if (!isFiniteNumber(value)) return NON_FINITE
|
|
107
|
+
return numberFormat(options.locale, { ...SIGN, maximumFractionDigits: 0 }).format(value)
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
const BYTE_UNITS = ['B', 'kB', 'MB', 'GB', 'TB', 'PB'] as const
|
|
111
|
+
|
|
112
|
+
export type BytesOptions = LocaleOption & {
|
|
113
|
+
/** Max fraction digits. Default 1 (`1.2 MB`). */
|
|
114
|
+
digits?: number
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
/**
|
|
118
|
+
* Byte sizes in DECIMAL (SI) units — `1 kB` is 1000 bytes, the convention of browsers' download
|
|
119
|
+
* UIs, storage vendors and `Intl`'s own `kilobyte`. `bytes(1_200_000)` → `1.2 MB`; below 1000 it
|
|
120
|
+
* prints whole bytes (`512 B`). Negative values keep their sign. The cap is `PB`.
|
|
121
|
+
*/
|
|
122
|
+
export function bytes(value: Numeric, options: BytesOptions = {}): string {
|
|
123
|
+
if (!isFiniteNumber(value)) return NON_FINITE
|
|
124
|
+
const { digits = 1, locale } = options
|
|
125
|
+
const { scaled, unit } = scaleThousands(value, digits, BYTE_UNITS.length - 1)
|
|
126
|
+
const text = numberFormat(locale, {
|
|
127
|
+
...SIGN,
|
|
128
|
+
maximumFractionDigits: unit === 0 ? 0 : digits,
|
|
129
|
+
}).format(scaled)
|
|
130
|
+
return `${text} ${BYTE_UNITS[unit]}`
|
|
131
|
+
}
|
|
98
132
|
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
return numberFormat(options.locale, { maximumFractionDigits: 0 }).format(value)
|
|
133
|
+
export type DeltaOptions = LocaleOption & {
|
|
134
|
+
/** Max fraction digits. Default 1 (`+3.2`). */
|
|
135
|
+
digits?: number
|
|
103
136
|
}
|
|
104
137
|
|
|
105
|
-
/**
|
|
106
|
-
export
|
|
138
|
+
/** A signed change: `+3.2`, `-1`, `0`. Zero (and anything that rounds to it) carries no sign. */
|
|
139
|
+
export function delta(value: Numeric, options: DeltaOptions = {}): string {
|
|
140
|
+
if (!isFiniteNumber(value)) return NON_FINITE
|
|
141
|
+
const { digits = 1, locale } = options
|
|
142
|
+
return numberFormat(locale, { signDisplay: 'exceptZero', maximumFractionDigits: digits }).format(
|
|
143
|
+
value,
|
|
144
|
+
)
|
|
145
|
+
}
|