UNPKG

@zener/nepali-date

Version:

Nepali Date is a lightweight utility designed to handle Nepali dates.

1,036 lines (1,029 loc) 30.8 kB
"use strict"; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __hasOwnProp = Object.prototype.hasOwnProperty; var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); // src/nepali-date/index.ts var nepali_date_exports = {}; __export(nepali_date_exports, { AD_MONTH: () => AD_MONTH, AD_MONTH_FULL: () => AD_MONTH_FULL, AD_MONTH_NEPALI: () => AD_MONTH_NEPALI, ENGLISH_NEPALI_MONTH: () => ENGLISH_NEPALI_MONTH, ENGLISH_WEEK: () => ENGLISH_WEEK, ENGLISH_WEEK_FULL: () => ENGLISH_WEEK_FULL, NEPALI_DIGITS: () => NEPALI_DIGITS, NEPALI_MONTH: () => NEPALI_MONTH, NEPALI_TODAY: () => NEPALI_TODAY, NEPALI_WEEK: () => NEPALI_WEEK, NEPALI_WEEK_FULL: () => NEPALI_WEEK_FULL, default: () => nepali_date_default2, formatADDate: () => formatADDate, lowerADDate: () => lowerADDate, startDateBS: () => startDateBS, toAD: () => findADfromBS, toBS: () => findBSfromAD, yearMonthDays: () => yearMonthDays }); module.exports = __toCommonJS(nepali_date_exports); // src/utils/data.ts var startDateAD = 19430414; var startDateBS = 20000101; var lowerADDate = -621355968e5; var yearMonthDays = [ [30, 32, 31, 32, 31, 30, 30, 30, 29, 30, 29, 31], // 2000 BS [31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30], [31, 31, 32, 32, 31, 30, 30, 29, 30, 29, 30, 30], [31, 32, 31, 32, 31, 30, 30, 30, 29, 29, 30, 31], [30, 32, 31, 32, 31, 30, 30, 30, 29, 30, 29, 31], [31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30], [31, 31, 32, 32, 31, 30, 30, 29, 30, 29, 30, 30], [31, 32, 31, 32, 31, 30, 30, 30, 29, 29, 30, 31], [31, 31, 31, 32, 31, 31, 29, 30, 30, 29, 29, 31], [31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30], [31, 31, 32, 32, 31, 30, 30, 29, 30, 29, 30, 30], // 2010 [31, 32, 31, 32, 31, 30, 30, 30, 29, 29, 30, 31], [31, 31, 31, 32, 31, 31, 29, 30, 30, 29, 30, 30], [31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30], [31, 31, 32, 32, 31, 30, 30, 29, 30, 29, 30, 30], [31, 32, 31, 32, 31, 30, 30, 30, 29, 29, 30, 31], [31, 31, 31, 32, 31, 31, 29, 30, 30, 29, 30, 30], [31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30], [31, 32, 31, 32, 31, 30, 30, 29, 30, 29, 30, 30], [31, 32, 31, 32, 31, 30, 30, 30, 29, 30, 29, 31], [31, 31, 31, 32, 31, 31, 30, 29, 30, 29, 30, 30], // 2020 [31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30], [31, 32, 31, 32, 31, 30, 30, 30, 29, 29, 30, 30], [31, 32, 31, 32, 31, 30, 30, 30, 29, 30, 29, 31], [31, 31, 31, 32, 31, 31, 30, 29, 30, 29, 30, 30], [31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30], [31, 32, 31, 32, 31, 30, 30, 30, 29, 29, 30, 31], [30, 32, 31, 32, 31, 30, 30, 30, 29, 30, 29, 31], [31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30], [31, 31, 32, 31, 32, 30, 30, 29, 30, 29, 30, 30], [31, 32, 31, 32, 31, 30, 30, 30, 29, 29, 30, 31], // 2030 [30, 32, 31, 32, 31, 30, 30, 30, 29, 30, 29, 31], [31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30], [31, 31, 32, 32, 31, 30, 30, 29, 30, 29, 30, 30], [31, 32, 31, 32, 31, 30, 30, 30, 29, 29, 30, 31], [30, 32, 31, 32, 31, 31, 29, 30, 30, 29, 29, 31], [31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30], [31, 31, 32, 32, 31, 30, 30, 29, 30, 29, 30, 30], [31, 32, 31, 32, 31, 30, 30, 30, 29, 29, 30, 31], [31, 31, 31, 32, 31, 31, 29, 30, 30, 29, 30, 30], [31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30], // 2040 [31, 31, 32, 32, 31, 30, 30, 29, 30, 29, 30, 30], [31, 32, 31, 32, 31, 30, 30, 30, 29, 29, 30, 31], [31, 31, 31, 32, 31, 31, 29, 30, 30, 29, 30, 30], [31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30], [31, 32, 31, 32, 31, 30, 30, 29, 30, 29, 30, 30], [31, 32, 31, 32, 31, 30, 30, 30, 29, 29, 30, 31], [31, 31, 31, 32, 31, 31, 30, 29, 30, 29, 30, 30], [31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30], [31, 32, 31, 32, 31, 30, 30, 30, 29, 29, 30, 30], [31, 32, 31, 32, 31, 30, 30, 30, 29, 30, 29, 31], // 2050 [31, 31, 31, 32, 31, 31, 30, 29, 30, 29, 30, 30], [31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30], [31, 32, 31, 32, 31, 30, 30, 30, 29, 29, 30, 30], [31, 32, 31, 32, 31, 30, 30, 30, 29, 30, 29, 31], [31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30], [31, 31, 32, 31, 32, 30, 30, 29, 30, 29, 30, 30], [31, 32, 31, 32, 31, 30, 30, 30, 29, 29, 30, 31], [30, 32, 31, 32, 31, 30, 30, 30, 29, 30, 29, 31], [31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30], [31, 31, 32, 32, 31, 30, 30, 29, 30, 29, 30, 30], // 2060 [31, 32, 31, 32, 31, 30, 30, 30, 29, 29, 30, 31], [30, 32, 31, 32, 31, 31, 29, 30, 29, 30, 29, 31], [31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30], [31, 31, 32, 32, 31, 30, 30, 29, 30, 29, 30, 30], [31, 32, 31, 32, 31, 30, 30, 30, 29, 29, 30, 31], [31, 31, 31, 32, 31, 31, 29, 30, 30, 29, 29, 31], [31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30], [31, 31, 32, 32, 31, 30, 30, 29, 30, 29, 30, 30], [31, 32, 31, 32, 31, 30, 30, 30, 29, 29, 30, 31], [31, 31, 31, 32, 31, 31, 29, 30, 30, 29, 30, 30], // 2070 [31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30], [31, 32, 31, 32, 31, 30, 30, 29, 30, 29, 30, 30], [31, 32, 31, 32, 31, 30, 30, 30, 29, 29, 30, 31], [31, 31, 31, 32, 31, 31, 30, 29, 30, 29, 30, 30], [31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30], [31, 32, 31, 32, 31, 30, 30, 30, 29, 29, 30, 30], [31, 32, 31, 32, 31, 30, 30, 30, 29, 30, 29, 31], [31, 31, 31, 32, 31, 31, 30, 29, 30, 29, 30, 30], [31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30], [31, 32, 31, 32, 31, 30, 30, 30, 29, 29, 30, 30], // 2080 [31, 32, 31, 32, 31, 30, 30, 30, 29, 30, 29, 31], // 2081 BS [31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30], // 2082 [31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30], // 2083 [31, 31, 32, 31, 31, 30, 30, 30, 29, 30, 30, 30], // 2084 [31, 32, 31, 32, 30, 31, 30, 30, 29, 30, 30, 30], // 2085 [30, 32, 31, 32, 31, 30, 30, 30, 29, 30, 30, 30], // 2086 [31, 31, 32, 31, 31, 31, 30, 30, 29, 30, 30, 30], // 2087 [30, 31, 32, 32, 30, 31, 30, 30, 29, 30, 30, 30], // 2088 [30, 32, 31, 32, 31, 30, 30, 30, 29, 30, 30, 30] // 2089 ]; var baishakOne = [ 19430414, 19440413, 19450413, 19460413, 19470414, 19480413, 19490413, 19500413, 19510414, 19520413, 19530413, 19540413, 19550414, 19560413, 19570413, 19580413, 19590414, 19600413, 19610413, 19620413, 19630414, 19640413, 19650413, 19660413, 19670414, 19680413, 19690413, 19700414, 19710414, 19720413, 19730413, 19740414, 19750414, 19760413, 19770413, 19780414, 19790414, 19800413, 19810413, 19820414, 19830414, 19840413, 19850413, 19860414, 19870414, 19880413, 19890413, 19900414, 19910414, 19920413, 19930413, 19940414, 19950414, 19960413, 19970413, 19980414, 19990414, 20000413, 20010414, 20020414, 20030414, 20040413, 20050414, 20060414, 20070414, 20080413, 20090414, 20100414, 20110414, 20120413, 20130414, 20140414, 20150414, 20160413, 20170414, 20180414, 20190414, 20200413, 20210414, 20220414, 20230414, 20240413, 20250414, 20260414, 20270414, 20280413, 20290414, 20300414, 20310415, 20320414, 20330414 ]; var ENGLISH_WEEK = ["Su", "Mo", "Tu", "We", "Th", "Fr", "Sa"]; var ENGLISH_WEEK_FULL = [ "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday" ]; var NEPALI_DIGITS = { "0": "\u0966", "1": "\u0967", "2": "\u0968", "3": "\u0969", "4": "\u096A", "5": "\u096B", "6": "\u096C", "7": "\u096D", "8": "\u096E", "9": "\u096F" }; var NEPALI_WEEK = [ "\u0906\u0907\u0924", "\u0938\u094B\u092E", "\u092E\u0902\u0917\u0932", "\u092C\u0941\u0927", "\u092C\u093F\u0939\u093F", "\u0936\u0941\u0915\u094D\u0930", "\u0936\u0928\u093F" ]; var NEPALI_WEEK_FULL = [ "\u0906\u0907\u0924\u092C\u093E\u0930", "\u0938\u094B\u092E\u092C\u093E\u0930", "\u092E\u0902\u0917\u0932\u092C\u093E\u0930", "\u092C\u0941\u0927\u092C\u093E\u0930", "\u092C\u093F\u0939\u0940\u092C\u093E\u0930", "\u0936\u0941\u0915\u094D\u0930\u092C\u093E\u0930", "\u0936\u0928\u093F\u092C\u093E\u0930" ]; var ENGLISH_NEPALI_MONTH = [ "Baisakh", "Jestha", "Asar", "Shrawan", "Bhadra", "Aswin", "Kartik", "Mangsir", "Poush", "Magh", "Falgun", "Chaitra" ]; var NEPALI_MONTH = [ "\u092C\u0948\u0936\u093E\u0916", "\u091C\u0947\u0920", "\u0905\u0938\u093E\u0930", "\u0936\u094D\u0930\u093E\u0935\u0923", "\u092D\u093E\u0926\u094D\u0930", "\u0906\u0936\u094D\u0935\u093F\u0928", "\u0915\u093E\u0930\u094D\u0924\u093F\u0915", "\u092E\u0902\u0938\u093F\u0930", "\u092A\u094C\u0937", "\u092E\u093E\u0918", "\u092B\u093E\u0932\u094D\u0917\u0941\u0923", "\u091A\u0948\u0924\u094D\u0930" ]; var AD_MONTH = [ "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" ]; var AD_MONTH_FULL = [ "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" ]; var AD_MONTH_NEPALI = [ "\u091C\u0928\u0935\u0930\u0940", "\u092B\u0947\u092C\u094D\u0930\u0941\u0905\u0930\u0940", "\u092E\u093E\u0930\u094D\u091A", "\u0905\u092A\u094D\u0930\u093F\u0932", "\u092E\u0947", "\u091C\u0941\u0928", "\u091C\u0941\u0932\u093E\u0908", "\u0905\u0917\u0938\u094D\u091F", "\u0938\u0947\u092A\u094D\u091F\u0947\u092E\u094D\u092C\u0930", "\u0905\u0915\u094D\u091F\u094B\u092C\u0930", "\u0928\u094B\u092D\u0947\u092E\u094D\u092C\u0930", "\u0921\u093F\u0938\u0947\u092E\u094D\u092C\u0930" ]; var NEPALI_TODAY = "\u0906\u091C"; // src/utils/commons.ts var reg = /^\d{4}-(0[1-9]|1[0-2])-(0[1-9]|[12]\d|3[012])$/; var engToNepaliNumber = (number) => { return number.toString().split("").map((n) => NEPALI_DIGITS[n]).join(""); }; var engToNepNumberFullDate = (date) => { return date.split("").map((d) => { if (d !== "-") { return NEPALI_DIGITS[d]; } return "-"; }).join(""); }; var parseDate = (date) => { if (reg.test(date)) { const dd = date.split("-"); if (dd.length !== 3) { return null; } const y = parseInt(dd[0], 10); const m = parseInt(dd[1], 10); const d = parseInt(dd[2], 10); return { date: d, year: y, month: m }; } return null; }; var padZero = (num) => `${num}`.padStart(2, "0"); var convertLang = (value, lang) => lang === "en" ? value : engToNepaliNumber(value); var formatADDate = (date, format = "", lang = "en") => { const enYYYY = date.getFullYear(); const enYY = String(enYYYY).slice(-2); const enM = date.getMonth() + 1; const enD = date.getDate(); const enDD = padZero(enD); const enMM = padZero(enM); const end = date.getDay(); const dateObj = { year: enYYYY, month: enM, date: enD }; const strDateObj = stringDateFormatter(dateObj); if (!format) { return lang === "en" ? strDateObj : engToNepNumberFullDate(strDateObj); } const DD = convertLang(enDD, lang); const D = convertLang(enD, lang); const YYYY = convertLang(enYYYY, lang); const YY = convertLang(enYY, lang); const M = convertLang(enM, lang); const MM = convertLang(enMM, lang); const MMM = lang === "en" ? AD_MONTH[date.getMonth()] : AD_MONTH_NEPALI[date.getMonth()]; const MMMM = lang === "en" ? AD_MONTH_FULL[date.getMonth()] : AD_MONTH_NEPALI[date.getMonth()]; const d = convertLang(end, lang); const dd = lang === "en" ? ENGLISH_WEEK[end] : NEPALI_WEEK[end]; const ddd = lang === "en" ? ENGLISH_WEEK_FULL[end].slice(0, 3) : NEPALI_WEEK[end]; const dddd = lang === "en" ? ENGLISH_WEEK_FULL[end] : NEPALI_WEEK_FULL[end]; const replacements = { DD, D, YYYY, YY, M, MM, MMM, MMMM, d, // Abbreviated day name dd, // Abbreviated day name ddd, // Abbreviated day name dddd // Full day name }; const tokenReg = /(\[.*?\])|(MMMM|MMM|dddd|ddd|dd|d|DD|D|MM|M|YYYY|YY)/g; return format.replace(tokenReg, (match, literal) => { if (literal) { return literal.slice(1, -1); } return replacements[match]; }); }; // src/utils/calendar.ts var toInt = (value) => ~~value; var yearDivider = 1e4; var monthDivider = 100; var padTwo = (v) => `${v}`.padStart(2, "0"); var padFour = (v) => `${v}`.padStart(4, "0"); var stringDateFormatter = ({ year, month, date }) => `${padFour(year)}-${padTwo(month)}-${padTwo(date)}`; var getDateFromNumber = (n) => { const year = toInt(n / yearDivider); const month = toInt(n % yearDivider / monthDivider); const date = n % (yearDivider / monthDivider); const dateData = { year, month, date }; return { ...dateData, toString: () => stringDateFormatter(dateData) }; }; var getBSYearIndexFromAd = (year) => { const startADYear = getDateFromNumber(startDateAD).year; if (year < startADYear) { throw new Error(`Invalid year, year should be greater than ${startADYear}`); } return year - startADYear; }; var getBSYearIndexFromBS = (year) => { const startBSYear = getDateFromNumber(startDateBS).year; if (year < startBSYear) { throw new Error(`Invalid year, year should be greater than ${startBSYear}`); } return year - startBSYear; }; var getDays = (a, b) => (a.getTime() - b.getTime()) / (1e3 * 3600 * 24) + 1; var nepaliDateExtractor = (datestring) => { if (!datestring) { throw new Error("Invalid Date"); } const startBSDate = getDateFromNumber(startDateBS); const datesplit = datestring.split("-"); if (datesplit.length !== 3) { throw new Error("Invalid Date"); } const year = parseInt(datesplit[0], 10); const month = parseInt(datesplit[1], 10); const date = parseInt(datesplit[2], 10); if (year < startBSDate.year || year > startBSDate.year + yearMonthDays.length - 1 || date <= 0 || date > 32 || month > 12 || month < 0) { throw new Error("Invalid Date"); } return { year, month, date }; }; var findADfromBS = (date) => { const d = nepaliDateExtractor(date); const year = d.year - getDateFromNumber(startDateBS).year; let days = 0; for (let x = 0; x < d.month - 1; x++) { days += yearMonthDays[year][x]; } days += d.date - 1; const adIndexDate = getDateFromNumber(baishakOne[year]); const currentDate = new Date(stringDateFormatter({ ...adIndexDate })); currentDate.setDate(adIndexDate.date + days); const dateData = { year: currentDate.getFullYear(), month: currentDate.getMonth(), date: currentDate.getDate() }; return { ...dateData, toString: () => stringDateFormatter({ ...dateData, month: dateData.month + 1 }) }; }; var findBSfromAD = (date) => { const d = new Date(date); const adYear = d.getFullYear(); let yearIndex = getBSYearIndexFromAd(adYear); let baishakOneInADForGivenYear = getDateFromNumber(baishakOne[yearIndex]); let dateForBaishakOneInADForGivenYear = new Date( baishakOneInADForGivenYear.toString() ); let days = getDays(d, dateForBaishakOneInADForGivenYear); if (days <= 0) { yearIndex -= 1; baishakOneInADForGivenYear = getDateFromNumber(baishakOne[yearIndex]); dateForBaishakOneInADForGivenYear = new Date( baishakOneInADForGivenYear.toString() ); days = getDays(d, dateForBaishakOneInADForGivenYear); } const startBSDate = getDateFromNumber(startDateBS); if (yearIndex + startBSDate.year > startBSDate.year + yearMonthDays.length - 1 || yearIndex + startBSDate.year < startBSDate.year) { throw new Error("Invalid date"); } const currentBSYearMonths = yearMonthDays[yearIndex]; let totalMonths = 0; let totalDays = 0; for (let x = 0; x < days; x++) { totalDays += 1; if (totalDays > currentBSYearMonths[totalMonths]) { totalMonths += 1; totalDays = 1; continue; } } const dateData = { year: yearIndex + getDateFromNumber(startDateBS).year, month: totalMonths, date: totalDays }; return { ...dateData, toString: () => stringDateFormatter({ ...dateData, month: dateData.month + 1 }) }; }; var getCurrentDate = ({ type = "np" }) => { const date = /* @__PURE__ */ new Date(); const formatDate = (date2) => { const year = date2.getFullYear(); const month = String(date2.getMonth() + 1).padStart(2, "0"); const day = String(date2.getDate()).padStart(2, "0"); return `${year}-${month}-${day}`; }; switch (type) { case "en": return { year: date.getFullYear(), month: date.getMonth(), date: date.getDate(), day: date.getDay() }; case "np": default: return { ...findBSfromAD(formatDate(date)), day: date.getDay() }; } }; var manipulate = (currentDate, value) => { const parsed = parseDate(currentDate); if (parsed) { return parsed; } else { throw Error( `Invalid date ${currentDate} to ${value < 0 ? "subtract" : "add"}.` ); } }; var manipulateDays = (currentDate, days) => { const parsed = manipulate(currentDate, days); const ad = findADfromBS(stringDateFormatter(parsed)); const date = new Date(ad.toString()); date.setDate(date.getDate() + days); try { return findBSfromAD( stringDateFormatter({ year: date.getFullYear(), month: date.getMonth() + 1, date: date.getDate() }) ); } catch (err) { if (days < 0) { throw Error( `Cannot subtract day(s). Subtracting ${Math.abs(days)} day(s) results in date smaller than ${getDateFromNumber(startDateBS).year}` ); } throw Error( `Cannot add day(s). Adding ${Math.abs(days)} day(s) results in date greater than ${getDateFromNumber(startDateBS).year + yearMonthDays.length - 1}` ); } }; var manipulateWeeks = (currentDate, weeks) => { const parsed = manipulate(currentDate, weeks); const ad = findADfromBS(stringDateFormatter(parsed)); const date = new Date(ad.toString()); date.setDate(date.getDate() + weeks * 7); try { return findBSfromAD( stringDateFormatter({ year: date.getFullYear(), month: date.getMonth() + 1, date: date.getDate() }) ); } catch (err) { if (weeks < 0) { throw Error( `Cannot subtract week(s). Subtracting ${Math.abs(weeks)} week(s) results in date smaller than ${getDateFromNumber(startDateBS).year}` ); } throw Error( `Cannot add week(s). Adding ${Math.abs(weeks)} week(s) results in date greater than ${getDateFromNumber(startDateBS).year + yearMonthDays.length - 1}` ); } }; var manipulateMonths = (currentDate, months) => { const parsed = manipulate(currentDate, months); const { year, month, date } = parsed; let yearIndex = getBSYearIndexFromBS(year); let incMonth = month - 1 + months; yearIndex += Math.floor(incMonth / 12); if (yearIndex > yearMonthDays.length - 1 || yearIndex < 0) { if (months < 0) { throw Error( `Cannot subtract month(s). Subtracting ${Math.abs(months)} month(s) results in date smaller than ${getDateFromNumber(startDateBS).year}` ); } throw Error( `Cannot add month(s). Adding ${Math.abs(months)} month(s) results in date greater than ${getDateFromNumber(startDateBS).year + yearMonthDays.length - 1}` ); } incMonth %= 12; if (incMonth < 0) { incMonth = 12 + incMonth; } let incDate = date; const daysInIncMonth = yearMonthDays[yearIndex][incMonth]; if (incDate > daysInIncMonth) { incMonth += 1; incDate -= daysInIncMonth; if (incMonth > 11) { incMonth = 0; yearIndex += 1; } } if (yearIndex > yearMonthDays.length - 1 || yearIndex < 0) { if (months < 0) { throw Error( `Cannot subtract month(s). Subtracting ${Math.abs(months)} month(s) results in date smaller than ${getDateFromNumber(startDateBS).year}` ); } throw Error( `Cannot add month(s). Adding ${Math.abs(months)} month(s) results in date greater than ${getDateFromNumber(startDateBS).year + yearMonthDays.length - 1}` ); } return { year: getDateFromNumber(startDateBS).year + yearIndex, month: incMonth, date: incDate }; }; var manipulateYears = (currentDate, years) => { const parsed = manipulate(currentDate, years); const { year, month, date } = parsed; let yearIndex = getBSYearIndexFromBS(year); yearIndex += years; if (yearIndex > yearMonthDays.length - 1 || yearIndex < 0) { if (years < 0) { throw Error( `Cannot subtract year(s). Subtracting ${Math.abs(years)} year(s) results in date smaller than ${getDateFromNumber(startDateBS).year}` ); } throw Error( `Cannot add year(s). Adding ${Math.abs(years)} year(s) results in date greater than ${getDateFromNumber(startDateBS).year + yearMonthDays.length - 1}` ); } let incMonth = month - 1; const daysInIncMonth = yearMonthDays[yearIndex][incMonth]; let incDate = date; if (incDate > daysInIncMonth) { incMonth += 1; incDate -= daysInIncMonth; if (incMonth > 11) { incMonth = 0; yearIndex += 1; } } if (yearIndex > yearMonthDays.length - 1 || yearIndex < 0) { if (years < 0) { throw Error( `Cannot subtract year(s). Subtracting ${Math.abs(years)} year(s) results in date smaller than ${getDateFromNumber(startDateBS).year}` ); } throw Error( `Cannot add year(s). Adding ${Math.abs(years)} year(s) results in date greater than ${getDateFromNumber(startDateBS).year + yearMonthDays.length - 1}` ); } return { year: getDateFromNumber(startDateBS).year + yearIndex, month: incMonth, date: incDate }; }; // src/nepali-date/nepali-date.ts var NepaliDate = class _NepaliDate { #year = 2e3; #month = 0; #date = 1; #adDate = /* @__PURE__ */ new Date(); constructor(yearOrValue, month, date) { if (typeof yearOrValue === "number") { this.#year = yearOrValue; this.#month = month; this.#date = date; this.#setADDate(); } else if (typeof yearOrValue === "string" || yearOrValue instanceof _NepaliDate || yearOrValue instanceof Date) { if (typeof yearOrValue === "string") { const parsed = parseDate(yearOrValue); if (!parsed) { throw new Error("Invalid date"); } this.#year = parsed.year; this.#month = parsed.month - 1; this.#date = parsed.date; } else if (yearOrValue instanceof _NepaliDate) { this.#year = yearOrValue.#year; this.#month = yearOrValue.#month; this.#date = yearOrValue.#date; } else { const adDate = yearOrValue; const stringDate = stringDateFormatter({ year: adDate.getFullYear(), month: adDate.getMonth() + 1, date: adDate.getDate() }); const d = findBSfromAD(stringDate); this.#year = d.year; this.#month = d.month; this.#date = d.date; } } else { const today = getCurrentDate({ type: "np" }); this.#year = today.year; this.#month = today.month; this.#date = today.date; } this.#setADDate(); } #setADDate = () => { this.#adDate = new Date( findADfromBS( stringDateFormatter({ year: this.#year, month: this.#month + 1, date: this.#date }) ).toString() ); }; format(format = "", lang = "en") { if (!format) { return this.toString(lang); } const date = padZero(this.getDate("en")); const month = padZero(this.#month + 1); const replacements = { DD: lang === "en" ? date : engToNepaliNumber(date), D: this.getDate(lang), YYYY: String(this.getFullYear(lang)), YY: String(this.getFullYear(lang)).slice(-2), M: lang === "en" ? this.#month + 1 : engToNepaliNumber(this.#month + 1), MM: lang === "en" ? month : engToNepaliNumber(month), MMM: lang === "en" ? this.getMonthName(lang).slice(0, 3) : this.getMonthName(lang), MMMM: this.getMonthName(lang), d: this.getDay(lang), // Abbreviated day name dd: lang === "en" ? this.getDayNameFull(lang).slice(0, 2) : this.getDayName(lang), // Abbreviated day name ddd: lang === "en" ? this.getDayNameFull(lang).slice(0, 3) : this.getDayName(lang), // Abbreviated day name dddd: this.getDayNameFull(lang) // Full day name }; const tokenReg = /(\[.*?\])|(MMMM|MMM|dddd|ddd|dd|d|DD|D|MM|M|YYYY|YY)/g; return format.replace(tokenReg, (match, literal) => { if (literal) { return literal.slice(1, -1); } return replacements[match]; }); } toString(lang = "en") { const d = stringDateFormatter({ year: this.#year, month: this.#month + 1, date: this.#date }); switch (lang) { case "en": return d; case "np": default: { return engToNepNumberFullDate(d); } } } toJson(lang = "en") { const d = { year: this.#year, month: this.#month, date: this.#date, day: this.#adDate.getDay() }; switch (lang) { case "en": return d; case "np": default: { return { year: engToNepaliNumber(this.#year), month: engToNepaliNumber(this.#month), date: engToNepaliNumber(this.#date), day: engToNepaliNumber(this.#adDate.getDay()) }; } } } toAD(lang = "en") { const d = findADfromBS( stringDateFormatter({ year: this.#year, month: this.#month + 1, date: this.#date }) ); switch (lang) { case "en": return d; case "np": default: { return { year: engToNepaliNumber(d.year), month: engToNepaliNumber(d.month), date: engToNepaliNumber(d.date), toString: () => engToNepNumberFullDate(d.toString()) }; } } } toADasDate() { return new Date(this.toAD("en").toString()); } getFullYear(lang = "en") { return lang === "en" ? this.#year : engToNepaliNumber(this.#year); } getMonth(lang = "en") { return lang === "en" ? this.#month : engToNepaliNumber(this.#month); } getMonthName(lang = "en") { return this.#getMonthByIndex(this.#month, lang); } #getMonthByIndex(index, lang = "en") { if (index < 0 || index > 11) { throw new Error("Invalid month"); } return lang === "en" ? ENGLISH_NEPALI_MONTH[index] : NEPALI_MONTH[index]; } getDate(lang = "en") { return lang === "en" ? this.#date : engToNepaliNumber(this.#date); } getDay(lang = "en") { return lang === "en" ? this.#adDate.getDay() : engToNepaliNumber(this.#adDate.getDay()); } getDayName(lang = "en") { return this.#getDayByIndex(this.#adDate.getDay(), lang, false); } getDayNameFull(lang = "en") { return this.#getDayByIndex(this.#adDate.getDay(), lang, true); } #getDayByIndex(index, lang = "en", full = false) { if (index < 0 || index > 6) { throw new Error("Invalid day"); } if (full) { return lang === "en" ? ENGLISH_WEEK_FULL[index] : NEPALI_WEEK_FULL[index]; } return lang === "en" ? ENGLISH_WEEK[index] : NEPALI_WEEK[index]; } #manipulate(v, type) { const cDate = this.toString(); switch (type) { case "day": case "d": return manipulateDays(cDate, v); case "week": case "w": return manipulateWeeks(cDate, v); case "month": case "m": return manipulateMonths(cDate, v); case "year": case "y": return manipulateYears(cDate, v); default: throw Error( "Invalid type. Type must be one of 'day'|'month'|'year'|'d'|'m'|'y'" ); } } subtract(value, type) { const v = -Math.abs(value); const m = this.#manipulate(v, type); return new _NepaliDate(m.year, m.month, m.date); } add(value, type) { const v = Math.abs(value); const m = this.#manipulate(v, type); return new _NepaliDate(m.year, m.month, m.date); } setDate(date) { if (date > 0) { if (date >= this.#date) { const dateToAdd = date - this.#date; try { const { year, month, date: d } = this.add(dateToAdd, "d").toJson("en"); this.#date = d; this.#year = year; this.#month = month; this.#setADDate(); } catch { throw Error(`Date exceeds limit.`); } } else { this.#date = date; } } else { this.#date = 1; try { const { year, month, date: d } = this.subtract(date - 1, "d").toJson("en"); this.#date = d; this.#year = year; this.#month = month; this.#setADDate(); } catch { throw Error(`Date exceeds limit.`); } } } setMonth(month) { if (month >= 0) { if (month >= this.#month) { const monthToAdd = month - this.#month; try { const { year, month: m, date } = this.add(monthToAdd, "m").toJson("en"); this.#date = date; this.#year = year; this.#month = m; this.#setADDate(); } catch { throw Error(`Date exceeds limit.`); } } else { this.#month = month; } } else { this.#month = 0; try { const { year, month: m, date } = this.subtract(month, "m").toJson("en"); this.#date = date; this.#year = year; this.#month = m; this.#setADDate(); } catch { throw Error(`Date exceeds limit.`); } } } setFullYear(year) { const sDate = getDateFromNumber(startDateBS).year; if (year < sDate) { throw Error(`Invalid year. Year cannot be less than $${sDate}`); } else { this.#year = year; } } static now(lang = "en") { const d = getCurrentDate({ type: "np" }); return lang === "en" ? d : { year: engToNepaliNumber(d.year), month: engToNepaliNumber(d.month), date: engToNepaliNumber(d.date), day: engToNepaliNumber(d.day), toString: () => engToNepNumberFullDate(d.toString()) }; } }; var nepali_date_default = NepaliDate; // src/nepali-date/index.ts var nepali_date_default2 = nepali_date_default;