dayjskh 0.1.1 → 0.1.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/index.html CHANGED
@@ -9,8 +9,7 @@
9
9
  <div id="app">
10
10
  <h1></h1>
11
11
  <h2></h2>
12
- <h3></h3>
13
- <pre></pre>
12
+
14
13
  <div
15
14
  style="
16
15
  display: flex;
@@ -27,36 +26,9 @@
27
26
  import toKhmerDate from "./src/main.ts";
28
27
  dayjs.extend(toKhmerDate);
29
28
  const date = dayjs();
30
- console.log(date.khNewYear());
31
29
  document.querySelector("h1").innerHTML = date.toKhmerDate();
32
30
  // document.querySelector("h2").innerHTML = date.format("DD/MM/YYYY HH:mm");
33
- document.querySelector("pre").innerHTML = JSON.stringify(
34
- date.year(2024).khNewYear(),
35
- );
36
- // .format("DD/MM/YYYY HH:mm");
37
- // console.log(date.khNewYear(2024).format("DD/MM/YYYY HH:mm"));
38
- // loop 5 days
39
- const ul = document.querySelector("ul");
40
- for (let i = 0; i < 30; i++) {
41
- const li = document.createElement("li");
42
- li.innerHTML = `${date
43
- .add(i, "day")
44
- .toKhmerDate("ថ្ងៃ W DN ខែ m")} - ${date
45
- .add(i, "day")
46
- .format("DD/MM/YYYY")}`;
47
- ul.appendChild(li);
48
- }
49
- const date2 = dayjs("2050-01-01T00:00:00+07:00");
50
- const ul2 = document.querySelector("#ul2");
51
- for (let i = 0; i < 30; i++) {
52
- const li = document.createElement("li");
53
- li.innerHTML = `${date2
54
- .add(i, "day")
55
- .toKhmerDate("ថ្ងៃ W DN ខែ m")} - ${date2
56
- .add(i, "day")
57
- .format("DD/MM/YYYY")}`;
58
- ul2.appendChild(li);
59
- }
31
+ document.querySelector("h2").innerHTML = date.khNewYear().date.format();
60
32
  </script>
61
33
  </body>
62
34
  </html>
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "dayjskh",
3
- "version": "0.1.1",
3
+ "version": "0.1.2",
4
4
  "description": "Khmer Lunar date Plugin for Day.js",
5
5
  "main": "./src/main.ts",
