Skip to content

Commit ff56dcf

Browse files
authored
fix(isDate): fix thrown error on certain invalid cases (#2443)
1 parent 96ff3b2 commit ff56dcf

File tree

2 files changed

+120
-2
lines changed

2 files changed

+120
-2
lines changed

src/lib/isDate.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ function isValidFormat(format) {
1212

1313
function zip(date, format) {
1414
const zippedArr = [],
15-
len = Math.min(date.length, format.length);
15+
len = Math.max(date.length, format.length);
1616

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

4242
for (const [dateWord, formatWord] of dateAndFormat) {
43-
if (dateWord.length !== formatWord.length) {
43+
if (!dateWord || !formatWord || dateWord.length !== formatWord.length) {
4444
return false;
4545
}
4646

test/validators.test.js

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13680,6 +13680,7 @@ describe('Validators', () => {
1368013680
new Date([2014, 2, 15]),
1368113681
new Date('2014-03-15'),
1368213682
'2020/02/29',
13683+
'2020-02-19',
1368313684
],
1368413685
invalid: [
1368513686
'',
@@ -13695,6 +13696,18 @@ describe('Validators', () => {
1369513696
'2020/03-15', // mixed delimiter
1369613697
'-2020-04-19',
1369713698
'-2023/05/24',
13699+
'abc-2023/05/24',
13700+
'2024',
13701+
'2024-',
13702+
'2024-05',
13703+
'2024-05-',
13704+
'2024-05-01-',
13705+
'2024-05-01-abc',
13706+
'2024/',
13707+
'2024/05',
13708+
'2024/05/',
13709+
'2024/05/01/',
13710+
'2024/05/01/abc',
1369813711
],
1369913712
});
1370013713
test({
@@ -13710,6 +13723,21 @@ describe('Validators', () => {
1371013723
'15/7/02',
1371113724
'15-7-02',
1371213725
'15-07/2002',
13726+
'2024',
13727+
'2024-',
13728+
'2024-05',
13729+
'2024-05-',
13730+
'2024-05-01-',
13731+
'2024-05-01-abc',
13732+
'2024/',
13733+
'2024/05',
13734+
'2024/05/',
13735+
'2024/05/01/',
13736+
'2024/05/01/abc',
13737+
'15/',
13738+
'15/07',
13739+
'15/07/',
13740+
'15/07/2024/',
1371313741
],
1371413742
});
1371513743
test({
@@ -13725,6 +13753,21 @@ describe('Validators', () => {
1372513753
'15/7/02',
1372613754
'15-7-02',
1372713755
'15-07/2002',
13756+
'2024',
13757+
'2024-',
13758+
'2024-05',
13759+
'2024-05-',
13760+
'2024-05-01-',
13761+
'2024-05-01-abc',
13762+
'2024/',
13763+
'2024/05',
13764+
'2024/05/',
13765+
'2024/05/01/',
13766+
'2024/05/01/abc',
13767+
'15/',
13768+
'15/07',
13769+
'15/07/',
13770+
'15/07/2024/',
1372813771
],
1372913772
});
1373013773
test({
@@ -13739,6 +13782,22 @@ describe('Validators', () => {
1373913782
'15-7-2002',
1374013783
'15/07-02',
1374113784
'30/04/--',
13785+
'2024',
13786+
'2024-',
13787+
'2024-05',
13788+
'2024-05-',
13789+
'2024-05-01-',
13790+
'2024-05-01-abc',
13791+
'2024/',
13792+
'2024/05',
13793+
'2024/05/',
13794+
'2024/05/01/',
13795+
'2024/05/01/abc',
13796+
'15/',
13797+
'15/07',
13798+
'15/07/',
13799+
'15/07/2024/',
13800+
'15/07/24/',
1374213801
],
1374313802
});
1374413803
test({
@@ -13754,6 +13813,16 @@ describe('Validators', () => {
1375413813
'15-7-02',
1375513814
'5/7-02',
1375613815
'3/4/aa',
13816+
'2024/',
13817+
'2024/05',
13818+
'2024/05/',
13819+
'2024/05/01/',
13820+
'2024/05/01/abc',
13821+
'15/',
13822+
'15/07',
13823+
'15/07/',
13824+
'15/07/2024/',
13825+
'15/07/24/',
1375713826
],
1375813827
});
1375913828
test({
@@ -13769,6 +13838,16 @@ describe('Validators', () => {
1376913838
'15/7/02',
1377013839
'15-7-02',
1377113840
'15-07/2002',
13841+
'2024/',
13842+
'2024/05',
13843+
'2024/05/',
13844+
'2024/05/01/',
13845+
'2024/05/01/abc',
13846+
'15/',
13847+
'15/07',
13848+
'15/07/',
13849+
'15/07/2024/',
13850+
'15/07/24/',
1377213851
],
1377313852
});
1377413853
test({
@@ -13787,6 +13866,20 @@ describe('Validators', () => {
1378713866
new Date(),
1378813867
new Date([2014, 2, 15]),
1378913868
new Date('2014-03-15'),
13869+
'-2020-04-19',
13870+
'-2023/05/24',
13871+
'abc-2023/05/24',
13872+
'2024',
13873+
'2024-',
13874+
'2024-05',
13875+
'2024-05-',
13876+
'2024-05-01-',
13877+
'2024-05-01-abc',
13878+
'2024/',
13879+
'2024/05',
13880+
'2024/05/',
13881+
'2024/05/01/',
13882+
'2024/05/01/abc',
1379013883
],
1379113884
});
1379213885
test({
@@ -13812,6 +13905,21 @@ describe('Validators', () => {
1381213905
'2019/02/29',
1381313906
'2020/04/31',
1381413907
'2020/03-15',
13908+
'-2020-04-19',
13909+
'-2023/05/24',
13910+
'abc-2023/05/24',
13911+
'2024',
13912+
'2024-',
13913+
'2024-05',
13914+
'2024-05-',
13915+
'2024-05-01-',
13916+
'2024-05-01-abc',
13917+
'2024/',
13918+
'2024/05',
13919+
'2024/05/',
13920+
'2024/05/01/',
13921+
'2024/05/01/abc',
13922+
'2024 05 01 abc',
1381513923
],
1381613924
});
1381713925
test({
@@ -13831,6 +13939,16 @@ describe('Validators', () => {
1383113939
new Date([2014, 2, 15]),
1383213940
new Date('2014-03-15'),
1383313941
'29.02.2020',
13942+
'2024-',
13943+
'2024-05',
13944+
'2024-05-',
13945+
'2024-05-01',
13946+
'-2020-04-19',
13947+
'-2023/05/24',
13948+
'abc-2023/05/24',
13949+
'04.05.2024.',
13950+
'04.05.2024.abc',
13951+
'abc.04.05.2024',
1383413952
],
1383513953
});
1383613954
// emulating Pacific time zone offset & time

0 commit comments

Comments
 (0)