@aemforms/af-formatters 0.22.158 → 0.22.161
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/README.md +14 -0
- package/esm/afb-formatters.js +9 -0
- package/lib/date/DateParser.js +19 -0
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -23,6 +23,20 @@ format(1234, 'en-US', 'date|MMM dd, y') //outputs Oct 10, 2022
|
|
|
23
23
|
|
|
24
24
|
```
|
|
25
25
|
|
|
26
|
+
## Testing Local Changes
|
|
27
|
+
|
|
28
|
+
```bash
|
|
29
|
+
# Build the package
|
|
30
|
+
npm run build
|
|
31
|
+
|
|
32
|
+
# Link it for local testing
|
|
33
|
+
npm link
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
In your project that uses this package:
|
|
37
|
+
```bash
|
|
38
|
+
npm link @aemforms/af-formatters
|
|
39
|
+
```
|
|
26
40
|
|
|
27
41
|
## License
|
|
28
42
|
|
package/esm/afb-formatters.js
CHANGED
|
@@ -436,6 +436,12 @@ function parseDate(dateString, language, skeleton, timeZone, bUseUTC = false) {
|
|
|
436
436
|
const [element, func] = lookups[index];
|
|
437
437
|
dateObj[element] = func(m, dateObj);
|
|
438
438
|
});
|
|
439
|
+
const isValidDate = (date, expectedDateObj, calendar) => {
|
|
440
|
+
if (calendar !== 'gregory') return true;
|
|
441
|
+
return date.getMonth() === expectedDateObj.month &&
|
|
442
|
+
date.getDate() === expectedDateObj.day &&
|
|
443
|
+
date.getFullYear() === expectedDateObj.year;
|
|
444
|
+
};
|
|
439
445
|
if (hourCycle === 'h24' && dateObj.hour === 24) dateObj.hour = 0;
|
|
440
446
|
if (hourCycle === 'h12' && dateObj.hour === 12) dateObj.hour = 0;
|
|
441
447
|
if (_bUseUTC) {
|
|
@@ -465,6 +471,9 @@ function parseDate(dateString, language, skeleton, timeZone, bUseUTC = false) {
|
|
|
465
471
|
if (_setFullYear) {
|
|
466
472
|
jsDate.setFullYear(dateObj.year);
|
|
467
473
|
}
|
|
474
|
+
if (!isValidDate(jsDate, dateObj, calendar)) {
|
|
475
|
+
return null;
|
|
476
|
+
}
|
|
468
477
|
return timeZone == null ? jsDate : adjustTimeZone(jsDate, timeZone);
|
|
469
478
|
}
|
|
470
479
|
function parseDefaultDate(dateString, language, bUseUTC) {
|
package/lib/date/DateParser.js
CHANGED
|
@@ -503,6 +503,20 @@ function parseDate(dateString, language, skeleton, timeZone) {
|
|
|
503
503
|
const [element, func] = lookups[index];
|
|
504
504
|
dateObj[element] = func(m, dateObj);
|
|
505
505
|
});
|
|
506
|
+
/**
|
|
507
|
+
* Validates that JavaScript's Date constructor didn't auto-correct the date
|
|
508
|
+
* (e.g., Feb 30 -> Mar 2, Month 13 -> Jan of next year)
|
|
509
|
+
* @param date The JavaScript Date object to validate
|
|
510
|
+
* @param expectedDateObj The expected date components {year, month, day}
|
|
511
|
+
* @param calendar The calendar type
|
|
512
|
+
* @returns true if date is valid, false if auto-correction occurred
|
|
513
|
+
*/
|
|
514
|
+
|
|
515
|
+
const isValidDate = (date, expectedDateObj, calendar) => {
|
|
516
|
+
if (calendar !== 'gregory') return true;
|
|
517
|
+
return date.getMonth() === expectedDateObj.month && date.getDate() === expectedDateObj.day && date.getFullYear() === expectedDateObj.year;
|
|
518
|
+
};
|
|
519
|
+
|
|
506
520
|
if (hourCycle === 'h24' && dateObj.hour === 24) dateObj.hour = 0;
|
|
507
521
|
if (hourCycle === 'h12' && dateObj.hour === 12) dateObj.hour = 0;
|
|
508
522
|
|
|
@@ -520,6 +534,11 @@ function parseDate(dateString, language, skeleton, timeZone) {
|
|
|
520
534
|
|
|
521
535
|
if (_setFullYear) {
|
|
522
536
|
jsDate.setFullYear(dateObj.year);
|
|
537
|
+
} // Verify the date wasn't auto-corrected by JavaScript
|
|
538
|
+
|
|
539
|
+
|
|
540
|
+
if (!isValidDate(jsDate, dateObj, calendar)) {
|
|
541
|
+
return null;
|
|
523
542
|
}
|
|
524
543
|
|
|
525
544
|
return timeZone == null ? jsDate : adjustTimeZone(jsDate, timeZone);
|