Skip to content

Commit

Permalink
(Bug Fix) isDate function: Add validation for delimiter and length ch…
Browse files Browse the repository at this point in the history
…ecks to prevent undefined errors
  • Loading branch information
Osho957 committed Sep 17, 2024
1 parent ff56dcf commit 3f1fba4
Showing 1 changed file with 40 additions and 16 deletions.
56 changes: 40 additions & 16 deletions src/lib/isDate.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
import merge from './util/merge';
import merge from "./util/merge";

Check failure on line 1 in src/lib/isDate.js

View workflow job for this annotation

GitHub Actions / Run tests on Node.js 10

Strings must use singlequote

Check failure on line 1 in src/lib/isDate.js

View workflow job for this annotation

GitHub Actions / Run tests on Node.js 18

Strings must use singlequote

Check failure on line 1 in src/lib/isDate.js

View workflow job for this annotation

GitHub Actions / Run tests on Node.js 14

Strings must use singlequote

Check failure on line 1 in src/lib/isDate.js

View workflow job for this annotation

GitHub Actions / Run tests on Node.js 12

Strings must use singlequote

Check failure on line 1 in src/lib/isDate.js

View workflow job for this annotation

GitHub Actions / Run tests on Node.js 8

Strings must use singlequote

Check failure on line 1 in src/lib/isDate.js

View workflow job for this annotation

GitHub Actions / Run tests on Node.js 16

Strings must use singlequote

Check failure on line 1 in src/lib/isDate.js

View workflow job for this annotation

GitHub Actions / Run tests on Node.js 20

Strings must use singlequote

const default_date_options = {
format: 'YYYY/MM/DD',
delimiters: ['/', '-'],
format: "YYYY/MM/DD",

Check failure on line 4 in src/lib/isDate.js

View workflow job for this annotation

GitHub Actions / Run tests on Node.js 10

Strings must use singlequote

Check failure on line 4 in src/lib/isDate.js

View workflow job for this annotation

GitHub Actions / Run tests on Node.js 18

Strings must use singlequote

Check failure on line 4 in src/lib/isDate.js

View workflow job for this annotation

GitHub Actions / Run tests on Node.js 14

Strings must use singlequote

Check failure on line 4 in src/lib/isDate.js

View workflow job for this annotation

GitHub Actions / Run tests on Node.js 12

Strings must use singlequote

Check failure on line 4 in src/lib/isDate.js

View workflow job for this annotation

GitHub Actions / Run tests on Node.js 8

Strings must use singlequote

Check failure on line 4 in src/lib/isDate.js

View workflow job for this annotation

GitHub Actions / Run tests on Node.js 16

Strings must use singlequote

Check failure on line 4 in src/lib/isDate.js

View workflow job for this annotation

GitHub Actions / Run tests on Node.js 20

Strings must use singlequote
delimiters: ["/", "-"],

Check failure on line 5 in src/lib/isDate.js

View workflow job for this annotation

GitHub Actions / Run tests on Node.js 10

Strings must use singlequote

Check failure on line 5 in src/lib/isDate.js

View workflow job for this annotation

GitHub Actions / Run tests on Node.js 10

Strings must use singlequote

Check failure on line 5 in src/lib/isDate.js

View workflow job for this annotation

GitHub Actions / Run tests on Node.js 18

Strings must use singlequote

Check failure on line 5 in src/lib/isDate.js

View workflow job for this annotation

GitHub Actions / Run tests on Node.js 18

Strings must use singlequote

Check failure on line 5 in src/lib/isDate.js

View workflow job for this annotation

GitHub Actions / Run tests on Node.js 14

Strings must use singlequote

Check failure on line 5 in src/lib/isDate.js

View workflow job for this annotation

GitHub Actions / Run tests on Node.js 14

Strings must use singlequote

Check failure on line 5 in src/lib/isDate.js

View workflow job for this annotation

GitHub Actions / Run tests on Node.js 12

Strings must use singlequote

Check failure on line 5 in src/lib/isDate.js

View workflow job for this annotation

GitHub Actions / Run tests on Node.js 12

Strings must use singlequote

Check failure on line 5 in src/lib/isDate.js

View workflow job for this annotation

GitHub Actions / Run tests on Node.js 8

Strings must use singlequote

Check failure on line 5 in src/lib/isDate.js

View workflow job for this annotation

GitHub Actions / Run tests on Node.js 8

Strings must use singlequote

Check failure on line 5 in src/lib/isDate.js

View workflow job for this annotation

GitHub Actions / Run tests on Node.js 16

Strings must use singlequote

Check failure on line 5 in src/lib/isDate.js

View workflow job for this annotation

GitHub Actions / Run tests on Node.js 16

Strings must use singlequote

Check failure on line 5 in src/lib/isDate.js

View workflow job for this annotation

GitHub Actions / Run tests on Node.js 20

Strings must use singlequote

Check failure on line 5 in src/lib/isDate.js

View workflow job for this annotation

GitHub Actions / Run tests on Node.js 20

Strings must use singlequote
strictMode: false,
};

