@bbn/bbn 2.0.45 → 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.
- package/dist/bbn.js +1 -1
- package/dist/bbn.js.map +1 -1
- package/dist/dt/functions/guessFormat.js +31 -3
- package/package.json +1 -1
|
@@ -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;
|