Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(isDate): fix thrown error on certain invalid cases #2443

Merged
merged 2 commits into from
Aug 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/lib/isDate.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ function isValidFormat(format) {

function zip(date, format) {
const zippedArr = [],
len = Math.min(date.length, format.length);
len = Math.max(date.length, format.length);

for (let i = 0; i < len; i++) {
zippedArr.push([date[i], format[i]]);
Expand Down Expand Up @@ -40,7 +40,7 @@ export default function isDate(input, options) {
const dateObj = {};

for (const [dateWord, formatWord] of dateAndFormat) {
if (dateWord.length !== formatWord.length) {
if (!dateWord || !formatWord || dateWord.length !== formatWord.length) {
return false;
}

Expand Down
118 changes: 118 additions & 0 deletions test/validators.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -13680,6 +13680,7 @@ describe('Validators', () => {
new Date([2014, 2, 15]),
new Date('2014-03-15'),
'2020/02/29',
'2020-02-19',
],
invalid: [
'',
Expand All @@ -13695,6 +13696,18 @@ describe('Validators', () => {
'2020/03-15', // mixed delimiter
'-2020-04-19',
'-2023/05/24',
'abc-2023/05/24',
'2024',
'2024-',
'2024-05',
'2024-05-',
'2024-05-01-',
'2024-05-01-abc',
'2024/',
'2024/05',
'2024/05/',
'2024/05/01/',
'2024/05/01/abc',
],
});
test({
Expand All @@ -13710,6 +13723,21 @@ describe('Validators', () => {
'15/7/02',
'15-7-02',
'15-07/2002',
'2024',
'2024-',
'2024-05',
'2024-05-',
'2024-05-01-',
'2024-05-01-abc',
'2024/',
'2024/05',
'2024/05/',
'2024/05/01/',
'2024/05/01/abc',
'15/',
'15/07',
'15/07/',
'15/07/2024/',
],
});
test({
Expand All @@ -13725,6 +13753,21 @@ describe('Validators', () => {
'15/7/02',
'15-7-02',
'15-07/2002',
'2024',
'2024-',
'2024-05',
'2024-05-',
'2024-05-01-',
'2024-05-01-abc',
'2024/',
'2024/05',
'2024/05/',
'2024/05/01/',
'2024/05/01/abc',
'15/',
'15/07',
'15/07/',
'15/07/2024/',
],
});
test({
Expand All @@ -13739,6 +13782,22 @@ describe('Validators', () => {
'15-7-2002',
'15/07-02',
'30/04/--',
'2024',
'2024-',
'2024-05',
'2024-05-',
'2024-05-01-',
'2024-05-01-abc',
'2024/',
'2024/05',
'2024/05/',
'2024/05/01/',
'2024/05/01/abc',
'15/',
'15/07',
'15/07/',
'15/07/2024/',
'15/07/24/',
],
});
test({
Expand All @@ -13754,6 +13813,16 @@ describe('Validators', () => {
'15-7-02',
'5/7-02',
'3/4/aa',
'2024/',
'2024/05',
'2024/05/',
'2024/05/01/',
'2024/05/01/abc',
'15/',
'15/07',
'15/07/',
'15/07/2024/',
'15/07/24/',
],
});
test({
Expand All @@ -13769,6 +13838,16 @@ describe('Validators', () => {
'15/7/02',
'15-7-02',
'15-07/2002',
'2024/',
'2024/05',
'2024/05/',
'2024/05/01/',
'2024/05/01/abc',
'15/',
'15/07',
'15/07/',
'15/07/2024/',
'15/07/24/',
],
});
test({
Expand All @@ -13787,6 +13866,20 @@ describe('Validators', () => {
new Date(),
new Date([2014, 2, 15]),
new Date('2014-03-15'),
'-2020-04-19',
'-2023/05/24',
'abc-2023/05/24',
'2024',
'2024-',
'2024-05',
'2024-05-',
'2024-05-01-',
'2024-05-01-abc',
'2024/',
'2024/05',
'2024/05/',
'2024/05/01/',
'2024/05/01/abc',
],
});
test({
Expand All @@ -13812,6 +13905,21 @@ describe('Validators', () => {
'2019/02/29',
'2020/04/31',
'2020/03-15',
'-2020-04-19',
'-2023/05/24',
'abc-2023/05/24',
'2024',
'2024-',
'2024-05',
'2024-05-',
'2024-05-01-',
'2024-05-01-abc',
'2024/',
'2024/05',
'2024/05/',
'2024/05/01/',
'2024/05/01/abc',
'2024 05 01 abc',
],
});
test({
Expand All @@ -13831,6 +13939,16 @@ describe('Validators', () => {
new Date([2014, 2, 15]),
new Date('2014-03-15'),
'29.02.2020',
'2024-',
'2024-05',
'2024-05-',
'2024-05-01',
'-2020-04-19',
'-2023/05/24',
'abc-2023/05/24',
'04.05.2024.',
'04.05.2024.abc',
'abc.04.05.2024',
],
});
// emulating Pacific time zone offset & time
Expand Down
Loading