@sebgroup/frontend-tools
Version:
A set of frontend tools
39 lines (36 loc) • 1.29 kB
JavaScript
import { isValidDate } from '../isValidDate/isValidDate.js';
/**
* Retrives a date object based on various inputs
* @param value The date value to use to retrive the date
* @returns The date object
*/
function toDate(value,
/** DEPRACATED! The input format has been depracated. The default javascript Date object string constructor will be used instead */
inputFormat) {
if (inputFormat) {
console.warn("The inputFormat has been depracated. The default javascript Date object string constructor will be used instead");
}
switch (true) {
case !value:
return null;
case value instanceof Date: {
return isValidDate(value) ? value : null;
}
case typeof value === "string":
case typeof value === "number": {
const newDate = new Date(value);
return isValidDate(newDate) ? newDate : null;
}
case typeof value === "object" &&
Array.isArray(value) &&
value.length > 1:
{
const newDate = new Date(...value);
return isValidDate(newDate) ? newDate : null;
}
default:
return null;
}
}
export { toDate };
//# sourceMappingURL=toDate.js.map