UNPKG

@brightsoftware/date-np

Version:

Simple & minimal Nepali date picker that just works.

193 lines (192 loc) 4.97 kB
var u = Object.defineProperty; var f = (s, t, e) => t in s ? u(s, t, { enumerable: !0, configurable: !0, writable: !0, value: e }) : s[t] = e; var a = (s, t, e) => f(s, typeof t != "symbol" ? t + "" : t, e); import { BS_MONTHS as n } from "../data/constants.js"; import { convertFromADToBS as m, convertFromBSToAD as y } from "../utils/conversion.js"; import { isValidBSYear as i } from "../utils/validators.js"; import r from "../utils/Errors.js"; class h { constructor(t, e, d) { a(this, "_year"); a(this, "_month"); // 0-based like JavaScript Date a(this, "_date"); if (t === void 0) { const _ = m(/* @__PURE__ */ new Date()); this._year = _.getFullYear(), this._month = _.getMonth(), this._date = _.getDate(); } else if (t instanceof Date) { const o = m(t); this._year = o.getFullYear(), this._month = o.getMonth(), this._date = o.getDate(); } else if (t instanceof h) this._year = t._year, this._month = t._month, this._date = t._date; else if (typeof t == "number") this._year = t, this._month = e ?? 0, this._date = d ?? 1, this.normalize(); else throw new Error("Invalid constructor arguments"); } /** * Normalize the date to handle overflow/underflow of dates and months */ normalize() { for (; this._month < 0; ) this._month += 12, this._year--; for (; this._month >= 12; ) this._month -= 12, this._year++; if (!i(this._year)) throw r.INVALID_BS_YEAR; const t = n[this._year][this._month]; for (; this._date < 1; ) { if (this._month--, this._month < 0 && (this._month = 11, this._year--), !i(this._year)) throw r.INVALID_BS_YEAR; const e = n[this._year][this._month]; this._date += e; } for (; this._date > t; ) { if (this._date -= t, this._month++, this._month >= 12 && (this._month = 0, this._year++), !i(this._year)) throw r.INVALID_BS_YEAR; const e = n[this._year][this._month]; if (this._date <= e) break; } } /** * Get the year (BS) */ getFullYear() { return this._year; } /** * Get the month (0-based, like JavaScript Date) */ getMonth() { return this._month; } /** * Get the date */ getDate() { return this._date; } /** * Get the day of the week (0 = Sunday, 6 = Saturday) * This converts to AD date to get the correct day */ getDay() { return y(this).getDay(); } /** * Set the year */ setFullYear(t) { this._year = t, this.normalize(); } /** * Set the month (0-based) */ setMonth(t) { this._month = t, this.normalize(); } /** * Set the date */ setDate(t) { this._date = t, this.normalize(); } /** * Get the number of days in the current month */ getDaysInMonth() { if (!i(this._year)) throw r.INVALID_BS_YEAR; return n[this._year][this._month]; } /** * Get the number of days in a specific month of the current year */ getDaysInMonthOf(t) { if (!i(this._year)) throw r.INVALID_BS_YEAR; if (t < 0 || t > 11) throw new Error("Month must be between 0 and 11"); return n[this._year][t]; } /** * Create a new NepaliDate with the first day of the current month */ getFirstDayOfMonth() { return new h(this._year, this._month, 1); } /** * Create a new NepaliDate with the last day of the current month */ getLastDayOfMonth() { const t = this.getDaysInMonth(); return new h(this._year, this._month, t); } /** * Add days to the current date */ addDays(t) { const e = new h(this); return e._date += t, e.normalize(), e; } /** * Add months to the current date */ addMonths(t) { const e = new h(this); return e._month += t, e.normalize(), e; } /** * Add years to the current date */ addYears(t) { const e = new h(this); return e._year += t, e.normalize(), e; } /** * Check if this date equals another date */ equals(t) { return this._year === t._year && this._month === t._month && this._date === t._date; } /** * Compare this date with another date * Returns: -1 if this < other, 0 if equal, 1 if this > other */ compare(t) { return this._year !== t._year ? this._year < t._year ? -1 : 1 : this._month !== t._month ? this._month < t._month ? -1 : 1 : this._date !== t._date ? this._date < t._date ? -1 : 1 : 0; } /** * Convert to AD Date */ toADDate() { return y(this); } /** * Create NepaliDate from AD Date */ static fromADDate(t) { return new h(t); } /** * Get today's Nepali date */ static today() { return new h(); } /** * String representation */ toString() { return `${this._year}/${String(this._month + 1).padStart(2, "0")}/${String(this._date).padStart(2, "0")}`; } /** * Create a clone of this date */ clone() { return new h(this); } } export { h as NepaliDate };