@bbn/bbn 2.0.45 → 2.0.47
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 +65 -4
- package/package.json +1 -1
|
@@ -1,19 +1,80 @@
|
|
|
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
|
+
};
|
|
18
|
+
/**
|
|
19
|
+
* If the format is a pure numeric date like D/M/YYYY or DD/MM/YYYY,
|
|
20
|
+
* and the input clearly uses 2-digit day and 2-digit month (22/11/2022),
|
|
21
|
+
* upgrade to DD/MM/YYYY.
|
|
22
|
+
*/
|
|
23
|
+
const normalizeNumericDM = (fmt, input) => {
|
|
24
|
+
// Only touch "pure numeric date" patterns: D/M/YYYY, DD-MM-YY, etc.
|
|
25
|
+
// No time tokens, no text months, no weekdays.
|
|
26
|
+
if (/[HhI SAzM]{2,}|[A-Za-z]/.test(fmt.replace(/[DMY]/g, ''))) {
|
|
27
|
+
// If there are other letters than D/M/Y (like MMM, ddd), don't touch.
|
|
28
|
+
// (We only want simple numeric dates)
|
|
29
|
+
return fmt;
|
|
30
|
+
}
|
|
31
|
+
// Quick check: must contain D and M and Y
|
|
32
|
+
if (!fmt.includes('D') || !fmt.includes('M') || !fmt.includes('Y')) {
|
|
33
|
+
return fmt;
|
|
34
|
+
}
|
|
35
|
+
// Extract numeric chunks from the input: ["22", "11", "2022"] for "22/11/2022"
|
|
36
|
+
const nums = input.split(/\D+/).filter(Boolean);
|
|
37
|
+
if (nums.length < 3) {
|
|
38
|
+
return fmt;
|
|
39
|
+
}
|
|
40
|
+
const [dayStr, monthStr] = nums;
|
|
41
|
+
// Only upgrade if both day and month are exactly 2-digit
|
|
42
|
+
if (dayStr.length === 2 && monthStr.length === 2) {
|
|
43
|
+
// Upgrade first D-group to DD and first M-group to MM
|
|
44
|
+
let out = fmt;
|
|
45
|
+
out = out.replace(/D+/, 'DD');
|
|
46
|
+
out = out.replace(/M+/, 'MM');
|
|
47
|
+
return out;
|
|
48
|
+
}
|
|
49
|
+
return fmt;
|
|
50
|
+
};
|
|
3
51
|
export default function guessFormat(input, formats, lng) {
|
|
4
52
|
const str = input.trim();
|
|
5
53
|
if (!str) {
|
|
6
54
|
return null;
|
|
7
55
|
}
|
|
8
|
-
// helper: try a list of formats with your parse()
|
|
9
56
|
const tryFormats = (fmts) => {
|
|
10
57
|
for (const fmt of fmts) {
|
|
58
|
+
// 1) Try strict format first
|
|
11
59
|
try {
|
|
12
|
-
parse(str, fmt);
|
|
13
|
-
return fmt;
|
|
60
|
+
parse(str, fmt);
|
|
61
|
+
return normalizeNumericDM(fmt, str);
|
|
14
62
|
}
|
|
15
63
|
catch (_a) {
|
|
16
|
-
// ignore
|
|
64
|
+
// ignore, we'll maybe try a relaxed version
|
|
65
|
+
}
|
|
66
|
+
// 2) If it's a pure numeric date pattern, try a relaxed version too
|
|
67
|
+
if (isPureNumericDateFormat(fmt)) {
|
|
68
|
+
const relaxed = makeRelaxedNumericFormat(fmt);
|
|
69
|
+
if (relaxed !== fmt) {
|
|
70
|
+
try {
|
|
71
|
+
parse(str, relaxed);
|
|
72
|
+
return relaxed;
|
|
73
|
+
}
|
|
74
|
+
catch (_b) {
|
|
75
|
+
// still nothing, move on
|
|
76
|
+
}
|
|
77
|
+
}
|
|
17
78
|
}
|
|
18
79
|
}
|
|
19
80
|
return null;
|