dayjskh 0.1.2 → 0.1.3

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
@@ -29,6 +29,15 @@
29
29
  document.querySelector("h1").innerHTML = date.toKhmerDate();
30
30
  // document.querySelector("h2").innerHTML = date.format("DD/MM/YYYY HH:mm");
31
31
  document.querySelector("h2").innerHTML = date.khNewYear().date.format();
32
+
33
+ let date2 = dayjs("2007-01-01");
34
+ for (let i = 0; i < 365; i++) {
35
+ const li = document.createElement("li");
36
+ li.innerHTML = `${date2.add(i, "day").toKhmerDate("dN m a")} - ${date2
37
+ .add(i, "day")
38
+ .format("DD/MM/YYYY")}`;
39
+ document.querySelector("ul").appendChild(li);
40
+ }
32
41
  </script>
33
42
  </body>
34
43
  </html>
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "dayjskh",
3
- "version": "0.1.2",
3
+ "version": "0.1.3",
4
4
  "description": "Khmer Lunar date Plugin for Day.js",
5
5
  "main": "./src/main.ts",
6
6
  "scripts": {
package/src/dayjskh.ts CHANGED
@@ -3,10 +3,9 @@
3
3
 
4
4
  import { constant } from "./constant";
5
5
  import learnSak from "./lerngSak";
6
- import dayjs from "dayjs/esm/index.js";
7
- import duration from "dayjs/esm/plugin/duration";
8
- import customParseFormat from "dayjs/esm/plugin/customParseFormat";
9
- import badMutable from "dayjs/esm/plugin/badMutable";
6
+ import dayjs from "dayjs";
7
+ import duration from "dayjs/plugin/duration";
8
+ import customParseFormat from "dayjs/plugin/customParseFormat";
10
9
 
11
10
  dayjs.extend(customParseFormat);
12
11
  dayjs.extend(duration);
@@ -224,27 +223,30 @@ export default function dayjskh(date?: dayjs.Dayjs | string) {
224
223
  month: number;
225
224
  epochMoved: dayjs.Dayjs;
226
225
  } {
227
- dayjs.extend(badMutable);
228
226
  // Epoch Date: January 1, 1900
229
227
  let epochDayjs = dayjs("1900-01-01");
228
+ let epochDayjsClone = epochDayjs.clone();
230
229
  let khmerMonth = 1;
231
230
  let khmerDay = 0; // 0 - 29 ១កើត ... ១៥កើត ១រោច ...១៤រោច (១៥រោច)
232
231
  // Find nearest year epoch
233
232
  if (targetDate.diff(epochDayjs) > 0) {
234
233
  while (
235
234
  dayjs.duration(targetDate.diff(epochDayjs), "milliseconds").asDays() > // ok
236
- getNumDayOfKhmerYear(getMaybeBEYear(epochDayjs.clone().add(1, "year")))
235
+ getNumDayOfKhmerYear(
236
+ getMaybeBEYear(epochDayjsClone.clone().add(1, "year")),
237
+ )
237
238
  ) {
238
- epochDayjs.add(
239
+ epochDayjs = epochDayjs.add(
239
240
  getNumDayOfKhmerYear(
240
- getMaybeBEYear(epochDayjs.clone().add(1, "year")),
241
+ getMaybeBEYear(epochDayjsClone.clone().add(1, "year")),
241
242
  ),
242
243
  "day",
243
244
  );
245
+ epochDayjsClone = epochDayjsClone.add(1, "year");
244
246
  }
245
247
  } else {
246
248
  do {
247
- epochDayjs.subtract(
249
+ epochDayjs = epochDayjs.subtract(
248
250
  getNumDayOfKhmerYear(getMaybeBEYear(epochDayjs)),
249
251
  "day",
250
252
  );
@@ -257,7 +259,7 @@ export default function dayjskh(date?: dayjs.Dayjs | string) {
257
259
  dayjs.duration(targetDate.diff(epochDayjs), "milliseconds").asDays() >
258
260
  getMaxDayOfKhmerMonth(khmerMonth, getMaybeBEYear(epochDayjs))
259
261
  ) {
260
- epochDayjs.add(
262
+ epochDayjs = epochDayjs.add(
261
263
  getMaxDayOfKhmerMonth(khmerMonth, getMaybeBEYear(epochDayjs)),
262
264
  "day",
263
265
  );
@@ -278,7 +280,7 @@ export default function dayjskh(date?: dayjs.Dayjs | string) {
278
280
  khmerMonth = nextMonthOf(khmerMonth, getMaybeBEYear(epochDayjs));
279
281
  }
280
282
 
281
- epochDayjs.add(
283
+ epochDayjs = epochDayjs.add(
282
284
  dayjs.duration(targetDate.diff(epochDayjs), "milliseconds").asDays(),
283
285
  "day",
284
286
  );
@@ -292,13 +294,13 @@ export default function dayjskh(date?: dayjs.Dayjs | string) {
292
294
  // រកថ្ងៃវិសាខបូជា
293
295
  // ថ្ងៃដាច់ឆ្នាំពុទ្ធសករាជ
294
296
  function getVisakBochea(gregorianYear: number): dayjs.Dayjs {
295
- const date = dayjs(`${gregorianYear}-01-01`);
297
+ let date = dayjs(`${gregorianYear}-01-01`);
296
298
  for (let i = 0; i < 365; i++) {
297
299
  const { month, day } = getLunarDate(date);
298
300
  if (month === 5 && day === 14) {
299
301
  return date;
300
302
  } else {
301
- date.add(1, "day");
303
+ date = date.add(1, "day");
302
304
  }
303
305
  }
304
306
  }
@@ -393,7 +395,7 @@ export default function dayjskh(date?: dayjs.Dayjs | string) {
393
395
  } {
394
396
  return {
395
397
  count: (day % 15) + 1,
396
- moonStatus: day > 14 ? 0 : 1,
398
+ moonStatus: day > 14 ? 1 : 0,
397
399
  };
398
400
  }
399
401
 
@@ -401,16 +403,16 @@ export default function dayjskh(date?: dayjs.Dayjs | string) {
401
403
  function formatKhmerDate(
402
404
  day: number,
403
405
  month: number,
404
- dayjs: dayjs.Dayjs,
406
+ date: dayjs.Dayjs,
405
407
  format: string,
406
408
  ): string {
407
409
  if (format === null || format === undefined || format === "") {
408
410
  // Default date format
409
- const dayOfWeek = dayjs.day();
411
+ const dayOfWeek = date.day();
410
412
  const moonDay = getKhmerLunarDayName(day);
411
- const beYear = getBEYear(dayjs);
412
- const animalYear = getAnimalYear(dayjs);
413
- const eraYear = getJolakSakarajYear(dayjs) % 10;
413
+ const beYear = getBEYear(date);
414
+ const animalYear = getAnimalYear(date);
415
+ const eraYear = getJolakSakarajYear(date) % 10;
414
416
  return constant.kh.postformat(
415
417
  `ថ្ងៃ${constant.kh.weekdays[dayOfWeek]} ${moonDay.count}${
416
418
  constant.kh.moonStatus[moonDay.moonStatus]
@@ -421,10 +423,10 @@ export default function dayjskh(date?: dayjs.Dayjs | string) {
421
423
  } else if (typeof format === "string") {
422
424
  const formatRules = {
423
425
  W: () => {
424
- return constant.kh.weekdays[dayjs.day()];
426
+ return constant.kh.weekdays[date.day()];
425
427
  },
426
428
  w: () => {
427
- return constant.kh.weekdaysShort[dayjs.day()];
429
+ return constant.kh.weekdaysShort[date.day()];
428
430
  },
429
431
  d: () => {
430
432
  const moonDay = getKhmerLunarDayName(day);
@@ -451,22 +453,22 @@ export default function dayjskh(date?: dayjs.Dayjs | string) {
451
453
  return constant.kh.lunarMonths[month];
452
454
  },
453
455
  M: () => {
454
- return constant.kh.months[dayjs.month()];
456
+ return constant.kh.months[date.month()];
455
457
  },
456
458
  a: () => {
457
- return constant.kh.animalYear[getAnimalYear(dayjs)];
459
+ return constant.kh.animalYear[getAnimalYear(date)];
458
460
  },
459
461
  e: () => {
460
- return constant.kh.eraYear[getJolakSakarajYear(dayjs) % 10];
462
+ return constant.kh.eraYear[getJolakSakarajYear(date) % 10];
461
463
  },
462
464
  b: () => {
463
- return getBEYear(dayjs);
465
+ return getBEYear(date);
464
466
  },
465
467
  c: () => {
466
- return dayjs.year();
468
+ return date.year();
467
469
  },
468
470
  j: () => {
469
- return getJolakSakarajYear(dayjs);
471
+ return getJolakSakarajYear(date);
470
472
  },
471
473
  };
472
474
  return constant.kh.postformat(
package/types/index.d.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  // add type to support dayjs
2
- import type { Dayjs, PluginFunc } from "dayjs/esm/index";
2
+ import type { Dayjs, PluginFunc } from "dayjs/index";
3
3
 
4
4
  declare const plugin: PluginFunc;
5
5
  export = plugin;