function isValidFormat(format) {
return /(^(y{4}|y{2})[.\/-](m{1,2})[.\/-](d{1,2})$)|(^(m{1,2})[.\/-](d{1,2})[.\/-]((y{4}|y{2})$))|(^(d{1,2})[.\/-](m{1,2})[.\/-]((y{4}|y{2})$))/gi.test(format);
return /(^(y{4}|y{2})[.\/-](m{1,2})[.\/-](d{1,2})$)|(^(m{1,2})[.\/-](d{1,2})[.\/-]((y{4}|y{2})$))|(^(d{1,2})[.\/-](m{1,2})[.\/-]((y{4}|y{2})$))/gi.test(

Check failure on line 10 in src/lib/isDate.js

View workflow job for this annotation

GitHub Actions / Run tests on Node.js 10

Unexpected newline after '('

Check failure on line 10 in src/lib/isDate.js

View workflow job for this annotation

GitHub Actions / Run tests on Node.js 18

Unexpected newline after '('

Check failure on line 10 in src/lib/isDate.js

View workflow job for this annotation

GitHub Actions / Run tests on Node.js 14

Unexpected newline after '('

Check failure on line 10 in src/lib/isDate.js

View workflow job for this annotation

GitHub Actions / Run tests on Node.js 12

Unexpected newline after '('

Check failure on line 10 in src/lib/isDate.js

View workflow job for this annotation

GitHub Actions / Run tests on Node.js 8

Unexpected newline after '('

Check failure on line 10 in src/lib/isDate.js

View workflow job for this annotation

GitHub Actions / Run tests on Node.js 16

Unexpected newline after '('

Check failure on line 10 in src/lib/isDate.js

View workflow job for this annotation

GitHub Actions / Run tests on Node.js 20

Unexpected newline after '('
format
);

Check failure on line 12 in src/lib/isDate.js

View workflow job for this annotation

GitHub Actions / Run tests on Node.js 10

Unexpected newline before ')'

Check failure on line 12 in src/lib/isDate.js

View workflow job for this annotation

GitHub Actions / Run tests on Node.js 18

Unexpected newline before ')'

Check failure on line 12 in src/lib/isDate.js

View workflow job for this annotation

GitHub Actions / Run tests on Node.js 14

Unexpected newline before ')'

Check failure on line 12 in src/lib/isDate.js

View workflow job for this annotation

GitHub Actions / Run tests on Node.js 12

Unexpected newline before ')'

Check failure on line 12 in src/lib/isDate.js

View workflow job for this annotation

GitHub Actions / Run tests on Node.js 8

Unexpected newline before ')'

Check failure on line 12 in src/lib/isDate.js

View workflow job for this annotation

GitHub Actions / Run tests on Node.js 16

Unexpected newline before ')'

Check failure on line 12 in src/lib/isDate.js

View workflow job for this annotation

GitHub Actions / Run tests on Node.js 20

Unexpected newline before ')'
}

