@bbn/bbn 2.0.37 → 2.0.39

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.
@@ -8,19 +8,41 @@ function partsToPattern(parts, hourCycle, opts) {
8
8
  let pattern = '';
9
9
  const hasDayPeriod = parts.some(p => p.type === 'dayPeriod');
10
10
  const is12h = hasDayPeriod || hourCycle === 'h12' || hourCycle === 'h11';
11
+ // ---- detect "all numeric date" ----
12
+ const yearIsNumeric = opts.year === 'numeric' || opts.year === '2-digit';
13
+ const monthIsNumeric = opts.month === 'numeric' || opts.month === '2-digit';
14
+ const dayIsNumeric = opts.day === 'numeric' || opts.day === '2-digit';
15
+ const hasYear = !!opts.year;
16
+ const hasMonth = !!opts.month;
17
+ const hasDay = !!opts.day;
18
+ const hasWeekday = !!opts.weekday;
19
+ const hasTextMonth = opts.month === 'short' || opts.month === 'long';
20
+ const isAllNumericDate = hasYear && hasMonth && hasDay &&
21
+ yearIsNumeric && monthIsNumeric && dayIsNumeric &&
22
+ !hasWeekday && !hasTextMonth;
11
23
  for (const p of parts) {
12
24
  switch (p.type) {
13
25
  case 'year':
14
- pattern += 'YYYY';
26
+ if (opts.year === '2-digit') {
27
+ pattern += 'YY';
28
+ }
29
+ else {
30
+ pattern += 'YYYY';
31
+ }
15
32
  break;
16
33
  case 'month':
17
- if (opts.month === 'short') {
34
+ if (isAllNumericDate) {
35
+ // normalize to single M when date is fully numeric
36
+ pattern += 'M';
37
+ }
38
+ else if (opts.month === 'short') {
18
39
  pattern += 'MMM';
19
40
  }
20
41
  else if (opts.month === 'long') {
21
42
  pattern += 'MMMM';
22
43
  }
23
44
  else if (opts.month === 'numeric' || opts.month === '2-digit') {
45
+ // non "all numeric" case (e.g., month+year or month+day only)
24
46
  pattern += /^\d{2}$/.test(p.value) ? 'MM' : 'M';
25
47
  }
26
48
  else {
@@ -34,7 +56,13 @@ function partsToPattern(parts, hourCycle, opts) {
34
56
  }
35
57
  break;
36
58
  case 'day':
37
- pattern += p.value.length === 2 ? 'DD' : 'D';
59
+ if (isAllNumericDate) {
60
+ // normalize to single D when date is fully numeric
61
+ pattern += 'D';
62
+ }
63
+ else {
64
+ pattern += p.value.length === 2 ? 'DD' : 'D';
65
+ }
38
66
  break;
39
67
  case 'weekday':
40
68
  if (opts.weekday === 'short' || opts.weekday === 'narrow') {
@@ -67,7 +95,14 @@ function partsToPattern(parts, hourCycle, opts) {
67
95
  case 'timeZoneName':
68
96
  pattern += 'z';
69
97
  break;
70
- case 'literal':
98
+ case 'literal': {
99
+ // Wrap literals in [ ... ] so your parser doesn't confuse them
100
+ if (p.value.length && ![' ', ',', '/', '-', ':', '.'].includes(p.value)) {
101
+ const v = p.value.replace(/]/g, '\\]');
102
+ pattern += `[${v}]`;
103
+ }
104
+ break;
105
+ }
71
106
  default:
72
107
  pattern += p.value;
73
108
  break;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@bbn/bbn",
3
- "version": "2.0.37",
3
+ "version": "2.0.39",
4
4
  "description": "Javascript toolkit",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",