@brightsoftware/date-np
Version:
Simple & minimal Nepali date picker that just works.
203 lines (202 loc) • 5.52 kB
JavaScript
var w = Object.defineProperty;
var c = (r, t, e) => t in r ? w(r, t, { enumerable: !0, configurable: !0, writable: !0, value: e }) : r[t] = e;
var m = (r, t, e) => c(r, typeof t != "symbol" ? t + "" : t, e);
import { BS_MONTHS as i } from "../data/constants.js";
import { convertFromADToBS as y, convertFromBSToAD as f } from "../utils/conversion.js";
import { isValidBSYear as o } from "../utils/validators.js";
import a from "../utils/Errors.js";
class s {
constructor(t, e, u) {
m(this, "_year");
m(this, "_month");
// 0-based like JavaScript Date
m(this, "_date");
if (t === void 0) {
const h = y(/* @__PURE__ */ new Date());
this._year = h.getFullYear(), this._month = h.getMonth(), this._date = h.getDate();
} else if (t instanceof Date) {
const n = y(t);
this._year = n.getFullYear(), this._month = n.getMonth(), this._date = n.getDate();
} else if (t instanceof s)
this._year = t._year, this._month = t._month, this._date = t._date;
else if (typeof t == "string") {
const n = t.split("-");
if (n.length !== 3)
throw new Error("Invalid date string format. Expected format: YYYY-MM-DD");
const h = parseInt(n[0], 10), _ = parseInt(n[1], 10), d = parseInt(n[2], 10);
if (isNaN(h) || isNaN(_) || isNaN(d))
throw new Error("Invalid date string format. All parts must be numbers");
if (_ < 1 || _ > 12)
throw new Error("Month must be between 1 and 12");
this._year = h, this._month = _ - 1, this._date = d, this.normalize();
} else if (typeof t == "number")
this._year = t, this._month = e ?? 0, this._date = u ?? 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 (!o(this._year))
throw a.INVALID_BS_YEAR;
const t = i[this._year][this._month];
for (; this._date < 1; ) {
if (this._month--, this._month < 0 && (this._month = 11, this._year--), !o(this._year))
throw a.INVALID_BS_YEAR;
const e = i[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++), !o(this._year))
throw a.INVALID_BS_YEAR;
const e = i[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 f(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 (!o(this._year))
throw a.INVALID_BS_YEAR;
return i[this._year][this._month];
}
/**
* Get the number of days in a specific month of the current year
*/
getDaysInMonthOf(t) {
if (!o(this._year))
throw a.INVALID_BS_YEAR;
if (t < 0 || t > 11)
throw new Error("Month must be between 0 and 11");
return i[this._year][t];
}
/**
* Create a new NepaliDate with the first day of the current month
*/
getFirstDayOfMonth() {
return new s(this._year, this._month, 1);
}
/**
* Create a new NepaliDate with the last day of the current month
*/
getLastDayOfMonth() {
const t = this.getDaysInMonth();
return new s(this._year, this._month, t);
}
/**
* Add days to the current date
*/
addDays(t) {
const e = new s(this);
return e._date += t, e.normalize(), e;
}
/**
* Add months to the current date
*/
addMonths(t) {
const e = new s(this);
return e._month += t, e.normalize(), e;
}
/**
* Add years to the current date
*/
addYears(t) {
const e = new s(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 f(this);
}
/**
* Create NepaliDate from AD Date
*/
static fromADDate(t) {
return new s(t);
}
/**
* Get today's Nepali date
*/
static today() {
return new s();
}
/**
* 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 s(this);
}
}
export {
s as NepaliDate
};