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 3f1fba4 commit 3677578
Showing 1 changed file with 6 additions and 7 deletions.
13 changes: 6 additions & 7 deletions src/lib/isDate.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
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 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 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 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 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 20

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 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 14

Strings must use singlequote

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

Expand All @@ -24,13 +24,13 @@ function zip(date, format) {
}

export default function isDate(input, options) {
if (typeof options === "string") {
if (typeof options === 'string') {
// 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)) {
if (typeof input === 'string' && isValidFormat(options.format)) {
// 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 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 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 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 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 20

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 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 14

Unexpected parentheses around single function argument having a body with no curly braces
input.includes(delimiter)
Expand Down Expand Up @@ -69,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 @@ -108,8 +108,7 @@ export default function isDate(input, options) {

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

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

View workflow job for this annotation

GitHub Actions / Run tests on Node.js 16

Expected indentation of 6 spaces but found 7

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

View workflow job for this annotation

GitHub Actions / Run tests on Node.js 8

Expected indentation of 6 spaces but found 7

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

View workflow job for this annotation

GitHub Actions / Run tests on Node.js 18

Expected indentation of 6 spaces but found 7

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

View workflow job for this annotation

GitHub Actions / Run tests on Node.js 12

Expected indentation of 6 spaces but found 7

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

View workflow job for this annotation

GitHub Actions / Run tests on Node.js 20

Expected indentation of 6 spaces but found 7

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

View workflow job for this annotation

GitHub Actions / Run tests on Node.js 10

Expected indentation of 6 spaces but found 7

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

View workflow job for this annotation

GitHub Actions / Run tests on Node.js 14

Expected indentation of 6 spaces but found 7
);
}

Expand Down

0 comments on commit 3677578

Please sign in to comment.