@cognitap/radiant 0.0.59 → 0.0.60

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 (41) hide show
  1. package/CHANGELOG.md +4 -0
  2. package/dist/library/Input.d.ts +1 -1
  3. package/dist/library/Input.d.ts.map +1 -1
  4. package/dist/library/Input.js +2 -2
  5. package/dist/library/Input.js.map +1 -1
  6. package/dist/library/InputGroup.d.ts +1 -1
  7. package/dist/library/InputGroup.d.ts.map +1 -1
  8. package/dist/library/InputGroup.js.map +1 -1
  9. package/dist/library/NumberField.d.ts +25 -0
  10. package/dist/library/NumberField.d.ts.map +1 -0
  11. package/dist/library/NumberField.js +160 -0
  12. package/dist/library/NumberField.js.map +1 -0
  13. package/dist/library/Separator.d.ts +2 -1
  14. package/dist/library/Separator.d.ts.map +1 -1
  15. package/dist/library/Separator.js.map +1 -1
  16. package/dist/library/cn.d.ts.map +1 -1
  17. package/dist/library/cn.js +1 -0
  18. package/dist/library/cn.js.map +1 -1
  19. package/dist/library/index.d.ts +1 -0
  20. package/dist/library/index.d.ts.map +1 -1
  21. package/dist/library/index.js +1 -0
  22. package/dist/library/index.js.map +1 -1
  23. package/dist/library/internal/numberField.d.ts +21 -0
  24. package/dist/library/internal/numberField.d.ts.map +1 -0
  25. package/dist/library/internal/numberField.js +158 -0
  26. package/dist/library/internal/numberField.js.map +1 -0
  27. package/dist/library/styles.css +1 -1
  28. package/dist/library/themes/components.d.ts +3 -0
  29. package/dist/library/themes/components.d.ts.map +1 -1
  30. package/dist/library/themes/components.js +1 -0
  31. package/dist/library/themes/components.js.map +1 -1
  32. package/docs/architecture.md +6 -8
  33. package/package.json +1 -1
  34. package/src/Input.tsx +3 -3
  35. package/src/InputGroup.tsx +1 -1
  36. package/src/NumberField.tsx +302 -0
  37. package/src/Separator.tsx +2 -1
  38. package/src/cn.ts +1 -0
  39. package/src/index.ts +2 -0
  40. package/src/internal/numberField.ts +193 -0
  41. package/src/themes/components.ts +1 -0
