@bbn/bbn 2.0.46 → 2.0.48

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,10 +1,9 @@
1
1
  import extend from "../../fn/object/extend.js";
2
- import numProperties from "../../fn/object/numProperties.js";
3
2
  /**
4
3
  * Build a token pattern (YYYY, MM, DD, dddd, HH, II, SS, A, z) from Intl parts.
5
4
  * Uses Intl options to distinguish MMM vs MMMM, ddd vs dddd, etc.
6
5
  */
7
- function partsToPattern(parts, resolved, requestedOpts) {
6
+ const partsToPattern = (parts, resolved, requestedOpts) => {
8
7
  let pattern = '';
9
8
  const hourCycle = resolved.hourCycle;
10
9
  const hasDayPeriod = parts.some(p => p.type === 'dayPeriod');
@@ -115,7 +114,7 @@ function partsToPattern(parts, resolved, requestedOpts) {
115
114
  }
116
115
  }
117
116
  return pattern;
118
- }
117
+ };
119
118
  /**
120
119
  * Get a curated set of *common* date, time and datetime formats
121
120
  * for the given locale, without exploding into thousands of combos.
@@ -218,7 +217,7 @@ export function getCommonFormatsForLocale(lng) {
218
217
  return { date, time, datetime };
219
218
  }
220
219
  export default function buildLocaleFromIntl() {
221
- if (numProperties(bbn.dt.locales)) {
220
+ if (Object.keys(bbn.dt.locales).length) {
222
221
  return;
223
222
  }
224
223
  const langs = [bbn.env.lang, ...navigator.languages];
@@ -1,5 +1,33 @@
1
1
  import parse from './parse.js';
2
2
  import { getCommonFormatsForLocale } from './buildLocaleFromIntl.js';
3
+ // Common MySQL & native JS string formats – tried first in guessFormat
4
+ const MYSQL_AND_NATIVE_FORMATS = [
5
+ // --- MySQL / MariaDB classic ---
6
+ // Date
7
+ 'YYYY-MM-DD',
8
+ // Date + time
9
+ 'YYYY-MM-DD HH:II:SS.ms',
10
+ 'YYYY-MM-DD HH:II:SS',
11
+ // Time only
12
+ 'HH:II:SS',
13
+ // --- ISO 8601 / JS toISOString() ---
14
+ // 2025-11-22T14:30:00.123Z
15
+ 'YYYY-MM-DDTHH:II:SS.msZ',
16
+ // 2025-11-22T14:30:00Z
17
+ 'YYYY-MM-DDTHH:II:SSZ',
18
+ // 2025-11-22T14:30:00
19
+ 'YYYY-MM-DDTHH:II:SS',
20
+ // 2025-11-22T14:30
21
+ 'YYYY-MM-DDTHH:II',
22
+ // --- JS toUTCString() ---
23
+ // Tue, 29 Oct 2024 14:30:00 GMT
24
+ 'ddd[, ]DD MMM YYYY HH:II:SS[ GMT]',
25
+ // --- JS toString() (without the parenthesized TZ name) ---
26
+ // Tue Oct 29 2024 14:30:00 GMT+0200
27
+ 'ddd MMM DD YYYY HH:II:SS[ GMT]Z',
28
+ // Tue Oct 29 2024 14:30:00
29
+ 'ddd MMM DD YYYY HH:II:SS'
30
+ ];
3
31
  const isPureNumericDateFormat = (fmt) => {
4
32
  // Only Y/M/D tokens and literal separators, no time or AM/PM tokens
5
33
  if (/[HhI SAz]/.test(fmt)) {
@@ -15,6 +43,39 @@ const makeRelaxedNumericFormat = (fmt) => {
15
43
  // Relax DD -> D and MM -> M, but don't touch other tokens
16
44
  return fmt.replace(/DD/g, 'D').replace(/MM/g, 'M');
17
45
  };
46
+ /**
47
+ * If the format is a pure numeric date like D/M/YYYY or DD/MM/YYYY,
48
+ * and the input clearly uses 2-digit day and 2-digit month (22/11/2022),
49
+ * upgrade to DD/MM/YYYY.
50
+ */
51
+ const normalizeNumericDM = (fmt, input) => {
52
+ // Only touch "pure numeric date" patterns: D/M/YYYY, DD-MM-YY, etc.
53
+ // No time tokens, no text months, no weekdays.
54
+ if (/[HhI SAzM]{2,}|[A-Za-z]/.test(fmt.replace(/[DMY]/g, ''))) {
55
+ // If there are other letters than D/M/Y (like MMM, ddd), don't touch.
56
+ // (We only want simple numeric dates)
57
+ return fmt;
58
+ }
59
+ // Quick check: must contain D and M and Y
60
+ if (!fmt.includes('D') || !fmt.includes('M') || !fmt.includes('Y')) {
61
+ return fmt;
62
+ }
63
+ // Extract numeric chunks from the input: ["22", "11", "2022"] for "22/11/2022"
64
+ const nums = input.split(/\D+/).filter(Boolean);
65
+ if (nums.length < 3) {
66
+ return fmt;
67
+ }
68
+ const [dayStr, monthStr] = nums;
69
+ // Only upgrade if both day and month are exactly 2-digit
70
+ if (dayStr.length === 2 && monthStr.length === 2) {
71
+ // Upgrade first D-group to DD and first M-group to MM
72
+ let out = fmt;
73
+ out = out.replace(/D+/, 'DD');
74
+ out = out.replace(/M+/, 'MM');
75
+ return out;
76
+ }
77
+ return fmt;
78
+ };
18
79
  export default function guessFormat(input, formats, lng) {
19
80
  const str = input.trim();
20
81
  if (!str) {
@@ -25,7 +86,7 @@ export default function guessFormat(input, formats, lng) {
25
86
  // 1) Try strict format first
26
87
  try {
27
88
  parse(str, fmt);
28
- return fmt;
89
+ return normalizeNumericDM(fmt, str);
29
90
  }
30
91
  catch (_a) {
31
92
  // ignore, we'll maybe try a relaxed version
@@ -57,9 +118,29 @@ export default function guessFormat(input, formats, lng) {
57
118
  ? navigator.language
58
119
  : Intl.DateTimeFormat().resolvedOptions().locale);
59
120
  const common = getCommonFormatsForLocale(resolvedLocale);
60
- const candidates = [];
61
- // prioritize datetime patterns first, then date, then time
62
- candidates.push(...common.datetime.map(f => f.pattern), ...common.date.map(f => f.pattern), ...common.time.map(f => f.pattern));
121
+ // Avoid trivial duplicates
122
+ const seen = new Set();
123
+ const mysqlNativeCandidates = MYSQL_AND_NATIVE_FORMATS.filter(fmt => {
124
+ if (seen.has(fmt))
125
+ return false;
126
+ seen.add(fmt);
127
+ return true;
128
+ });
129
+ const localeCandidates = [
130
+ ...common.datetime.map(f => f.pattern),
131
+ ...common.date.map(f => f.pattern),
132
+ ...common.time.map(f => f.pattern)
133
+ ].filter(fmt => {
134
+ if (seen.has(fmt))
135
+ return false;
136
+ seen.add(fmt);
137
+ return true;
138
+ });
139
+ // MySQL & native JS patterns are checked FIRST
140
+ const candidates = [
141
+ ...mysqlNativeCandidates,
142
+ ...localeCandidates
143
+ ];
63
144
  const fmt = tryFormats(candidates);
64
145
  if (fmt) {
65
146
  return fmt;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@bbn/bbn",
3
- "version": "2.0.46",
3
+ "version": "2.0.48",
4
4
  "description": "Javascript toolkit",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",