@umituz/web-design-system 6.8.0 → 6.9.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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@umituz/web-design-system",
3
- "version": "6.8.0",
3
+ "version": "6.9.0",
4
4
  "private": false,
5
5
  "type": "module",
6
6
  "description": "Web Design System - Tailwind v4 authored, precompiled CSS shipped. Atomic Design components (Atoms, Molecules, Organisms, Templates) for React applications.",
@@ -0,0 +1,101 @@
1
+
2
+ import { describe, expect, it } from 'vitest';
3
+ import {
4
+ formatCurrency,
5
+ formatCurrencyMinor,
6
+ formatCount,
7
+ timeAgo,
8
+ } from '../infrastructure/utils';
9
+
10
+ describe('formatCurrency symbol composition', () => {
11
+ it('forces the symbol to the front regardless of locale order', () => {
12
+ expect(formatCurrency(1234.56, 'EUR', { locale: 'de-DE', symbolPosition: 'prefix' })).toBe('€1.234,56');
13
+ expect(formatCurrency(1234.5, 'USD', { locale: 'en-US', symbolPosition: 'prefix' })).toBe('$1,234.50');
14
+ });
15
+
16
+ it('forces the symbol to the end with a single space', () => {
17
+ expect(formatCurrency(1234.5, 'USD', { locale: 'en-US', symbolPosition: 'suffix' })).toBe('1,234.50 $');
18
+ });
19
+
20
+ it('moves the sign between symbol and digits on demand', () => {
21
+ expect(
22
+ formatCurrency(-50.25, 'EUR', {
23
+ locale: 'en-US',
24
+ currencyDisplay: 'narrowSymbol',
25
+ symbolPosition: 'prefix',
26
+ signPosition: 'afterSymbol',
27
+ }),
28
+ ).toBe('€-50.25');
29
+ expect(formatCurrency(-5, 'USD', { locale: 'en-US', signPosition: 'afterSymbol' })).toBe('$-5.00');
30
+ });
31
+
32
+ it('drops the symbol entirely for symbol-less composites', () => {
33
+ expect(formatCurrency(1500, 'USD', { cents: false, currencyDisplay: 'none' })).toBe('1,500');
34
+ expect(formatCurrency(1500, 'USD', { cents: false, currencyDisplay: 'none', showCode: true })).toBe(
35
+ '1,500 USD',
36
+ );
37
+ expect(formatCurrency(-42.25, 'USD', { currencyDisplay: 'none' })).toBe('-42.25');
38
+ });
39
+
40
+ it('applies SI suffix casing to compact output', () => {
41
+ expect(
42
+ formatCurrency(1500, 'USD', { compact: true, maximumFractionDigits: 1, compactSuffixCase: 'si' }),
43
+ ).toBe('$1.5k');
44
+ expect(
45
+ formatCurrency(12500000, 'USD', {
46
+ compact: true,
47
+ minimumFractionDigits: 2,
48
+ maximumFractionDigits: 2,
49
+ compactSuffixCase: 'si',
50
+ }),
51
+ ).toBe('$12.50M');
52
+ expect(
53
+ formatCurrency(1500000, 'GBP', { locale: 'en-GB', compact: true, maximumFractionDigits: 1, compactSuffixCase: 'si' }),
54
+ ).toBe('£1.5M');
55
+ });
56
+
57
+ it('composes without contaminating the plain formatter cache', () => {
58
+ expect(formatCurrency(5, 'USD')).toBe('$5.00');
59
+ expect(formatCurrency(5, 'USD', { symbolPosition: 'suffix' })).toBe('5.00 $');
60
+ expect(formatCurrency(5, 'USD')).toBe('$5.00');
61
+ });
62
+ });
63
+
64
+ describe('formatCurrencyMinor', () => {
65
+ it('divides minor units by the currency digits', () => {
66
+ expect(formatCurrencyMinor(123456, 'USD')).toBe('$1,234.56');
67
+ expect(formatCurrencyMinor(1500, 'JPY')).toBe('¥1,500');
68
+ });
69
+
70
+ it('honors an explicit minor digit override', () => {
71
+ expect(formatCurrencyMinor(1500, 'JPY', { minorDigits: 2 })).toBe('¥15.00');
72
+ expect(formatCurrencyMinor(1500, 'JPY', { minorDigits: 2, maximumFractionDigits: 0 })).toBe('¥15');
73
+ });
74
+
75
+ it('passes the full option surface through', () => {
76
+ expect(formatCurrencyMinor(12500000, 'USD', { compact: true })).toBe('$125.0K');
77
+ expect(formatCurrencyMinor(12500000, 'USD', { compact: true, maximumFractionDigits: 0 })).toBe('$125K');
78
+ });
79
+ });
80
+
81
+ describe('formatCount digit options', () => {
82
+ it('keeps the plain default untouched', () => {
83
+ expect(formatCount(1234.567, 'en-US')).toBe('1,234.567');
84
+ });
85
+
86
+ it('applies explicit fraction digits', () => {
87
+ expect(formatCount(1234.567, 'en-US', { maximumFractionDigits: 2 })).toBe('1,234.57');
88
+ expect(formatCount(1234.5, 'de-DE', { minimumFractionDigits: 2 })).toBe('1.234,50');
89
+ expect(formatCount(1234, 'en-US', { minimumFractionDigits: 0, maximumFractionDigits: 1 })).toBe('1,234');
90
+ });
91
+ });
92
+
93
+ describe('timeAgo invalid date handling', () => {
94
+ it('returns the fallback instead of throwing when requested', () => {
95
+ expect(timeAgo('not-a-date', { invalidDateOutput: '—' })).toBe('—');
96
+ });
97
+
98
+ it('still throws on invalid dates by default', () => {
99
+ expect(() => timeAgo('not-a-date')).toThrow();
100
+ });
101
+ });
@@ -1 +1,10 @@
1
- export const formatCount = (value: number, locale: string): string => new Intl.NumberFormat(locale).format(value);
1
+ export interface CountFormatOptions {
2
+ minimumFractionDigits?: number;
3
+ maximumFractionDigits?: number;
4
+ }
5
+
6
+ export const formatCount = (
7
+ value: number,
8
+ locale: string,
9
+ options: CountFormatOptions = {},
10
+ ): string => new Intl.NumberFormat(locale, options).format(value);
@@ -34,6 +34,7 @@ export { timeAgo } from './time.util';
34
34
  export type { TimeAgoOptions } from './time.util';
