stringzy
Version:
A versatile string manipulation library providing a range of text utilities for JavaScript and Node.js applications.
62 lines (61 loc) • 2.26 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
exports.DateFormats = void 0;
exports.isDate = isDate;
var DateFormats;
(function (DateFormats) {
DateFormats["YYYYMMDD"] = "YYYYMMDD";
DateFormats["MMDDYYYY"] = "MMDDYYYY";
DateFormats["DDMMYYYY"] = "DDMMYYYY";
})(DateFormats || (exports.DateFormats = DateFormats = {}));
const VALID_SEPARATORS = ['.', '-', '/'];
/**
* Takes a date and a format and returns whether or not the date matches the specified format.
* Optionally takes a separator that can be used instead of the default hyphen.
*
* Only accepts 2 digit month/day and 4 digit year
* @example 01-01-2000 -> True
* @example 01-1-2000 -> False
* @example 01-01-00 -> False
*/
function isDate(input, format, separator = '-') {
if (typeof input !== 'string')
return false;
if (input.length !== 10)
return false;
// Ensure separator is supported
if (![...VALID_SEPARATORS].includes(separator))
return false;
const formats = [
{
// Format: YYYY-MM-DD
format: DateFormats.YYYYMMDD,
regex: new RegExp(`^\\d{4}${separator}\\d{2}${separator}\\d{2}$`),
order: ['Y', 'M', 'D'],
},
{
// Format: MM-DD-YYYY
format: DateFormats.MMDDYYYY,
regex: new RegExp(`^\d{2}${separator}\d{2}${separator}\d{4}$/`),
order: ['M', 'D', 'Y'],
},
{
// Format: DD-MM-YYYY
format: DateFormats.DDMMYYYY,
regex: new RegExp(`^\d{2}${separator}\d{2}${separator}\d{4}$`),
order: ['D', 'M', 'Y'],
},
];
const targetFormat = formats.find((f) => f.format === format);
const parts = input.split(separator).map(Number);
const dateParts = {};
targetFormat === null || targetFormat === void 0 ? void 0 : targetFormat.order.forEach((k, i) => {
dateParts[k] = parts[i];
});
const { Y, M, D } = dateParts;
const checkDate = new Date(Y, M - 1, D); // Months are 0 indexed
if (checkDate.toString() === 'Invalid Date')
return false;
const isValid = checkDate.getFullYear() === Y && checkDate.getMonth() === M - 1 && checkDate.getDate() === D;
return isValid;
}
;