@umituz/web-design-system 6.9.0 → 6.10.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.
|
|
3
|
+
"version": "6.10.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
|
+
import { describe, expect, it } from 'vitest';
|
|
2
|
+
import { formatDate, formatDateTime, parseDateInput } from '../infrastructure/utils/date.util';
|
|
3
|
+
|
|
4
|
+
const D = (y: number, m: number, d: number) => new Date(y, m - 1, d);
|
|
5
|
+
|
|
6
|
+
describe('parseDateInput', () => {
|
|
7
|
+
it('parses date-only ISO as local midnight, not UTC', () => {
|
|
8
|
+
expect(parseDateInput('2026-11-10')?.getTime()).toBe(D(2026, 11, 10).getTime());
|
|
9
|
+
});
|
|
10
|
+
|
|
11
|
+
it('passes Date instances through and rejects invalid ones', () => {
|
|
12
|
+
const date = D(2026, 1, 5);
|
|
13
|
+
expect(parseDateInput(date)).toBe(date);
|
|
14
|
+
expect(parseDateInput(new Date('nope'))).toBeNull();
|
|
15
|
+
});
|
|
16
|
+
|
|
17
|
+
it('rejects empty and whitespace strings', () => {
|
|
18
|
+
expect(parseDateInput('')).toBeNull();
|
|
19
|
+
expect(parseDateInput(' ')).toBeNull();
|
|
20
|
+
});
|
|
21
|
+
|
|
22
|
+
it('falls back to the standard parser for full ISO strings', () => {
|
|
23
|
+
expect(parseDateInput('2026-11-10T09:30:00')?.getFullYear()).toBe(2026);
|
|
24
|
+
});
|
|
25
|
+
});
|
|
26
|
+
|
|
27
|
+
describe('formatDate presets', () => {
|
|
28
|
+
it('medium renders Mon D, YYYY', () => {
|
|
29
|
+
expect(formatDate('2026-11-10')).toBe('Nov 10, 2026');
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
it('long renders weekday, Month D, YYYY', () => {
|
|
33
|
+
expect(formatDate('2026-11-10', 'long')).toBe('Tuesday, November 10, 2026');
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
it('short renders Mon D without the year', () => {
|
|
37
|
+
expect(formatDate('2026-11-10', 'short')).toBe('Nov 10');
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
it('monthYear renders Mon YYYY', () => {
|
|
41
|
+
expect(formatDate('2026-11-10', 'monthYear')).toBe('Nov 2026');
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
it('honors a non-default locale', () => {
|
|
45
|
+
const out = formatDate('2026-11-10', 'medium', { locale: 'de-DE' });
|
|
46
|
+
expect(out.replace(new RegExp(String.fromCharCode(160), 'g'), ' ')).toBe('10. Nov. 2026');
|
|
47
|
+
});
|
|
48
|
+
});
|
|
49
|
+
|
|
50
|
+
describe('formatDate numeric style', () => {
|
|
51
|
+
it('renders dmy with the default dot separator', () => {
|
|
52
|
+
expect(formatDate('2026-11-10', 'numeric')).toBe('10.11.2026');
|
|
53
|
+
});
|
|
54
|
+
|
|
55
|
+
it('renders slash-separated dmy, mdy, and ymd orders', () => {
|
|
56
|
+
expect(formatDate('2026-11-10', 'numeric', { order: 'dmy', separator: '/' })).toBe('10/11/2026');
|
|
57
|
+
expect(formatDate('2026-11-10', 'numeric', { order: 'mdy', separator: '/' })).toBe('11/10/2026');
|
|
58
|
+
expect(formatDate('2026-11-10', 'numeric', { order: 'ymd', separator: '-' })).toBe('2026-11-10');
|
|
59
|
+
});
|
|
60
|
+
|
|
61
|
+
it('pads single-digit day and month to two digits', () => {
|
|
62
|
+
expect(formatDate('2026-01-05', 'numeric')).toBe('05.01.2026');
|
|
63
|
+
});
|
|
64
|
+
});
|
|
65
|
+
|
|
66
|
+
describe('formatDate invalid inputs', () => {
|
|
67
|
+
it('renders the em dash for null, undefined, and garbage', () => {
|
|
68
|
+
expect(formatDate(null)).toBe('—');
|
|
69
|
+
expect(formatDate(undefined)).toBe('—');
|
|
70
|
+
expect(formatDate('not-a-date')).toBe('—');
|
|
71
|
+
expect(formatDate('', 'numeric')).toBe('—');
|
|
72
|
+
});
|
|
73
|
+
|
|
74
|
+
it('supports a custom invalidOutput', () => {
|
|
75
|
+
expect(formatDate(null, 'medium', { invalidOutput: 'n/a' })).toBe('n/a');
|
|
76
|
+
});
|
|
77
|
+
});
|
|
78
|
+
|
|
79
|
+
describe('formatDateTime', () => {
|
|
80
|
+
it('composes the medium date with a 24h clock', () => {
|
|
81
|
+
expect(formatDateTime('2026-11-10T09:30:00')).toBe('Nov 10, 2026, 09:30');
|
|
82
|
+
});
|
|
83
|
+
|
|
84
|
+
it('supports a custom time separator and locale clock', () => {
|
|
85
|
+
const out = formatDateTime('2026-11-10T14:05:00', { timeSeparator: ' · ', locale: 'en-GB' });
|
|
86
|
+
expect(out).toBe('10 Nov 2026 · 14:05');
|
|
87
|
+
});
|
|
88
|
+
|
|
89
|
+
it('renders the placeholder for invalid input', () => {
|
|
90
|
+
expect(formatDateTime('nope')).toBe('—');
|
|
91
|
+
expect(formatDateTime(null, { invalidOutput: '—' })).toBe('—');
|
|
92
|
+
});
|
|
93
|
+
});
|
|
94
|
+
|
|
95
|
+
describe('formatter cache', () => {
|
|
96
|
+
it('does not contaminate across locales for the same instant', () => {
|
|
97
|
+
const en = formatDate('2026-11-10');
|
|
98
|
+
formatDate('2026-11-10', 'medium', { locale: 'de-DE' });
|
|
99
|
+
expect(formatDate('2026-11-10')).toBe(en);
|
|
100
|
+
});
|
|
101
|
+
});
|
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
export type DateStyle = 'medium' | 'long' | 'short' | 'monthYear' | 'numeric';
|
|
2
|
+
|
|
3
|
+
export interface DateFormatOptions {
|
|
4
|
+
locale?: string;
|
|
5
|
+
order?: 'dmy' | 'mdy' | 'ymd';
|
|
6
|
+
separator?: string;
|
|
7
|
+
invalidOutput?: string;
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
export interface DateTimeFormatOptions extends DateFormatOptions {
|
|
11
|
+
timeSeparator?: string;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
const DEFAULT_LOCALE = 'en';
|
|
15
|
+
const DEFAULT_INVALID_OUTPUT = '—';
|
|
16
|
+
const DEFAULT_SEPARATOR = '.';
|
|
17
|
+
|
|
18
|
+
const PRESET_OPTIONS: Record<Exclude<DateStyle, 'numeric'>, Intl.DateTimeFormatOptions> = {
|
|
19
|
+
medium: { day: 'numeric', month: 'short', year: 'numeric' },
|
|
20
|
+
long: { weekday: 'long', day: 'numeric', month: 'long', year: 'numeric' },
|
|
21
|
+
short: { day: 'numeric', month: 'short' },
|
|
22
|
+
monthYear: { month: 'short', year: 'numeric' },
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
const formatterCache = new Map<string, Intl.DateTimeFormat>();
|
|
26
|
+
|
|
27
|
+
const cachedFormatter = (locale: string, options: Intl.DateTimeFormatOptions): Intl.DateTimeFormat => {
|
|
28
|
+
const key = `${locale}|${JSON.stringify(options)}`;
|
|
29
|
+
let formatter = formatterCache.get(key);
|
|
30
|
+
if (!formatter) {
|
|
31
|
+
formatter = new Intl.DateTimeFormat(locale, options);
|
|
32
|
+
formatterCache.set(key, formatter);
|
|
33
|
+
}
|
|
34
|
+
return formatter;
|
|
35
|
+
};
|
|
36
|
+
|
|
37
|
+
export const parseDateInput = (input: string | Date): Date | null => {
|
|
38
|
+
if (input instanceof Date) {
|
|
39
|
+
return Number.isNaN(input.getTime()) ? null : input;
|
|
40
|
+
}
|
|
41
|
+
if (typeof input !== 'string' || input.trim() === '') {
|
|
42
|
+
return null;
|
|
43
|
+
}
|
|
44
|
+
const dateOnly = /^(\d{4})-(\d{2})-(\d{2})$/.exec(input.trim());
|
|
45
|
+
if (dateOnly) {
|
|
46
|
+
const local = new Date(
|
|
47
|
+
Number(dateOnly[1]),
|
|
48
|
+
Number(dateOnly[2]) - 1,
|
|
49
|
+
Number(dateOnly[3]),
|
|
50
|
+
);
|
|
51
|
+
return Number.isNaN(local.getTime()) ? null : local;
|
|
52
|
+
}
|
|
53
|
+
const parsed = new Date(input);
|
|
54
|
+
return Number.isNaN(parsed.getTime()) ? null : parsed;
|
|
55
|
+
};
|
|
56
|
+
|
|
57
|
+
const formatNumeric = (date: Date, locale: string, order: NonNullable<DateFormatOptions['order']>, separator: string): string => {
|
|
58
|
+
const parts = cachedFormatter(locale, {
|
|
59
|
+
day: '2-digit',
|
|
60
|
+
month: '2-digit',
|
|
61
|
+
year: 'numeric',
|
|
62
|
+
}).formatToParts(date);
|
|
63
|
+
const valueOf = (type: string): string => parts.find((part) => part.type === type)?.value ?? '';
|
|
64
|
+
const day = valueOf('day');
|
|
65
|
+
const month = valueOf('month');
|
|
66
|
+
const year = valueOf('year');
|
|
67
|
+
if (order === 'mdy') return `${month}${separator}${day}${separator}${year}`;
|
|
68
|
+
if (order === 'ymd') return `${year}${separator}${month}${separator}${day}`;
|
|
69
|
+
return `${day}${separator}${month}${separator}${year}`;
|
|
70
|
+
};
|
|
71
|
+
|
|
72
|
+
export const formatDate = (
|
|
73
|
+
input: string | Date | null | undefined,
|
|
74
|
+
style: DateStyle = 'medium',
|
|
75
|
+
options: DateFormatOptions = {},
|
|
76
|
+
): string => {
|
|
77
|
+
if (input === null || input === undefined) {
|
|
78
|
+
return options.invalidOutput ?? DEFAULT_INVALID_OUTPUT;
|
|
79
|
+
}
|
|
80
|
+
const date = parseDateInput(input);
|
|
81
|
+
if (!date) {
|
|
82
|
+
return options.invalidOutput ?? DEFAULT_INVALID_OUTPUT;
|
|
83
|
+
}
|
|
84
|
+
const locale = options.locale ?? DEFAULT_LOCALE;
|
|
85
|
+
if (style === 'numeric') {
|
|
86
|
+
return formatNumeric(date, locale, options.order ?? 'dmy', options.separator ?? DEFAULT_SEPARATOR);
|
|
87
|
+
}
|
|
88
|
+
return cachedFormatter(locale, PRESET_OPTIONS[style]).format(date);
|
|
89
|
+
};
|
|
90
|
+
|
|
91
|
+
export const formatDateTime = (
|
|
92
|
+
input: string | Date | null | undefined,
|
|
93
|
+
options: DateTimeFormatOptions = {},
|
|
94
|
+
): string => {
|
|
95
|
+
if (input === null || input === undefined) {
|
|
96
|
+
return options.invalidOutput ?? DEFAULT_INVALID_OUTPUT;
|
|
97
|
+
}
|
|
98
|
+
const date = parseDateInput(input);
|
|
99
|
+
if (!date) {
|
|
100
|
+
return options.invalidOutput ?? DEFAULT_INVALID_OUTPUT;
|
|
101
|
+
}
|
|
102
|
+
const locale = options.locale ?? DEFAULT_LOCALE;
|
|
103
|
+
const clock = cachedFormatter(locale, {
|
|
104
|
+
hour: '2-digit',
|
|
105
|
+
minute: '2-digit',
|
|
106
|
+
hour12: false,
|
|
107
|
+
}).format(date);
|
|
108
|
+
return `${formatDate(date, 'medium', { locale })}${options.timeSeparator ?? ', '}${clock}`;
|
|
109
|
+
};
|
|
@@ -33,6 +33,9 @@ export { initialsOf, nameHue } from './name.util';
|
|
|
33
33
|
export { timeAgo } from './time.util';
|
|
34
34
|
export type { TimeAgoOptions } from './time.util';
|
|
35
35
|
|
|
36
|
+
export { formatDate, formatDateTime, parseDateInput } from './date.util';
|
|
37
|
+
export type { DateStyle, DateFormatOptions, DateTimeFormatOptions } from './date.util';
|
|
38
|
+
|
|
36
39
|
export { formatCount } from './format.util';
|
|
37
40
|
export type { CountFormatOptions } from './format.util';
|
|
38
41
|
|