-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmod.ts
More file actions
233 lines (217 loc) · 5.46 KB
/
mod.ts
File metadata and controls
233 lines (217 loc) · 5.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
/**
* Constants for Nepali calendar months and weekdays
* @public
*/
export const MONTHS_EN = [
"Baisakh",
"Jestha",
"Asar",
"Shrawan",
"Bhadra",
"Aswin",
"Kartik",
"Mangsir",
"Poush",
"Magh",
"Falgun",
"Chaitra",
];
export const MONTHS_NP = [
"बैशाख",
"जेठ",
"असार",
"श्रावण",
"भाद्र",
"आश्विन",
"कार्तिक",
"मंसिर",
"पौष",
"माघ",
"फाल्गुण",
"चैत्र",
];
export const WEEKDAYS_NP = [
"आइतबार",
"सोमबार",
"मंगलबार",
"बुधबार",
"बिहिबार",
"शुक्रबार",
"शनिबार",
];
export const NUM_NP = ["०", "१", "२", "३", "४", "५", "६", "७", "८", "९"];
/**
* Interface for Nepali date object
* @public
*/
export interface NepaliDate {
year: number; // Nepali year (e.g., 2080)
month: number; // Month (1-12)
day: number; // Day of month (1-32)
weekday: number; // Day of week (0-6, 0 = Sunday)
monthName: string; // Nepali month name
weekdayName: string; // Nepali weekday name
}
/**
* Converts English digits to Nepali unicode digits
* @example
* toNepaliDigits(123) // returns "१२३"
* toNepaliDigits("45") // returns "४५"
*/
export function toNepaliDigits(num: number | string): string {
return num
.toString()
.split("")
.map((char) => (char >= "0" && char <= "9" ? NUM_NP[parseInt(char)] : char))
.join("");
}
/**
* Creates a NepaliDate object with date information
* @param year - Nepali year (e.g., 2080)
* @param month - Month (1-12)
* @param day - Day of month
* @throws {Error} If date is invalid
* @example
* const date = createNepaliDate(2080, 1, 15);
* console.log(date.monthName); // बैशाख
* console.log(date.weekdayName); // आइतबार
*/
export function createNepaliDate(
year: number,
month: number,
day: number
): NepaliDate {
validateNepaliDate(year, month, day);
const date = new Date(year, month - 1, day);
const weekday = date.getDay();
return {
year,
month,
day,
weekday,
monthName: MONTHS_NP[month - 1],
weekdayName: WEEKDAYS_NP[weekday],
};
}
/**
* Formats a Nepali date according to the specified format string
* @param year - Nepali year
* @param month - Month (1-12)
* @param day - Day of month
* @param options - Formatting options
* @throws {Error} If date is invalid
*
* @example
* // Default format (YYYY-MM-DD)
* formatNepaliDate(2080, 1, 15) // २०८०-०१-१५
*
* // Custom format with English
* formatNepaliDate(2080, 1, 15, {
* format: "MMMM DD, YYYY",
* language: "en"
* }) // Baisakh 15, 2080
*
* // Custom format with Nepali
* formatNepaliDate(2080, 1, 15, {
* format: "MMMM DD, YYYY",
* language: "np"
* }) // बैशाख १५, २०८०
*/
export function formatNepaliDate(
year: number,
month: number,
day: number,
options: { format?: string; language?: "en" | "np" } = {}
): string {
const { format = "YYYY-MM-DD", language = "np" } = options;
validateNepaliDate(year, month, day);
let result = format;
const monthIndex = month - 1;
const date = new Date(year, monthIndex, day);
const weekday = date.getDay();
if (language === "np") {
result = result
.replace("YYYY", toNepaliDigits(year))
.replace("YY", toNepaliDigits(year % 100))
.replace("MM", toNepaliDigits(month.toString().padStart(2, "0")))
.replace("DD", toNepaliDigits(day.toString().padStart(2, "0")))
.replace("MMMM", MONTHS_NP[monthIndex])
.replace("dddd", WEEKDAYS_NP[weekday]);
} else {
result = result
.replace("YYYY", year.toString())
.replace("YY", (year % 100).toString().padStart(2, "0"))
.replace("MM", month.toString().padStart(2, "0"))
.replace("DD", day.toString().padStart(2, "0"))
.replace("MMMM", MONTHS_EN[monthIndex])
.replace("dddd", WEEKDAYS_NP[weekday]);
}
return result;
}
/**
* Returns the number of days in a given Nepali month
* @param month - Month number (1-12)
* @returns Number of days in the month
* @example
* getDaysInMonth(1) // 31 (Baisakh)
* getDaysInMonth(3) // 32 (Asar)
*/
export function getDaysInMonth(month: number): number {
const daysInMonth: Record<number, number> = {
1: 31,
2: 31,
3: 32,
4: 32,
5: 31,
6: 31,
7: 30,
8: 30,
9: 30,
10: 29,
11: 30,
12: 30,
};
return daysInMonth[month] || 30;
}
/**
* Validates a Nepali date
* @throws {Error} If the date is invalid
*/
export function validateNepaliDate(
year: number,
month: number,
day: number
): void {
if (
year < 1970 ||
year > 2100 ||
month < 1 ||
month > 12 ||
day < 1 ||
day > getDaysInMonth(month)
) {
throw new Error(`Invalid Nepali date: ${year}-${month}-${day}`);
}
}
/**
* Compares two Nepali dates
* @returns Negative if date1 < date2, 0 if equal, positive if date1 > date2
* @example
* compareNepaliDates(2080, 1, 15, 2080, 2, 1) // returns negative number
* compareNepaliDates(2080, 1, 15, 2080, 1, 15) // returns 0
*/
export function compareNepaliDates(
year1: number,
month1: number,
day1: number,
year2: number,
month2: number,
day2: number
): number {
validateNepaliDate(year1, month1, day1);
validateNepaliDate(year2, month2, day2);
if (year1 !== year2) return year1 - year2;
if (month1 !== month2) return month1 - month2;
return day1 - day2;
}
export const VERSION = "0.1.3";