Skip to content

Commit

Permalink
fixed the issue on isDateValidWithFormat function
Browse files Browse the repository at this point in the history
  • Loading branch information
suntoss0708 committed Oct 6, 2021
1 parent 43bd37c commit 2766190
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 83 deletions.
106 changes: 34 additions & 72 deletions src/CalendarData/validator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,91 +133,53 @@ export const isInValidRange = (
}
};

type iterator = {
index: number;
length: number;
};

type DateFormat = {
yyyy?: number;
mm?: number;
m?: number;
dd?: number;
d?: number;
yyyy?: iterator;
mm?: iterator;
m?: iterator;
dd?: iterator;
d?: iterator;
};

export function isDateValidWithFormat(
input: string,
format: string,
throwError?: boolean
): boolean {
if (!format || !input) {
if (throwError) {
throw new Error(
`Date provided or Format isn't supported. Got date=${input} and format =${format}`
);
}
return false;
}
export function isDateValidWithFormat(input: string, format: string): boolean {
format = String(format)?.toLocaleLowerCase(); // default format
const parts = input.match(/(\d+)/g);

const formatSymbol = format.replace(/(yyyy|dd|d|mm|m)/g, "");

const inputSymbol = input.replace(/\d/g, "");

console.log("parts", parts);
if (parts) {
let i = 0;
const fmt: DateFormat = {};
if (!parts) return false;

format.replace(/(yyyy|dd|d|mm|m)/g, function (part) {
fmt[part as keyof DateFormat] = i++;
return "";
});
let i = 0;
const fmt: DateFormat = {};
format.replace(/(yyyy|dd|d|mm|m)/g, function (part) {
fmt[part as keyof DateFormat] = { index: i++, length: part.length };
return "";
});

const yearIndex = fmt["yyyy"];
const monthIndex = fmt["mm"] ?? fmt["m"];
const dateIndex = fmt["dd"] ?? fmt["d"];
if (inputSymbol !== formatSymbol) return false;

if (
yearIndex !== undefined &&
monthIndex !== undefined &&
dateIndex !== undefined
) {
let year = +parts[yearIndex];
let month = +parts[monthIndex];
let date = +parts[dateIndex];

if (!year && month > 12 && date > 31) {
return false;
}
}
const yearD = fmt["yyyy"];

if (inputSymbol !== formatSymbol) return false;
const monthD = fmt["mm"] ?? fmt["m"];
const dateD = fmt["dd"] ?? fmt["d"];
if (yearD !== undefined && monthD !== undefined && dateD !== undefined) {
let year = parts[yearD.index];
let month = parts[monthD.index];
let date = parts[dateD.index];

if (yearIndex === undefined) {
if (throwError)
throw new TypeError(
`Year isn't Provided in the given date input or the format doesn't contain correct combination of 'yyyy'. Acceptable formats are the pemutation of 'yyyy', 'mm' and 'dd' instead got ${format} `
);
return false;
}
if (monthIndex === undefined) {
if (throwError)
throw new TypeError(
`Month isn't Provided in the given date input or the format doesn't contain correct combination of 'mm' | 'm'. Acceptable formats are the pemutation of 'yyyy', 'mm','m','d', and 'dd' instead got ${format} `
);
return false;
}
if (dateIndex === undefined) {
if (throwError)
throw new TypeError(
`Date isn't Provided in the given date input or the format doesn't contain correct combination of 'dd' | 'd'. Acceptable formats are the pemutation of 'yyyy', 'mm','m', 'd', and 'dd' instead got ${format} `
);
return false;
}
return true;
} else {
if (throwError)
throw new TypeError(
"Passed Input isn't a valid date. Please check again. Acceptable formats are the pemutation of 'yyyy', 'mm','m','d' and 'dd' "
);
return false;
if (
year.length === yearD.length &&
month.length === monthD.length &&
date.length === dateD.length
)
return true;
}

return false;
}
11 changes: 0 additions & 11 deletions src/DatePicker/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -84,12 +84,8 @@ const DatePicker = (props: IDatePicker) => {
dateFormat
);

console.log("Data string of selected date>>>>", dateStringOfSelectedDate);

const obj = getDateObj(selectedDate || today, dateFormat);

console.log("Date object from getDateObj", obj);

//TODO static
const acceptableFormat = [
"dd-mm-yyyy",
Expand All @@ -99,13 +95,6 @@ const DatePicker = (props: IDatePicker) => {
dateFormat,
];

console.log(
"isDateValidWithFormat",
isDateValidWithFormat("13-07-2061", "yyyy-mm-dd")
);

console.log("Acceptable Format >>>>", acceptableFormat);

acceptableFormat.forEach((format, index) => {
if (isDateValidWithFormat(value, format)) {
const yearValidator = /\d+.\d+.(\d){4}/;
Expand Down

0 comments on commit 2766190

Please sign in to comment.