UNPKG

dk-plus

Version:
48 lines (47 loc) 1.65 kB
const o = (e) => String(e).padStart(2, "0"), T = (e, s) => { const t = { YYYY: String(e.getFullYear()), MM: o(e.getMonth() + 1), DD: o(e.getDate()), HH: o(e.getHours()), mm: o(e.getMinutes()), ss: o(e.getSeconds()) }; return s.replace(/YYYY/g, t.YYYY).replace(/MM/g, t.MM).replace(/DD/g, t.DD).replace(/HH/g, t.HH).replace(/mm/g, t.mm).replace(/ss/g, t.ss); }, $ = (e, s) => { var D, M, f, H, d, N; const t = String(e != null ? e : "").trim(); if (!t) return null; const S = s.replace(/[-/\\^$*+?.()|[\]{}]/g, "\\$&"), h = { YYYY: "(\\d{4})", MM: "(\\d{2})", DD: "(\\d{2})", HH: "(\\d{2})", mm: "(\\d{2})", ss: "(\\d{2})" }, c = [], y = S.replace(/YYYY|MM|DD|HH|mm|ss/g, (u) => (c.push(u), h[u])), l = new RegExp(`^${y}$`).exec(t); if (!l) return null; const n = {}; c.forEach((u, b) => { n[u] = Number(l[b + 1]); }); const g = (D = n.YYYY) != null ? D : new Date().getFullYear(), Y = ((M = n.MM) != null ? M : 1) - 1, m = (f = n.DD) != null ? f : 1, i = (H = n.HH) != null ? H : 0, a = (d = n.mm) != null ? d : 0, p = (N = n.ss) != null ? N : 0, r = new Date(g, Y, m, i, a, p); return r.getFullYear() !== g || r.getMonth() !== Y || r.getDate() !== m || r.getHours() !== i || r.getMinutes() !== a || r.getSeconds() !== p ? null : r; }, x = (e) => { if (e == null) return null; if (e instanceof Date) return Number.isNaN(e.getTime()) ? null : e; if (typeof e == "number") { const s = new Date(e); return Number.isNaN(s.getTime()) ? null : s; } return null; }; export { x as coerceToDate, T as formatByPattern, $ as parseByPattern };