@bbn/bbn 2.0.44 → 2.0.46
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.
|
@@ -91,10 +91,22 @@ function partsToPattern(parts, resolved, requestedOpts) {
|
|
|
91
91
|
pattern += 'z';
|
|
92
92
|
break;
|
|
93
93
|
case 'literal': {
|
|
94
|
-
if (p.value
|
|
94
|
+
if (!p.value) {
|
|
95
|
+
break;
|
|
96
|
+
}
|
|
97
|
+
// If the literal contains any letter (ASCII or basic Latin-1),
|
|
98
|
+
// wrap it in [ ... ] so it can't be confused with tokens.
|
|
99
|
+
// Otherwise (spaces, /, -, :, commas, etc.) keep it raw.
|
|
100
|
+
const hasLetter = /[A-Za-zÀ-ÖØ-öø-ÿ]/.test(p.value);
|
|
101
|
+
if (hasLetter) {
|
|
102
|
+
// Escape ']' inside the bracketed literal
|
|
95
103
|
const v = p.value.replace(/]/g, '\\]');
|
|
96
104
|
pattern += `[${v}]`;
|
|
97
105
|
}
|
|
106
|
+
else {
|
|
107
|
+
// Non-problematic punctuation/whitespace: just append as-is
|
|
108
|
+
pattern += p.value;
|
|
109
|
+
}
|
|
98
110
|
break;
|
|
99
111
|
}
|
|
100
112
|
default:
|
|
@@ -1,19 +1,47 @@
|
|
|
1
1
|
import parse from './parse.js';
|
|
2
2
|
import { getCommonFormatsForLocale } from './buildLocaleFromIntl.js';
|
|
3
|
+
const isPureNumericDateFormat = (fmt) => {
|
|
4
|
+
// Only Y/M/D tokens and literal separators, no time or AM/PM tokens
|
|
5
|
+
if (/[HhI SAz]/.test(fmt)) {
|
|
6
|
+
return false;
|
|
7
|
+
}
|
|
8
|
+
// Must have year and month and day tokens
|
|
9
|
+
if (!/[Y]/.test(fmt) || !/[M]/.test(fmt) || !/[D]/.test(fmt)) {
|
|
10
|
+
return false;
|
|
11
|
+
}
|
|
12
|
+
return true;
|
|
13
|
+
};
|
|
14
|
+
const makeRelaxedNumericFormat = (fmt) => {
|
|
15
|
+
// Relax DD -> D and MM -> M, but don't touch other tokens
|
|
16
|
+
return fmt.replace(/DD/g, 'D').replace(/MM/g, 'M');
|
|
17
|
+
};
|
|
3
18
|
export default function guessFormat(input, formats, lng) {
|
|
4
19
|
const str = input.trim();
|
|
5
20
|
if (!str) {
|
|
6
21
|
return null;
|
|
7
22
|
}
|
|
8
|
-
// helper: try a list of formats with your parse()
|
|
9
23
|
const tryFormats = (fmts) => {
|
|
10
24
|
for (const fmt of fmts) {
|
|
25
|
+
// 1) Try strict format first
|
|
11
26
|
try {
|
|
12
|
-
parse(str, fmt);
|
|
27
|
+
parse(str, fmt);
|
|
13
28
|
return fmt;
|
|
14
29
|
}
|
|
15
30
|
catch (_a) {
|
|
16
|
-
// ignore
|
|
31
|
+
// ignore, we'll maybe try a relaxed version
|
|
32
|
+
}
|
|
33
|
+
// 2) If it's a pure numeric date pattern, try a relaxed version too
|
|
34
|
+
if (isPureNumericDateFormat(fmt)) {
|
|
35
|
+
const relaxed = makeRelaxedNumericFormat(fmt);
|
|
36
|
+
if (relaxed !== fmt) {
|
|
37
|
+
try {
|
|
38
|
+
parse(str, relaxed);
|
|
39
|
+
return relaxed;
|
|
40
|
+
}
|
|
41
|
+
catch (_b) {
|
|
42
|
+
// still nothing, move on
|
|
43
|
+
}
|
|
44
|
+
}
|
|
17
45
|
}
|
|
18
46
|
}
|
|
19
47
|
return null;
|