UNPKG

@brightsoftware/date-np

Version:

Simple & minimal Nepali date picker that just works.

70 lines (69 loc) 2.16 kB
import { MIN_AD_YEAR as A, AD_MONTH_LEAP_YEAR as Y, AD_MONTH as g } from "../data/constants.js"; import { NepaliDate as r } from "../src/NepaliDate.js"; import i from "./Errors.js"; import { isValidADYear as D, isADLeapYear as l, isValidBSYear as c } from "./validators.js"; const p = (t) => { let a = t.getDate(); const s = D(t), n = t.getFullYear(); if (!s) throw i.INVALID_AD_YEAR; const o = l(t); for (let e = 0; e < t.getMonth(); e++) a += o ? Y[e] : g[e]; if (n === Number(A)) return a; for (let e = A; e < n; e++) a += h(e); return a - 31; }, h = (t) => { let a = u(t); if (!D(a)) throw i.INVALID_AD_YEAR; return l(a) ? 366 : 365; }, u = (t) => { if (t instanceof Date) return t.getFullYear(); if (typeof t == "number" || typeof Number(t) == "number") return t; throw new TypeError("Error while extracting year, input to extractYear must be a Date, BS_MONTHS_KEYS or a number"); }, w = ({ date: t, locale: a }) => { if (a === "en") { const o = t instanceof r ? t.toADDate() : t; if (!D(o)) throw i.INVALID_AD_YEAR; return l(o) ? Y[o.getMonth()] : g[o.getMonth()]; } const s = t instanceof r ? t : new r(t); if (!c(s.getFullYear())) throw i.INVALID_BS_YEAR; return s.getDaysInMonth(); }, E = ({ date: t, locale: a }) => { if (a === "en") { const e = t instanceof r ? t.toADDate() : t; if (!D(e)) throw i.INVALID_AD_YEAR; return new Date(e.getFullYear(), e.getMonth(), 1).getDay(); } const n = (t instanceof r ? t : new r(t)).getFirstDayOfMonth(); if (!c(n.getFullYear())) throw i.INVALID_BS_YEAR; return n.getDay(); }, L = ({ date: t, locale: a }) => { if (a === "en") { const e = t instanceof r ? t.toADDate() : t; if (!D(e)) throw i.INVALID_AD_YEAR; return new Date(e.getFullYear(), e.getMonth() + 1, 0).getDay(); } const n = (t instanceof r ? t : new r(t)).getLastDayOfMonth(); if (!c(n.getFullYear())) throw i.INVALID_BS_YEAR; return n.getDay(); }; export { p as calcTotalDaysFromMinAD, u as extractYear, L as getEndingDayOfMonth, E as getStartingDayOfMonth, w as getTotalDaysInMonth };