Skip to content

Commit

Permalink
Merge branch 'master' into types
Browse files Browse the repository at this point in the history
  • Loading branch information
rubiin authored Aug 26, 2024
2 parents d499579 + ff56dcf commit 12cb821
Show file tree
Hide file tree
Showing 3 changed files with 127 additions and 5 deletions.
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
10 changes: 7 additions & 3 deletions test/testFunctions.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@ import assert from 'assert';
import { format } from 'util';
import validator from '../src/index';

function stringifyArgs(argsArr) {
return argsArr.map(arg => JSON.stringify(arg)).join(', ');
}

export default function test(options) {
const args = options.args || [];

Expand All @@ -16,7 +20,7 @@ export default function test(options) {
} catch (err) {
const warning = format(
'validator.%s(%s) passed but should error',
options.validator, args.join(', ')
options.validator, stringifyArgs(args)
);

throw new Error(warning);
Expand All @@ -31,7 +35,7 @@ export default function test(options) {
if (validator[options.validator](...args) !== true) {
const warning = format(
'validator.%s(%s) failed but should have passed',
options.validator, args.join(', ')
options.validator, stringifyArgs(args)
);

throw new Error(warning);
Expand All @@ -46,7 +50,7 @@ export default function test(options) {
if (validator[options.validator](...args) !== false) {
const warning = format(
'validator.%s(%s) passed but should have failed',
options.validator, args.join(', ')
options.validator, stringifyArgs(args)
);

throw new Error(warning);
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

0 comments on commit 12cb821

Please sign in to comment.