@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.
@@ -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
- else
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
- pattern += is12h ? (p.value.length === 2 ? 'hh' : 'h') : (p.value.length === 2 ? 'HH' : 'H');
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 all common date/time/datetime formats + weekday formats for a given locale.
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 getCommonFormatsForLocale(lng) {
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 result = {
57
- date: [],
58
- time: [],
59
- datetime: [],
60
- dateWithWeekday: [],
61
- datetimeWithWeekday: []
62
- };
63
- // --- Date only ---
64
- for (const ds of dateStyles) {
65
- const options = { dateStyle: ds };
66
- const fmt = new Intl.DateTimeFormat(lng, options);
67
- const parts = fmt.formatToParts(sample);
68
- result.date.push({
69
- style: ds,
70
- pattern: partsToPattern(parts, fmt.resolvedOptions().hourCycle),
71
- sample: fmt.format(sample),
72
- options
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
- // --- Time only ---
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
- result.time.push({
81
- style: ts,
82
- pattern: partsToPattern(parts, fmt.resolvedOptions().hourCycle),
83
- sample: fmt.format(sample),
84
- options
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
- // --- 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 };
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
- 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),
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 result;
136
- }
160
+ return { date, time, datetime };
161
+ };
137
162
  export default function buildLocaleFromIntl() {
138
163
  if (numProperties(bbn.dt.locales)) {
139
164
  return;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@bbn/bbn",
3
- "version": "2.0.31",
3
+ "version": "2.0.32",
4
4
  "description": "Javascript toolkit",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",