@bbn/bbn 2.0.33 → 2.0.34

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.
@@ -21,7 +21,10 @@ type CommonFormats = {
21
21
  * - weekday / year / month / day
22
22
  * - hour / minute / second / timeZoneName
23
23
  *
24
- * No dateStyle/timeStyle is used, so we can safely combine date + time.
24
+ * Constraints:
25
+ * - no minutes/seconds if you don't have hours
26
+ * - no seconds if you don't have minutes
27
+ * - no timezone if you don't have time
25
28
  */
26
29
  export declare function getCommonFormatsForLocale(lng: string | string[]): CommonFormats;
27
30
  export default function buildLocaleFromIntl(): void;
@@ -60,7 +60,10 @@ function partsToPattern(parts, hourCycle) {
60
60
  * - weekday / year / month / day
61
61
  * - hour / minute / second / timeZoneName
62
62
  *
63
- * No dateStyle/timeStyle is used, so we can safely combine date + time.
63
+ * Constraints:
64
+ * - no minutes/seconds if you don't have hours
65
+ * - no seconds if you don't have minutes
66
+ * - no timezone if you don't have time
64
67
  */
65
68
  export function getCommonFormatsForLocale(lng) {
66
69
  // Fixed sample: 2 Jan 2000, 13:45:30 UTC
@@ -68,9 +71,11 @@ export function getCommonFormatsForLocale(lng) {
68
71
  const date = [];
69
72
  const time = [];
70
73
  const datetime = [];
71
- const seenDatePatterns = new Set();
72
- const seenTimePatterns = new Set();
73
- const seenDateTimePatterns = new Set();
74
+ // Dedupe by options (not just pattern), so we don't lose combinations like
75
+ // { day: "numeric", month: "short", year: "numeric" }.
76
+ const seenDateOptions = new Set();
77
+ const seenTimeOptions = new Set();
78
+ const seenDateTimeOptions = new Set();
74
79
  // ---- 1) DATE formats: combinations of weekday/year/month/day ----
75
80
  const weekdayOptions = [
76
81
  undefined,
@@ -106,19 +111,21 @@ export function getCommonFormatsForLocale(lng) {
106
111
  options.month = month;
107
112
  if (day)
108
113
  options.day = day;
114
+ const key = JSON.stringify(options);
115
+ if (seenDateOptions.has(key)) {
116
+ continue;
117
+ }
118
+ seenDateOptions.add(key);
109
119
  const fmt = new Intl.DateTimeFormat(lng, options);
110
120
  const parts = fmt.formatToParts(sample);
111
121
  const resolved = fmt.resolvedOptions();
112
122
  const pattern = partsToPattern(parts, resolved.hourCycle);
113
- if (!seenDatePatterns.has(pattern)) {
114
- seenDatePatterns.add(pattern);
115
- dateOptionsList.push(options);
116
- date.push({
117
- pattern,
118
- sample: fmt.format(sample),
119
- options
120
- });
121
- }
123
+ dateOptionsList.push(options);
124
+ date.push({
125
+ pattern,
126
+ sample: fmt.format(sample),
127
+ options
128
+ });
122
129
  }
123
130
  }
124
131
  }
@@ -144,8 +151,10 @@ export function getCommonFormatsForLocale(lng) {
144
151
  for (const minute of minuteOptions) {
145
152
  for (const second of secondOptions) {
146
153
  for (const tzName of tzNameOptions) {
147
- // Need at least hour to be a "time"
148
- if (!hour) {
154
+ // Constraints:
155
+ // - we always have hour (by design)
156
+ // - if we have second, we must have minute
157
+ if (second && !minute) {
149
158
  continue;
150
159
  }
151
160
  const options = { hour };
@@ -155,19 +164,21 @@ export function getCommonFormatsForLocale(lng) {
155
164
  options.second = second;
156
165
  if (tzName)
157
166
  options.timeZoneName = tzName;
167
+ const key = JSON.stringify(options);
168
+ if (seenTimeOptions.has(key)) {
169
+ continue;
170
+ }
171
+ seenTimeOptions.add(key);
158
172
  const fmt = new Intl.DateTimeFormat(lng, options);
159
173
  const parts = fmt.formatToParts(sample);
160
174
  const resolved = fmt.resolvedOptions();
161
175
  const pattern = partsToPattern(parts, resolved.hourCycle);
162
- if (!seenTimePatterns.has(pattern)) {
163
- seenTimePatterns.add(pattern);
164
- timeOptionsList.push(options);
165
- time.push({
166
- pattern,
167
- sample: fmt.format(sample),
168
- options
169
- });
170
- }
176
+ timeOptionsList.push(options);
177
+ time.push({
178
+ pattern,
179
+ sample: fmt.format(sample),
180
+ options
181
+ });
171
182
  }
172
183
  }
173
184
  }
@@ -176,18 +187,20 @@ export function getCommonFormatsForLocale(lng) {
176
187
  for (const dateOpts of dateOptionsList) {
177
188
  for (const timeOpts of timeOptionsList) {
178
189
  const options = Object.assign(Object.assign({}, dateOpts), timeOpts);
190
+ const key = JSON.stringify(options);
191
+ if (seenDateTimeOptions.has(key)) {
192
+ continue;
193
+ }
194
+ seenDateTimeOptions.add(key);
179
195
  const fmt = new Intl.DateTimeFormat(lng, options);
180
196
  const parts = fmt.formatToParts(sample);
181
197
  const resolved = fmt.resolvedOptions();
182
198
  const pattern = partsToPattern(parts, resolved.hourCycle);
183
- if (!seenDateTimePatterns.has(pattern)) {
184
- seenDateTimePatterns.add(pattern);
185
- datetime.push({
186
- pattern,
187
- sample: fmt.format(sample),
188
- options
189
- });
190
- }
199
+ datetime.push({
200
+ pattern,
201
+ sample: fmt.format(sample),
202
+ options
203
+ });
191
204
  }
192
205
  }
193
206
  return { date, time, datetime };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@bbn/bbn",
3
- "version": "2.0.33",
3
+ "version": "2.0.34",
4
4
  "description": "Javascript toolkit",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",