function zip(date, format) {
Expand All @@ -22,21 +24,38 @@ function zip(date, format) {
}

export default function isDate(input, options) {
if (typeof options === 'string') { // Allow backward compatibility for old format isDate(input [, format])
if (typeof options === "string") {

Check failure on line 27 in src/lib/isDate.js

View workflow job for this annotation

GitHub Actions / Run tests on Node.js 10

Strings must use singlequote

Check failure on line 27 in src/lib/isDate.js

View workflow job for this annotation

GitHub Actions / Run tests on Node.js 18

Strings must use singlequote

Check failure on line 27 in src/lib/isDate.js

View workflow job for this annotation

GitHub Actions / Run tests on Node.js 14

Strings must use singlequote

Check failure on line 27 in src/lib/isDate.js

View workflow job for this annotation

GitHub Actions / Run tests on Node.js 12

Strings must use singlequote

Check failure on line 27 in src/lib/isDate.js

View workflow job for this annotation

GitHub Actions / Run tests on Node.js 8

Strings must use singlequote

Check failure on line 27 in src/lib/isDate.js

View workflow job for this annotation

GitHub Actions / Run tests on Node.js 16

Strings must use singlequote

Check failure on line 27 in src/lib/isDate.js

View workflow job for this annotation

GitHub Actions / Run tests on Node.js 20

Strings must use singlequote
// Allow backward compatibility for old format isDate(input [, format])
options = merge({ format: options }, default_date_options);
} else {
options = merge(options, default_date_options);
}
if (typeof input === 'string' && isValidFormat(options.format)) {
const formatDelimiter = options.delimiters
.find(delimiter => options.format.indexOf(delimiter) !== -1);
if (typeof input === "string" && isValidFormat(options.format)) {

Check failure on line 33 in src/lib/isDate.js

View workflow job for this annotation

GitHub Actions / Run tests on Node.js 10

Strings must use singlequote

Check failure on line 33 in src/lib/isDate.js

View workflow job for this annotation

GitHub Actions / Run tests on Node.js 18

Strings must use singlequote

Check failure on line 33 in src/lib/isDate.js

View workflow job for this annotation

GitHub Actions / Run tests on Node.js 14

Strings must use singlequote

Check failure on line 33 in src/lib/isDate.js

View workflow job for this annotation

GitHub Actions / Run tests on Node.js 12

Strings must use singlequote

Check failure on line 33 in src/lib/isDate.js

View workflow job for this annotation

GitHub Actions / Run tests on Node.js 8

Strings must use singlequote

Check failure on line 33 in src/lib/isDate.js

View workflow job for this annotation

GitHub Actions / Run tests on Node.js 16

Strings must use singlequote

Check failure on line 33 in src/lib/isDate.js

View workflow job for this annotation

GitHub Actions / Run tests on Node.js 20

Strings must use singlequote
// Ensure the input contains at least one valid delimiter
const hasValidDelimiter = options.delimiters.some((delimiter) =>

Check failure on line 35 in src/lib/isDate.js

View workflow job for this annotation

GitHub Actions / Run tests on Node.js 10

Unexpected parentheses around single function argument having a body with no curly braces

Check failure on line 35 in src/lib/isDate.js

View workflow job for this annotation

GitHub Actions / Run tests on Node.js 18

Unexpected parentheses around single function argument having a body with no curly braces

Check failure on line 35 in src/lib/isDate.js

View workflow job for this annotation

GitHub Actions / Run tests on Node.js 14

Unexpected parentheses around single function argument having a body with no curly braces

Check failure on line 35 in src/lib/isDate.js

View workflow job for this annotation

GitHub Actions / Run tests on Node.js 12

Unexpected parentheses around single function argument having a body with no curly braces

Check failure on line 35 in src/lib/isDate.js

View workflow job for this annotation

GitHub Actions / Run tests on Node.js 8

Unexpected parentheses around single function argument having a body with no curly braces

Check failure on line 35 in src/lib/isDate.js

View workflow job for this annotation

GitHub Actions / Run tests on Node.js 16

Unexpected parentheses around single function argument having a body with no curly braces

Check failure on line 35 in src/lib/isDate.js

View workflow job for this annotation

GitHub Actions / Run tests on Node.js 20

Unexpected parentheses around single function argument having a body with no curly braces
input.includes(delimiter)
);

Check failure on line 37 in src/lib/isDate.js

View workflow job for this annotation

GitHub Actions / Run tests on Node.js 10

Unexpected newline before ')'

Check failure on line 37 in src/lib/isDate.js

View workflow job for this annotation

GitHub Actions / Run tests on Node.js 18

Unexpected newline before ')'

Check failure on line 37 in src/lib/isDate.js

View workflow job for this annotation

GitHub Actions / Run tests on Node.js 14

Unexpected newline before ')'

Check failure on line 37 in src/lib/isDate.js

View workflow job for this annotation

GitHub Actions / Run tests on Node.js 12

Unexpected newline before ')'

Check failure on line 37 in src/lib/isDate.js

View workflow job for this annotation

GitHub Actions / Run tests on Node.js 8

Unexpected newline before ')'

Check failure on line 37 in src/lib/isDate.js

View workflow job for this annotation

GitHub Actions / Run tests on Node.js 16

Unexpected newline before ')'

Check failure on line 37 in src/lib/isDate.js

View workflow job for this annotation

GitHub Actions / Run tests on Node.js 20

Unexpected newline before ')'
if (!hasValidDelimiter) {
return false;
}

const formatDelimiter = options.delimiters.find(
(delimiter) => options.format.indexOf(delimiter) !== -1
);
const dateDelimiter = options.strictMode
? formatDelimiter
: options.delimiters.find(delimiter => input.indexOf(delimiter) !== -1);
const dateAndFormat = zip(
input.split(dateDelimiter),
options.format.toLowerCase().split(formatDelimiter)
);
: options.delimiters.find((delimiter) => input.indexOf(delimiter) !== -1);

// Split input and format by the delimiters
const inputParts = input.split(dateDelimiter);
const formatParts = options.format.toLowerCase().split(formatDelimiter);

// Check if splitting produced the same number of parts
if (inputParts.length !== formatParts.length) {
return false;
}

const dateAndFormat = zip(inputParts, formatParts);
const dateObj = {};

for (const [dateWord, formatWord] of dateAndFormat) {
Expand All @@ -50,7 +69,7 @@ export default function isDate(input, options) {
let fullYear = dateObj.y;

// Check if the year starts with a hyphen
if (fullYear.startsWith('-')) {
if (fullYear.startsWith("-")) {
return false; // Hyphen before year is not allowed
}

Expand Down Expand Up @@ -82,11 +101,16 @@ export default function isDate(input, options) {
day = `0${dateObj.d}`;
}

return new Date(`${fullYear}-${month}-${day}T00:00:00.000Z`).getUTCDate() === +dateObj.d;
// Construct a date string in the format YYYY-MM-DD and validate
const parsedDate = new Date(`${fullYear}-${month}-${day}T00:00:00.000Z`);
return parsedDate.getUTCDate() === +dateObj.d; // Check if the day matches
}

if (!options.strictMode) {
return Object.prototype.toString.call(input) === '[object Date]' && isFinite(input);
return (
Object.prototype.toString.call(input) === "[object Date]" &&
isFinite(input)
);
}

return false;
Expand Down

0 comments on commit 3f1fba4

Please sign in to comment.