35
35
 
36
36
  export { formatCount } from './format.util';
37
+ export type { CountFormatOptions } from './format.util';
37
38
 
38
39
  export { uniqueId } from './id.util';
39
40
 
@@ -49,6 +50,13 @@ export {
49
50
  sumMinor,
50
51
  formatCurrency,
51
52
  formatCurrencyCompact,
53
+ formatCurrencyMinor,
52
54
  CURRENCY_LOCALES,
53
55
  } from './money.utils';
54
- export type { LineTotals, TotalsInputLine, MoneyValue, FormatCurrencyOptions } from './money.utils';
56
+ export type {
57
+ LineTotals,
58
+ TotalsInputLine,
59
+ MoneyValue,
60
+ FormatCurrencyOptions,
61
+ MinorCurrencyOptions,
62
+ } from './money.utils';
@@ -120,7 +120,10 @@ export interface FormatCurrencyOptions {
120
120
  smartFractions?: boolean;
121
121
  minimumFractionDigits?: number;
122
122
  maximumFractionDigits?: number;
123
- currencyDisplay?: Intl.NumberFormatOptions['currencyDisplay'];
123
+ currencyDisplay?: Intl.NumberFormatOptions['currencyDisplay'] | 'none';
124
+ symbolPosition?: 'auto' | 'prefix' | 'suffix';
125
+ signPosition?: 'auto' | 'afterSymbol';
126
+ compactSuffixCase?: 'auto' | 'si';
124
127
  }
125
128
 
