@bbn/bbn 2.0.31 → 2.0.32
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/dist/bbn.js +1 -1
- package/dist/bbn.js.map +1 -1
- package/dist/dt/functions/buildLocaleFromIntl.js +96 -71
- package/package.json +1 -1
|
@@ -1,5 +1,8 @@
|
|
|
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
|
+
*/
|
|
3
6
|
function partsToPattern(parts, hourCycle) {
|
|
4
7
|
let pattern = '';
|
|
5
8
|
const hasDayPeriod = parts.some(p => p.type === 'dayPeriod');
|
|
@@ -10,10 +13,12 @@ function partsToPattern(parts, hourCycle) {
|
|
|
10
13
|
pattern += 'YYYY';
|
|
11
14
|
break;
|
|
12
15
|
case 'month':
|
|
13
|
-
if (/^\d+$/.test(p.value))
|
|
16
|
+
if (/^\d+$/.test(p.value)) {
|
|
14
17
|
pattern += p.value.length === 2 ? 'MM' : 'M';
|
|
15
|
-
|
|
18
|
+
}
|
|
19
|
+
else {
|
|
16
20
|
pattern += p.value.length > 3 ? 'MMMM' : 'MMM';
|
|
21
|
+
}
|
|
17
22
|
break;
|
|
18
23
|
case 'day':
|
|
19
24
|
pattern += p.value.length === 2 ? 'DD' : 'D';
|
|
@@ -22,7 +27,12 @@ function partsToPattern(parts, hourCycle) {
|
|
|
22
27
|
pattern += p.value.length > 3 ? 'dddd' : 'ddd';
|
|
23
28
|
break;
|
|
24
29
|
case 'hour':
|
|
25
|
-
|
|
30
|
+
if (is12h) {
|
|
31
|
+
pattern += p.value.length === 2 ? 'hh' : 'h';
|
|
32
|
+
}
|
|
33
|
+
else {
|
|
34
|
+
pattern += p.value.length === 2 ? 'HH' : 'H';
|
|
35
|
+
}
|
|
26
36
|
break;
|
|
27
37
|
case 'minute':
|
|
28
38
|
pattern += 'II';
|
|
@@ -37,8 +47,6 @@ function partsToPattern(parts, hourCycle) {
|
|
|
37
47
|
pattern += 'z';
|
|
38
48
|
break;
|
|
39
49
|
case 'literal':
|
|
40
|
-
pattern += p.value;
|
|
41
|
-
break;
|
|
42
50
|
default:
|
|
43
51
|
pattern += p.value;
|
|
44
52
|
break;
|
|
@@ -47,93 +55,110 @@ function partsToPattern(parts, hourCycle) {
|
|
|
47
55
|
return pattern;
|
|
48
56
|
}
|
|
49
57
|
/**
|
|
50
|
-
* Returns
|
|
58
|
+
* Returns common date / time / datetime formats for a locale, including
|
|
59
|
+
* all reasonable combinations of weekday, year, month and day.
|
|
51
60
|
*/
|
|
52
|
-
function
|
|
53
|
-
const dateStyles = ['full', 'long', 'medium', 'short'];
|
|
61
|
+
const getCommonFormatsForLocale = function (lng) {
|
|
54
62
|
const timeStyles = ['full', 'long', 'medium', 'short'];
|
|
63
|
+
// Fixed sample: 2 Jan 2000, 13:45:30 UTC
|
|
55
64
|
const sample = new Date(Date.UTC(2000, 0, 2, 13, 45, 30));
|
|
56
|
-
const
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
// ---
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
65
|
+
const date = [];
|
|
66
|
+
const time = [];
|
|
67
|
+
const datetime = [];
|
|
68
|
+
// To avoid duplicates
|
|
69
|
+
const seenDatePatterns = new Set();
|
|
70
|
+
const seenTimePatterns = new Set();
|
|
71
|
+
const seenDateTimePatterns = new Set();
|
|
72
|
+
// --- 1) DATE formats via combinations of weekday/year/month/day ---
|
|
73
|
+
const weekdayOptions = [
|
|
74
|
+
undefined,
|
|
75
|
+
'short',
|
|
76
|
+
'long'
|
|
77
|
+
];
|
|
78
|
+
const yearOptions = [
|
|
79
|
+
undefined,
|
|
80
|
+
'numeric',
|
|
81
|
+
'2-digit'
|
|
82
|
+
];
|
|
83
|
+
const monthOptions = [undefined, 'numeric', '2-digit', 'short', 'long'];
|
|
84
|
+
const dayOptions = [
|
|
85
|
+
undefined,
|
|
86
|
+
'numeric',
|
|
87
|
+
'2-digit'
|
|
88
|
+
];
|
|
89
|
+
const dateOptionsList = [];
|
|
90
|
+
for (const weekday of weekdayOptions) {
|
|
91
|
+
for (const year of yearOptions) {
|
|
92
|
+
for (const month of monthOptions) {
|
|
93
|
+
for (const day of dayOptions) {
|
|
94
|
+
// Skip combos with no actual date fields
|
|
95
|
+
if (!year && !month && !day) {
|
|
96
|
+
continue;
|
|
97
|
+
}
|
|
98
|
+
const options = {};
|
|
99
|
+
if (weekday)
|
|
100
|
+
options.weekday = weekday;
|
|
101
|
+
if (year)
|
|
102
|
+
options.year = year;
|
|
103
|
+
if (month)
|
|
104
|
+
options.month = month;
|
|
105
|
+
if (day)
|
|
106
|
+
options.day = day;
|
|
107
|
+
// Build formatter and pattern
|
|
108
|
+
const fmt = new Intl.DateTimeFormat(lng, options);
|
|
109
|
+
const parts = fmt.formatToParts(sample);
|
|
110
|
+
const resolved = fmt.resolvedOptions();
|
|
111
|
+
const pattern = partsToPattern(parts, resolved.hourCycle);
|
|
112
|
+
if (!seenDatePatterns.has(pattern)) {
|
|
113
|
+
seenDatePatterns.add(pattern);
|
|
114
|
+
dateOptionsList.push(options); // keep for datetime combos later
|
|
115
|
+
date.push({
|
|
116
|
+
pattern,
|
|
117
|
+
sample: fmt.format(sample),
|
|
118
|
+
options
|
|
119
|
+
});
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
}
|
|
74
124
|
}
|
|
75
|
-
// ---
|
|
125
|
+
// --- 2) TIME formats via timeStyle only ---
|
|
76
126
|
for (const ts of timeStyles) {
|
|
77
127
|
const options = { timeStyle: ts };
|
|
78
128
|
const fmt = new Intl.DateTimeFormat(lng, options);
|
|
79
129
|
const parts = fmt.formatToParts(sample);
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
// --- Date + Time ---
|
|
88
|
-
for (const ds of dateStyles) {
|
|
89
|
-
for (const ts of timeStyles) {
|
|
90
|
-
const options = { dateStyle: ds, timeStyle: ts };
|
|
91
|
-
const fmt = new Intl.DateTimeFormat(lng, options);
|
|
92
|
-
const parts = fmt.formatToParts(sample);
|
|
93
|
-
result.datetime.push({
|
|
94
|
-
dateStyle: ds,
|
|
95
|
-
timeStyle: ts,
|
|
96
|
-
pattern: partsToPattern(parts, fmt.resolvedOptions().hourCycle),
|
|
130
|
+
const resolved = fmt.resolvedOptions();
|
|
131
|
+
const pattern = partsToPattern(parts, resolved.hourCycle);
|
|
132
|
+
if (!seenTimePatterns.has(pattern)) {
|
|
133
|
+
seenTimePatterns.add(pattern);
|
|
134
|
+
time.push({
|
|
135
|
+
style: ts,
|
|
136
|
+
pattern,
|
|
97
137
|
sample: fmt.format(sample),
|
|
98
138
|
options
|
|
99
139
|
});
|
|
100
140
|
}
|
|
101
141
|
}
|
|
102
|
-
// ---
|
|
103
|
-
for (const
|
|
104
|
-
for (const
|
|
105
|
-
const options = {
|
|
142
|
+
// --- 3) DATETIME formats: each date option × each timeStyle ---
|
|
143
|
+
for (const dateOpts of dateOptionsList) {
|
|
144
|
+
for (const ts of timeStyles) {
|
|
145
|
+
const options = Object.assign(Object.assign({}, dateOpts), { timeStyle: ts });
|
|
106
146
|
const fmt = new Intl.DateTimeFormat(lng, options);
|
|
107
147
|
const parts = fmt.formatToParts(sample);
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
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),
|
|
148
|
+
const resolved = fmt.resolvedOptions();
|
|
149
|
+
const pattern = partsToPattern(parts, resolved.hourCycle);
|
|
150
|
+
if (!seenDateTimePatterns.has(pattern)) {
|
|
151
|
+
seenDateTimePatterns.add(pattern);
|
|
152
|
+
datetime.push({
|
|
153
|
+
pattern,
|
|
129
154
|
sample: fmt.format(sample),
|
|
130
155
|
options
|
|
131
156
|
});
|
|
132
157
|
}
|
|
133
158
|
}
|
|
134
159
|
}
|
|
135
|
-
return
|
|
136
|
-
}
|
|
160
|
+
return { date, time, datetime };
|
|
161
|
+
};
|
|
137
162
|
export default function buildLocaleFromIntl() {
|
|
138
163
|
if (numProperties(bbn.dt.locales)) {
|
|
139
164
|
return;
|