@@ -0,0 +1,193 @@
1
+ /** A bounded editable decimal adapter; Intl supplies display and locale symbols. */
2
+ export type NumberFieldFormatOptions = {
3
+ style?: 'decimal' | 'percent' | 'currency'
4
+ currency?: string
5
+ currencyDisplay?: 'symbol' | 'narrowSymbol' | 'code'
6
+ currencySign?: 'standard' | 'accounting'
7
+ minimumFractionDigits?: number
8
+ maximumFractionDigits?: number
9
+ useGrouping?: boolean
10
+ signDisplay?: 'auto' | 'always' | 'exceptZero'
11
+ }
12
+ const clean = (text: string) =>
13
+ text.replace(/[\u061c\u200e\u200f]/g, '').replace(/[\u00a0\u202f]/g, ' ')
14
+ const numeric = new Set(['integer', 'group', 'decimal', 'fraction'])
15
+ export function createNumberAdapter(locale: string, options: NumberFieldFormatOptions = {}) {
16
+ const allowed = new Set([
17
+ 'style',
18
+ 'currency',
19
+ 'currencyDisplay',
20
+ 'currencySign',
21
+ 'minimumFractionDigits',
22
+ 'maximumFractionDigits',
23
+ 'useGrouping',
24
+ 'signDisplay'
25
+ ])
26
+ if (
27
+ Object.keys(options).some(key => !allowed.has(key)) ||
28
+ (options.style !== undefined && !['decimal', 'percent', 'currency'].includes(options.style)) ||
29
+ (options.currencyDisplay !== undefined &&
30
+ !['symbol', 'narrowSymbol', 'code'].includes(options.currencyDisplay)) ||
31
+ (options.signDisplay !== undefined &&
32
+ !['auto', 'always', 'exceptZero'].includes(options.signDisplay))
33
+ )
34
+ throw new TypeError('NumberField supports standard decimal, percent and currency formatting.')
35
+ const formatter = new Intl.NumberFormat(locale, { maximumFractionDigits: 20, ...options })
36
+ const resolved = formatter.resolvedOptions()
37
+ const decimal = new Intl.NumberFormat(locale, {
38
+ numberingSystem: resolved.numberingSystem,
39
+ maximumFractionDigits: 20
40
+ })
41
+ const digits = new Map<string, string>()
42
+ const digitFormatter = new Intl.NumberFormat(locale, {
43
+ numberingSystem: resolved.numberingSystem,
44
+ useGrouping: false
45
+ })
46
+ for (let index = 0; index < 10; index++) digits.set(digitFormatter.format(index), String(index))
47
+ const ascii = (text: string) =>
48
+ Array.from(clean(text), digit => digits.get(digit) ?? digit).join('')
49
+ const samples = decimal.formatToParts(123456789012345.6)
50
+ const group = clean(samples.find(part => part.type === 'group')?.value ?? ',')
51
+ const separator = clean(samples.find(part => part.type === 'decimal')?.value ?? '.')
52
+ const groups = samples
53
+ .filter(part => part.type === 'integer')
54
+ .map(part => ascii(part.value).length)
55
+ const primary = groups.at(-1) ?? 3,
56
+ secondary = groups.at(-2) ?? primary
57
+ const affixes = [1, -1, 0].map(value => {
58
+ const parts = formatter.formatToParts(value)
59
+ const first = parts.findIndex(part => numeric.has(part.type))
60
+ const last = parts.length - 1 - [...parts].reverse().findIndex(part => numeric.has(part.type))
61
+ return {
62
+ prefix: clean(
63
+ parts
64
+ .slice(0, first)
65
+ .map(part => part.value)
66
+ .join('')
67
+ ),
68
+ suffix: clean(
69
+ parts
70
+ .slice(last + 1)
71
+ .map(part => part.value)
72
+ .join('')
73
+ ),
74
+ sign: value < 0 ? -1 : 1
75
+ }
76
+ })
77
+ const plus = new Intl.NumberFormat(locale, {
78
+ maximumFractionDigits: 20,
79
+ ...options,
80
+ signDisplay: 'always'
81
+ }).formatToParts(1)
82
+ affixes.push({
83
+ prefix: clean(
84
+ plus
85
+ .slice(
86
+ 0,
87
+ plus.findIndex(part => numeric.has(part.type))
88
+ )
89
+ .map(part => part.value)
90
+ .join('')
91
+ ),
92
+ suffix: clean(
93
+ plus
94
+ .slice(plus.length - 1 - [...plus].reverse().findIndex(part => numeric.has(part.type)) + 1)
95
+ .map(part => part.value)
96
+ .join('')
97
+ ),
98
+ sign: 1
99
+ })
100
+ const minus = clean(
101
+ decimal.formatToParts(-1).find(part => part.type === 'minusSign')?.value ?? '-'
102
+ )
103
+ function bodyNumber(body: string): number | undefined {
104
+ body = ascii(body)
105
+ const split = body.split(separator)
106
+ if (split.length > 2) return
107
+ const whole = split[0]!,
108
+ fraction = split[1]
109
+ if (fraction !== undefined && !/^\d*$/.test(fraction)) return
110
+ const pieces = whole.split(group)
111
+ if (
112
+ pieces.some(piece => !/^\d+$/.test(piece)) &&
113
+ !(whole === '' && fraction && /^\d+$/.test(fraction))
114
+ )
115
+ return
116
+ if (
117
+ pieces.length > 1 &&
118
+ (pieces[0]!.length < 1 ||
119
+ pieces[0]!.length > secondary ||
120
+ pieces.at(-1)!.length !== primary ||
121
+ pieces.slice(1, -1).some(piece => piece.length !== secondary))
122
+ )
123
+ return
124
+ // Shift the decimal before conversion, avoiding percentage division tails or intermediate overflow.
125
+ const value = Number(
126
+ pieces.join('') +
127
+ (fraction === undefined ? '' : '.' + fraction) +
128
+ (resolved.style === 'percent' ? 'e-2' : '')
129
+ )
130
+ return Number.isFinite(value) ? value : undefined
131
+ }
132
+ return {
133
+ key: JSON.stringify(resolved),
134
+ format: (value: number | null) => (value === null ? '' : formatter.format(value)),
135
+ parse(text: string): number | null | undefined {
136
+ text = clean(text).trim()
137
+ if (text === '') return null
138
+ const candidates: { body: string; sign: number }[] = []
139
+ for (const { prefix, suffix, sign } of affixes)
140
+ if (
141
+ text.startsWith(prefix) &&
142
+ text.endsWith(suffix) &&
143
+ text.length > prefix.length + suffix.length
144
+ )
145
+ candidates.push({
146
+ body: text.slice(prefix.length, suffix.length ? -suffix.length : undefined),
147
+ sign
148
+ })
149
+ const negative = text.startsWith(minus) || text.startsWith('-')
150
+ const signed = negative || text.startsWith('+')
151
+ candidates.push({
152
+ body: signed
153
+ ? text.slice((negative && text.startsWith(minus) ? minus : text[0]!).length)
154
+ : text,
155
+ sign: negative ? -1 : 1
156
+ })
157
+ for (const { body, sign } of candidates) {
158
+ const parsed = bodyNumber(body)
159
+ if (parsed !== undefined) {
160
+ const value = sign * parsed
161
+ if (Number.isFinite(value)) return value === 0 ? 0 : value
162
+ }
163
+ }
164
+ return undefined
165
+ }
166
+ }
167
+ }
168
+ export function requireFiniteNumber(name: string, value: unknown, nullable = false) {
169
+ if (nullable && value === null) return
170
+ if (typeof value !== 'number' || !Number.isFinite(value))
171
+ throw new TypeError(name + ' must be finite.')
172
+ }
173
+ export const clampNumber = (value: number, min?: number, max?: number) =>
174
+ Math.min(max ?? Infinity, Math.max(min ?? -Infinity, value))
175
+ /** Add decimal coefficients before converting back to the public JavaScript number. */
176
+ export function addNumberStep(value: number, amount: number, min?: number, max?: number) {
177
+ const parts = (number: number) => {
178
+ const [mantissa, exponent = '0'] = number.toString().split('e')
179
+ const fraction = mantissa!.split('.')[1]?.length ?? 0
180
+ return {
181
+ coefficient: BigInt(mantissa!.replace('.', '')),
182
+ exponent: Number(exponent) - fraction
183
+ }
184
+ }
185
+ const a = parts(value),
186
+ b = parts(amount),
187
+ exponent = Math.min(a.exponent, b.exponent)
188
+ const coefficient =
189
+ a.coefficient * 10n ** BigInt(a.exponent - exponent) +
190
+ b.coefficient * 10n ** BigInt(b.exponent - exponent)
191
+ const next = clampNumber(Number(coefficient.toString() + 'e' + exponent), min, max)
192
+ return Number.isFinite(next) ? next : value
193
+ }
@@ -63,6 +63,7 @@ export const componentRegistry = {
63
63
  focusWidth: 'css',
64
64
  focusOffset: 'css'
65
65
  },
66
+ numberField: { gap: 'css' },
66
67
  passwordInput: { gap: 'css' },
67
68
  meter: {
68
69
  height: 'css',