6
6
  "scripts": {
package/src/dayjskh.ts CHANGED
@@ -10,9 +10,6 @@ import badMutable from "dayjs/esm/plugin/badMutable";
10
10
 
11
11
  dayjs.extend(customParseFormat);
12
12
  dayjs.extend(duration);
13
-
14
- const { lunarMonths } = constant;
15
-
16
13
  export default function dayjskh(date?: dayjs.Dayjs | string) {
17
14
  // check if date is valid
18
15
  const globalDate = dayjs(date); // if date is undefined, it will return current date
@@ -201,43 +198,24 @@ export default function dayjskh(date?: dayjs.Dayjs | string) {
201
198
  // Next month of Khmer month
202
199
 
203
200
  function nextMonthOf(khmerMonth: number, beYear: number): number {
204
- switch (khmerMonth) {
205
- case lunarMonths["មិគសិរ"]:
206
- return lunarMonths["បុស្ស"];
207
- case lunarMonths["បុស្ស"]:
208
- return lunarMonths["មាឃ"];
209
- case lunarMonths["មាឃ"]:
210
- return lunarMonths["ផល្គុន"];
211
- case lunarMonths["ផល្គុន"]:
212
- return lunarMonths["ចេត្រ"];
213
- case lunarMonths["ចេត្រ"]:
214
- return lunarMonths["ពិសាខ"];
215
- case lunarMonths["ពិសាខ"]:
216
- return lunarMonths["ជេស្ឋ"];
217
- case lunarMonths["ជេស្ឋ"]: {
218
- if (isKhmerLeapMonth(beYear)) {
219
- return lunarMonths["បឋមាសាឍ"];
220
- } else {
221
- return lunarMonths["អាសាឍ"];
222
- }
223
- }
224
- case lunarMonths["អាសាឍ"]:
225
- return lunarMonths["ស្រាពណ៍"];
226
- case lunarMonths["ស្រាពណ៍"]:
227
- return lunarMonths["ភទ្របទ"];
228
- case lunarMonths["ភទ្របទ"]:
229
- return lunarMonths["អស្សុជ"];
230
- case lunarMonths["អស្សុជ"]:
231
- return lunarMonths["កក្ដិក"];
232
- case lunarMonths["កក្ដិក"]:
233
- return lunarMonths["មិគសិរ"];
234
- case lunarMonths["បឋមាសាឍ"]:
235
- return lunarMonths["ទុតិយាសាឍ"];
236
- case lunarMonths["ទុតិយាសាឍ"]:
237
- return lunarMonths["ស្រាពណ៍"];
238
- default:
239
- throw Error("Plugin is facing wrong calculation (Invalid month)");
240
- }
201
+ let khmerMonths = [
202
+ 1,
203
+ 2,
204
+ 3,
205
+ 4,
206
+ 5,
207
+ 6,
208
+ isKhmerLeapMonth(beYear) ? 12 : 7,
209
+ 8,
210
+ 9,
211
+ 10,
212
+ 11,
213
+ 0,
214
+ 13,
215
+ 8,
216
+ ];
217
+
218
+ return khmerMonths[khmerMonth];
241
219
  }
242
220
 
243
221
  // calculate date from dayjs to Khmer date
@@ -313,16 +291,15 @@ export default function dayjskh(date?: dayjs.Dayjs | string) {
313
291
 
314
292
  // រកថ្ងៃវិសាខបូជា
315
293
  // ថ្ងៃដាច់ឆ្នាំពុទ្ធសករាជ
316
- function getVisakBochea(
317
- gregorianYear: number,
318
- ): string | number | dayjs.Dayjs | Date | null | undefined | any {
294
+ function getVisakBochea(gregorianYear: number): dayjs.Dayjs {
319
295
  const date = dayjs(`${gregorianYear}-01-01`);
320
- for (var i = 0; i < 365; i++) {
321
- const lunarDate = getLunarDate(date);
322
- if (lunarDate.month === lunarMonths["ពិសាខ"] && lunarDate.day === 14) {
296
+ for (let i = 0; i < 365; i++) {
297
+ const { month, day } = getLunarDate(date);
298
+ if (month === 5 && day === 14) {
323
299
  return date;
300
+ } else {
301
+ date.add(1, "day");
324
302
  }
325
- date.add(1, "day");
326
303
  }
327
304
  }
328
305
 
@@ -416,8 +393,7 @@ export default function dayjskh(date?: dayjs.Dayjs | string) {
416
393
  } {
417
394
  return {
418
395
  count: (day % 15) + 1,
419
- moonStatus:
420
- day > 14 ? constant.moonStatus["រោច"] : constant.moonStatus["កើត"],
396
+ moonStatus: day > 14 ? 0 : 1,
421
397
  };
422
398
  }
423
399
 
@@ -505,13 +481,8 @@ export default function dayjskh(date?: dayjs.Dayjs | string) {
505
481
  // format date to Khmer date
506
482
  function format(format?: string): string {
507
483
  const date = globalDate.clone();
508
- const lunarDate = getLunarDate(date);
509
- const result = formatKhmerDate(
510
- lunarDate.day,
511
- lunarDate.month,
512
- date,
513
- format,
514
- );
484
+ const { day, month } = getLunarDate(date);
485
+ const result = formatKhmerDate(day, month, date, format);
515
486
  return result;
516
487
  }
517
488