@bbn/bbn 2.0.32 → 2.0.33

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 +1,28 @@
1
+ type CommonFormats = {
2
+ date: Array<{
3
+ pattern: string;
4
+ sample: string;
5
+ options: Intl.DateTimeFormatOptions;
6
+ }>;
7
+ time: Array<{
8
+ pattern: string;
9
+ sample: string;
10
+ options: Intl.DateTimeFormatOptions;
11
+ }>;
12
+ datetime: Array<{
13
+ pattern: string;
14
+ sample: string;
15
+ options: Intl.DateTimeFormatOptions;
16
+ }>;
17
+ };
18
+ /**
19
+ * Enumerate common date, time and datetime formats for a locale, by iterating
20
+ * over combinations of:
21
+ * - weekday / year / month / day
22
+ * - hour / minute / second / timeZoneName
23
+ *
24
+ * No dateStyle/timeStyle is used, so we can safely combine date + time.
25
+ */
26
+ export declare function getCommonFormatsForLocale(lng: string | string[]): CommonFormats;
1
27
  export default function buildLocaleFromIntl(): void;
28
+ export {};
@@ -1,7 +1,7 @@
1
1
  import extend from "../../fn/object/extend.js";
2
2
  import numProperties from "../../fn/object/numProperties.js";
3
3
  /**
4
- * Build a token pattern like "DD/MM/YYYY HH:II:SS" from Intl.DateTimeFormat parts.
4
+ * Build a token pattern (YYYY, MM, DD, dddd, HH, II, SS, A, z) from Intl parts.
5
5
  */
6
6
  function partsToPattern(parts, hourCycle) {
7
7
  let pattern = '';
@@ -55,21 +55,23 @@ function partsToPattern(parts, hourCycle) {
55
55
  return pattern;
56
56
  }
57
57
  /**
58
- * Returns common date / time / datetime formats for a locale, including
59
- * all reasonable combinations of weekday, year, month and day.
58
+ * Enumerate common date, time and datetime formats for a locale, by iterating
59
+ * over combinations of:
60
+ * - weekday / year / month / day
61
+ * - hour / minute / second / timeZoneName
62
+ *
63
+ * No dateStyle/timeStyle is used, so we can safely combine date + time.
60
64
  */
61
- const getCommonFormatsForLocale = function (lng) {
62
- const timeStyles = ['full', 'long', 'medium', 'short'];
65
+ export function getCommonFormatsForLocale(lng) {
63
66
  // Fixed sample: 2 Jan 2000, 13:45:30 UTC
64
67
  const sample = new Date(Date.UTC(2000, 0, 2, 13, 45, 30));
65
68
  const date = [];
66
69
  const time = [];
67
70
  const datetime = [];
68
- // To avoid duplicates
69
71
  const seenDatePatterns = new Set();
70
72
  const seenTimePatterns = new Set();
71
73
  const seenDateTimePatterns = new Set();
72
- // --- 1) DATE formats via combinations of weekday/year/month/day ---
74
+ // ---- 1) DATE formats: combinations of weekday/year/month/day ----
73
75
  const weekdayOptions = [
74
76
  undefined,
75
77
  'short',
@@ -104,14 +106,13 @@ const getCommonFormatsForLocale = function (lng) {
104
106
  options.month = month;
105
107
  if (day)
106
108
  options.day = day;
107
- // Build formatter and pattern
108
109
  const fmt = new Intl.DateTimeFormat(lng, options);
109
110
  const parts = fmt.formatToParts(sample);
110
111
  const resolved = fmt.resolvedOptions();
111
112
  const pattern = partsToPattern(parts, resolved.hourCycle);
112
113
  if (!seenDatePatterns.has(pattern)) {
113
114
  seenDatePatterns.add(pattern);
114
- dateOptionsList.push(options); // keep for datetime combos later
115
+ dateOptionsList.push(options);
115
116
  date.push({
116
117
  pattern,
117
118
  sample: fmt.format(sample),
@@ -122,27 +123,59 @@ const getCommonFormatsForLocale = function (lng) {
122
123
  }
123
124
  }
124
125
  }
125
- // --- 2) TIME formats via timeStyle only ---
126
- for (const ts of timeStyles) {
127
- const options = { timeStyle: ts };
128
- const fmt = new Intl.DateTimeFormat(lng, options);
129
- const parts = fmt.formatToParts(sample);
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,
137
- sample: fmt.format(sample),
138
- options
139
- });
126
+ const hourOptions = ['numeric', '2-digit'];
127
+ const minuteOptions = [
128
+ undefined,
129
+ 'numeric',
130
+ '2-digit'
131
+ ];
132
+ const secondOptions = [
133
+ undefined,
134
+ 'numeric',
135
+ '2-digit'
136
+ ];
137
+ const tzNameOptions = [
138
+ undefined,
139
+ 'short',
140
+ 'long'
141
+ ];
142
+ const timeOptionsList = [];
143
+ for (const hour of hourOptions) {
144
+ for (const minute of minuteOptions) {
145
+ for (const second of secondOptions) {
146
+ for (const tzName of tzNameOptions) {
147
+ // Need at least hour to be a "time"
148
+ if (!hour) {
149
+ continue;
150
+ }
151
+ const options = { hour };
152
+ if (minute)
153
+ options.minute = minute;
154
+ if (second)
155
+ options.second = second;
156
+ if (tzName)
157
+ options.timeZoneName = tzName;
158
+ const fmt = new Intl.DateTimeFormat(lng, options);
159
+ const parts = fmt.formatToParts(sample);
160
+ const resolved = fmt.resolvedOptions();
161
+ 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
+ }
171
+ }
172
+ }
140
173
  }
141
174
  }
142
- // --- 3) DATETIME formats: each date option × each timeStyle ---
175
+ // ---- 3) DATETIME formats: each dateOption × each timeOption ----
143
176
  for (const dateOpts of dateOptionsList) {
144
- for (const ts of timeStyles) {
145
- const options = Object.assign(Object.assign({}, dateOpts), { timeStyle: ts });
177
+ for (const timeOpts of timeOptionsList) {
178
+ const options = Object.assign(Object.assign({}, dateOpts), timeOpts);
146
179
  const fmt = new Intl.DateTimeFormat(lng, options);
147
180
  const parts = fmt.formatToParts(sample);
148
181
  const resolved = fmt.resolvedOptions();
@@ -158,7 +191,7 @@ const getCommonFormatsForLocale = function (lng) {
158
191
  }
159
192
  }
160
193
  return { date, time, datetime };
161
- };
194
+ }
162
195
  export default function buildLocaleFromIntl() {
163
196
  if (numProperties(bbn.dt.locales)) {
164
197
  return;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@bbn/bbn",
3
- "version": "2.0.32",
3
+ "version": "2.0.33",
4
4
  "description": "Javascript toolkit",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",