import { FromJSONStrategy } from "../compose_strategy.ts";import { JSONValue } from "../../serializable.ts";import { ERROR_INVALID_DATE } from "../../error_messages.ts";
export function createDateStrategy(regex: RegExp): FromJSONStrategy { return (input: JSONValue): Date => { if (typeof input !== "string" || !regex.exec(input)) { throw new Error(ERROR_INVALID_DATE); } const date = new Date(input); if (date.toJSON() === null) { throw new Error(ERROR_INVALID_DATE); } return date; };}
export function iso8601Date(): FromJSONStrategy { return (input: JSONValue): Date => { const iso = /^(-?(?:[1-9][0-9]*)?[0-9]{4})-(1[0-2]|0[1-9])-(3[01]|0[1-9]|[12][0-9])T(2[0-3]|[01][0-9]):([0-5][0-9]):([0-5][0-9])(\.[0-9]+)?(Z|[+-](?:2[0-3]|[01][0-9]):[0-5][0-9])?$/; if (typeof input !== "string" || !iso.exec(input)) { throw new Error(ERROR_INVALID_DATE); } return new Date(input); };}