@bbn/bbn 2.0.30 → 2.0.31
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.
|
@@ -1,34 +1 @@
|
|
|
1
|
-
type Style = 'full' | 'long' | 'medium' | 'short';
|
|
2
|
-
type CommonFormats = {
|
|
3
|
-
date: Array<{
|
|
4
|
-
style: Style;
|
|
5
|
-
pattern: string;
|
|
6
|
-
sample: string;
|
|
7
|
-
options: Intl.DateTimeFormatOptions;
|
|
8
|
-
}>;
|
|
9
|
-
time: Array<{
|
|
10
|
-
style: Style;
|
|
11
|
-
pattern: string;
|
|
12
|
-
sample: string;
|
|
13
|
-
options: Intl.DateTimeFormatOptions;
|
|
14
|
-
}>;
|
|
15
|
-
datetime: Array<{
|
|
16
|
-
dateStyle: Style;
|
|
17
|
-
timeStyle: Style;
|
|
18
|
-
pattern: string;
|
|
19
|
-
sample: string;
|
|
20
|
-
options: Intl.DateTimeFormatOptions;
|
|
21
|
-
}>;
|
|
22
|
-
};
|
|
23
|
-
/**
|
|
24
|
-
* Get all common date/time/datetime formats for a given locale using Intl.DateTimeFormat.
|
|
25
|
-
*
|
|
26
|
-
* - Date formats: dateStyle only (full/long/medium/short)
|
|
27
|
-
* - Time formats: timeStyle only
|
|
28
|
-
* - Datetime formats: all combinations of dateStyle × timeStyle
|
|
29
|
-
*
|
|
30
|
-
* Returns tokens using your convention: YYYY, MM, DD, HH, II, SS, A, etc.
|
|
31
|
-
*/
|
|
32
|
-
export declare function getCommonFormatsForLocale(lng: string | string[]): CommonFormats;
|
|
33
1
|
export default function buildLocaleFromIntl(): void;
|
|
34
|
-
export {};
|
|
@@ -1,45 +1,28 @@
|
|
|
1
1
|
import extend from "../../fn/object/extend.js";
|
|
2
2
|
import numProperties from "../../fn/object/numProperties.js";
|
|
3
|
-
/**
|
|
4
|
-
* Build a token pattern like "DD/MM/YYYY HH:II:SS" from Intl.DateTimeFormat parts.
|
|
5
|
-
*/
|
|
6
3
|
function partsToPattern(parts, hourCycle) {
|
|
7
4
|
let pattern = '';
|
|
8
|
-
// If we see a dayPeriod in parts, it's definitely 12-hour clock
|
|
9
5
|
const hasDayPeriod = parts.some(p => p.type === 'dayPeriod');
|
|
10
6
|
const is12h = hasDayPeriod || hourCycle === 'h12' || hourCycle === 'h11';
|
|
11
7
|
for (const p of parts) {
|
|
12
8
|
switch (p.type) {
|
|
13
9
|
case 'year':
|
|
14
|
-
// Usually "2000" → "YYYY"
|
|
15
10
|
pattern += 'YYYY';
|
|
16
11
|
break;
|
|
17
12
|
case 'month':
|
|
18
|
-
if (/^\d+$/.test(p.value))
|
|
19
|
-
// numeric month
|
|
13
|
+
if (/^\d+$/.test(p.value))
|
|
20
14
|
pattern += p.value.length === 2 ? 'MM' : 'M';
|
|
21
|
-
|
|
22
|
-
else {
|
|
23
|
-
// textual month
|
|
15
|
+
else
|
|
24
16
|
pattern += p.value.length > 3 ? 'MMMM' : 'MMM';
|
|
25
|
-
}
|
|
26
17
|
break;
|
|
27
18
|
case 'day':
|
|
28
19
|
pattern += p.value.length === 2 ? 'DD' : 'D';
|
|
29
20
|
break;
|
|
30
21
|
case 'weekday':
|
|
31
|
-
// You can refine this if you care about full vs short
|
|
32
22
|
pattern += p.value.length > 3 ? 'dddd' : 'ddd';
|
|
33
23
|
break;
|
|
34
24
|
case 'hour':
|
|
35
|
-
|
|
36
|
-
// 12-hour clock
|
|
37
|
-
pattern += p.value.length === 2 ? 'hh' : 'h';
|
|
38
|
-
}
|
|
39
|
-
else {
|
|
40
|
-
// 24-hour clock
|
|
41
|
-
pattern += p.value.length === 2 ? 'HH' : 'H';
|
|
42
|
-
}
|
|
25
|
+
pattern += is12h ? (p.value.length === 2 ? 'hh' : 'h') : (p.value.length === 2 ? 'HH' : 'H');
|
|
43
26
|
break;
|
|
44
27
|
case 'minute':
|
|
45
28
|
pattern += 'II';
|
|
@@ -48,14 +31,14 @@ function partsToPattern(parts, hourCycle) {
|
|
|
48
31
|
pattern += 'SS';
|
|
49
32
|
break;
|
|
50
33
|
case 'dayPeriod':
|
|
51
|
-
// AM/PM
|
|
52
34
|
pattern += 'A';
|
|
53
35
|
break;
|
|
54
36
|
case 'timeZoneName':
|
|
55
|
-
// You may want 'z' or 'Z' depending on your conventions
|
|
56
37
|
pattern += 'z';
|
|
57
38
|
break;
|
|
58
39
|
case 'literal':
|
|
40
|
+
pattern += p.value;
|
|
41
|
+
break;
|
|
59
42
|
default:
|
|
60
43
|
pattern += p.value;
|
|
61
44
|
break;
|
|
@@ -64,69 +47,92 @@ function partsToPattern(parts, hourCycle) {
|
|
|
64
47
|
return pattern;
|
|
65
48
|
}
|
|
66
49
|
/**
|
|
67
|
-
*
|
|
68
|
-
*
|
|
69
|
-
* - Date formats: dateStyle only (full/long/medium/short)
|
|
70
|
-
* - Time formats: timeStyle only
|
|
71
|
-
* - Datetime formats: all combinations of dateStyle × timeStyle
|
|
72
|
-
*
|
|
73
|
-
* Returns tokens using your convention: YYYY, MM, DD, HH, II, SS, A, etc.
|
|
50
|
+
* Returns all common date/time/datetime formats + weekday formats for a given locale.
|
|
74
51
|
*/
|
|
75
|
-
|
|
52
|
+
function getCommonFormatsForLocale(lng) {
|
|
76
53
|
const dateStyles = ['full', 'long', 'medium', 'short'];
|
|
77
54
|
const timeStyles = ['full', 'long', 'medium', 'short'];
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
55
|
+
const sample = new Date(Date.UTC(2000, 0, 2, 13, 45, 30));
|
|
56
|
+
const result = {
|
|
57
|
+
date: [],
|
|
58
|
+
time: [],
|
|
59
|
+
datetime: [],
|
|
60
|
+
dateWithWeekday: [],
|
|
61
|
+
datetimeWithWeekday: []
|
|
62
|
+
};
|
|
63
|
+
// --- Date only ---
|
|
85
64
|
for (const ds of dateStyles) {
|
|
86
65
|
const options = { dateStyle: ds };
|
|
87
66
|
const fmt = new Intl.DateTimeFormat(lng, options);
|
|
88
|
-
const parts = fmt.formatToParts(
|
|
89
|
-
|
|
90
|
-
date.push({
|
|
67
|
+
const parts = fmt.formatToParts(sample);
|
|
68
|
+
result.date.push({
|
|
91
69
|
style: ds,
|
|
92
|
-
pattern,
|
|
93
|
-
sample: fmt.format(
|
|
70
|
+
pattern: partsToPattern(parts, fmt.resolvedOptions().hourCycle),
|
|
71
|
+
sample: fmt.format(sample),
|
|
94
72
|
options
|
|
95
73
|
});
|
|
96
74
|
}
|
|
97
|
-
// --- Time
|
|
75
|
+
// --- Time only ---
|
|
98
76
|
for (const ts of timeStyles) {
|
|
99
77
|
const options = { timeStyle: ts };
|
|
100
78
|
const fmt = new Intl.DateTimeFormat(lng, options);
|
|
101
|
-
const parts = fmt.formatToParts(
|
|
102
|
-
|
|
103
|
-
time.push({
|
|
79
|
+
const parts = fmt.formatToParts(sample);
|
|
80
|
+
result.time.push({
|
|
104
81
|
style: ts,
|
|
105
|
-
pattern,
|
|
106
|
-
sample: fmt.format(
|
|
82
|
+
pattern: partsToPattern(parts, fmt.resolvedOptions().hourCycle),
|
|
83
|
+
sample: fmt.format(sample),
|
|
107
84
|
options
|
|
108
85
|
});
|
|
109
86
|
}
|
|
110
|
-
// --- Date +
|
|
87
|
+
// --- Date + Time ---
|
|
111
88
|
for (const ds of dateStyles) {
|
|
112
89
|
for (const ts of timeStyles) {
|
|
113
|
-
const options = {
|
|
114
|
-
dateStyle: ds,
|
|
115
|
-
timeStyle: ts
|
|
116
|
-
};
|
|
90
|
+
const options = { dateStyle: ds, timeStyle: ts };
|
|
117
91
|
const fmt = new Intl.DateTimeFormat(lng, options);
|
|
118
|
-
const parts = fmt.formatToParts(
|
|
119
|
-
|
|
120
|
-
datetime.push({
|
|
92
|
+
const parts = fmt.formatToParts(sample);
|
|
93
|
+
result.datetime.push({
|
|
121
94
|
dateStyle: ds,
|
|
122
95
|
timeStyle: ts,
|
|
123
|
-
pattern,
|
|
124
|
-
sample: fmt.format(
|
|
96
|
+
pattern: partsToPattern(parts, fmt.resolvedOptions().hourCycle),
|
|
97
|
+
sample: fmt.format(sample),
|
|
125
98
|
options
|
|
126
99
|
});
|
|
127
100
|
}
|
|
128
101
|
}
|
|
129
|
-
|
|
102
|
+
// --- Date with Weekday (long + short) ---
|
|
103
|
+
for (const ds of dateStyles) {
|
|
104
|
+
for (const w of ["long", "short"]) {
|
|
105
|
+
const options = { dateStyle: ds, weekday: w };
|
|
106
|
+
const fmt = new Intl.DateTimeFormat(lng, options);
|
|
107
|
+
const parts = fmt.formatToParts(sample);
|
|
108
|
+
result.dateWithWeekday.push({
|
|
109
|
+
style: ds,
|
|
110
|
+
weekday: w,
|
|
111
|
+
pattern: partsToPattern(parts, fmt.resolvedOptions().hourCycle),
|
|
112
|
+
sample: fmt.format(sample),
|
|
113
|
+
options
|
|
114
|
+
});
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
// --- Date + Time + Weekday ---
|
|
118
|
+
for (const ds of dateStyles) {
|
|
119
|
+
for (const ts of timeStyles) {
|
|
120
|
+
for (const w of ["long", "short"]) {
|
|
121
|
+
const options = { dateStyle: ds, timeStyle: ts, weekday: w };
|
|
122
|
+
const fmt = new Intl.DateTimeFormat(lng, options);
|
|
123
|
+
const parts = fmt.formatToParts(sample);
|
|
124
|
+
result.datetimeWithWeekday.push({
|
|
125
|
+
dateStyle: ds,
|
|
126
|
+
timeStyle: ts,
|
|
127
|
+
weekday: w,
|
|
128
|
+
pattern: partsToPattern(parts, fmt.resolvedOptions().hourCycle),
|
|
129
|
+
sample: fmt.format(sample),
|
|
130
|
+
options
|
|
131
|
+
});
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
return result;
|
|
130
136
|
}
|
|
131
137
|
export default function buildLocaleFromIntl() {
|
|
132
138
|
if (numProperties(bbn.dt.locales)) {
|