126
129
  interface FractionRange {
@@ -155,6 +158,63 @@ function resolveFractionRange(amount: MoneyValue, currency: string, options: For
155
158
 
156
159
  const currencyFormatterCache = new Map<string, Intl.NumberFormat>();
157
160
 
161
+ const SIGN_PART_TYPES = new Set(['minusSign', 'plusSign']);
162
+
163
+ function composeCurrencyParts(parts: Intl.NumberFormatPart[], options: FormatCurrencyOptions): string {
164
+ const mapped =
165
+ options.compactSuffixCase === 'si'
166
+ ? parts.map((part) =>
167
+ part.type === 'compact'
168
+ ? { ...part, value: part.value.toLowerCase() === 'k' ? 'k' : part.value.toUpperCase() }
169
+ : part,
170
+ )
171
+ : parts;
172
+ const dropSymbol = options.currencyDisplay === 'none';
173
+ const position = options.symbolPosition ?? 'auto';
174
+ const reposition = dropSymbol || position === 'prefix' || position === 'suffix';
175
+ if (!reposition && options.signPosition !== 'afterSymbol') {
176
+ return mapped.map((part) => part.value).join('');
177
+ }
178
+ if (!reposition) {
179
+ const signs = mapped.filter((part) => SIGN_PART_TYPES.has(part.type)).map((part) => part.value).join('');
180
+ const chunks: string[] = [];
181
+ let signsEmitted = false;
182
+ for (const part of mapped) {
183
+ if (SIGN_PART_TYPES.has(part.type)) continue;
184
+ chunks.push(part.value);
185
+ if (!signsEmitted && part.type === 'currency') {
186
+ chunks.push(signs);
187
+ signsEmitted = true;
188
+ }
189
+ }
190
+ if (!signsEmitted) chunks.push(signs);
191
+ return chunks.join('');
192
+ }
193
+ const currencyIndexes: number[] = [];
194
+ mapped.forEach((part, index) => {
195
+ if (part.type === 'currency') currencyIndexes.push(index);
196
+ });
197
+ const removed = new Set<number>(currencyIndexes);
198
+ for (const index of currencyIndexes) {
199
+ const next = mapped[index + 1];
200
+ const prev = mapped[index - 1];
201
+ if (next && next.type === 'literal' && next.value.trim() === '') removed.add(index + 1);
202
+ else if (prev && prev.type === 'literal' && prev.value.trim() === '') removed.add(index - 1);
203
+ }
204
+ const symbol = mapped
205
+ .filter((_, index) => currencyIndexes.includes(index))
206
+ .map((part) => part.value)
207
+ .join('');
208
+ const keep = mapped.filter((_, index) => !removed.has(index));
209
+ if (dropSymbol) return keep.map((part) => part.value).join('');
210
+ if (position === 'prefix') {
211
+ const signs = keep.filter((part) => SIGN_PART_TYPES.has(part.type)).map((part) => part.value).join('');
212
+ const body = keep.filter((part) => !SIGN_PART_TYPES.has(part.type)).map((part) => part.value).join('');
213
+ return options.signPosition === 'afterSymbol' ? `${symbol}${signs}${body}` : `${symbol}${body}`;
214
+ }
215
+ return `${keep.map((part) => part.value).join('')} ${symbol}`;
216
+ }
217
+
158
218
  export function formatCurrency(
159
219
  amount: MoneyValue,
160
220
  currency: string,
@@ -173,7 +233,9 @@ export function formatCurrency(
173
233
  minimumFractionDigits: min,
174
234
  maximumFractionDigits: max,
175
235
  signDisplay: options.signed ? 'always' : 'auto',
176
- ...(options.currencyDisplay ? { currencyDisplay: options.currencyDisplay } : {}),
236
+ ...(options.currencyDisplay && options.currencyDisplay !== 'none'
237
+ ? { currencyDisplay: options.currencyDisplay }
238
+ : {}),
177
239
  });
178
240
  currencyFormatterCache.set(key, formatter);
179
241
  }
@@ -181,10 +243,35 @@ export function formatCurrency(
181
243
  typeof amount === 'number'
182
244
  ? amount
183
245
  : (amount as unknown as Parameters<Intl.NumberFormat['format']>[0]);
184
- const formatted = formatter.format(value);
246
+ const needsComposition =
247
+ options.currencyDisplay === 'none' ||
248
+ (options.symbolPosition !== undefined && options.symbolPosition !== 'auto') ||
249
+ options.signPosition === 'afterSymbol' ||
250
+ options.compactSuffixCase === 'si';
251
+ const formatted = needsComposition
252
+ ? composeCurrencyParts(formatter.formatToParts(value), options)
253
+ : formatter.format(value);
185
254
  return options.showCode ? `${formatted} ${upper}` : formatted;
186
255
  }
187
256
 
257
+ export interface MinorCurrencyOptions extends FormatCurrencyOptions {
258
+ minorDigits?: number;
259
+ }
260
+
261
+ export function formatCurrencyMinor(
262
+ amountMinor: number,
263
+ currency: string,
264
+ options: MinorCurrencyOptions = {},
265
+ ): string {
266
+ const { minorDigits, ...rest } = options;
267
+ const digits = minorDigits ?? currencyFractionDigits(currency);
268
+ if (minorDigits !== undefined) {
269
+ rest.minimumFractionDigits = rest.minimumFractionDigits ?? minorDigits;
270
+ rest.maximumFractionDigits = rest.maximumFractionDigits ?? minorDigits;
271
+ }
272
+ return formatCurrency(amountMinor / 10 ** digits, currency, rest);
273
+ }
274
+
188
275
  export function formatCurrencyCompact(
189
276
  amount: MoneyValue,
190
277
  currency: string,
@@ -2,6 +2,7 @@ export interface TimeAgoOptions {
2
2
  now?: Date;
3
3
  locale?: string;
4
4
  compact?: boolean;
5
+ invalidDateOutput?: string;
5
6
  }
6
7
 
7
8
  const MINUTE = 60;
@@ -14,7 +15,9 @@ const YEAR = 365.25 * DAY;
14
15
  export const timeAgo = (input: Date | string | number, options: TimeAgoOptions = {}): string => {
15
16
  const { now = new Date(), locale = 'en', compact = false } = options;
16
17
  const date = input instanceof Date ? input : new Date(input);
17
- const deltaSeconds = Math.round((date.getTime() - now.getTime()) / 1000);
18
+ const time = date.getTime();
19
+ if (Number.isNaN(time) && options.invalidDateOutput !== undefined) return options.invalidDateOutput;
20
+ const deltaSeconds = Math.round((time - now.getTime()) / 1000);
18
21
  const formatter = new Intl.RelativeTimeFormat(locale, { numeric: 'auto', style: compact ? 'narrow' : 'long' });
19
22
  const abs = Math.abs(deltaSeconds);
20
23
  if (abs < 45) return formatter.format(0, 'second');