dayjskh 0.1.0 → 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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2023 ChanthornAcademy
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md CHANGED
@@ -13,12 +13,12 @@ npm install dayjskh
13
13
 
14
14
  ```js
15
15
  import dayjs from "dayjs";
16
- import toKhDate from "dayjskh";
16
+ import toKhmerDate from "dayjskh";
17
17
 
18
- dayjs.extend(toKhDate);
18
+ dayjs.extend(toKhmerDate);
19
19
 
20
20
  const date = dayjs();
21
- console.log(date.toKhDate(format?)); // ថ្ងៃច័ន្ទ ១២កើត ខែស្រាពណ៍ ឆ្នាំថោះ បញ្ចស័ក ពុទ្ធសករាជ ២៥៦៧
21
+ console.log(date.toKhmerDate(format?)); // ថ្ងៃច័ន្ទ ១២កើត ខែស្រាពណ៍ ឆ្នាំថោះ បញ្ចស័ក ពុទ្ធសករាជ ២៥៦៧
22
22
  // find khmer new year dateTime
23
23
  console.log(date.khNewYear());
24
24
  // type
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;
@@ -24,39 +23,12 @@
24
23
  </div>
25
24
  <script type="module">
26
25
  import dayjs from "dayjs";
27
- import toKhDate from "./src/main.ts";
28
- dayjs.extend(toKhDate);
26
+ import toKhmerDate from "./src/main.ts";
27
+ dayjs.extend(toKhmerDate);
29
28
  const date = dayjs();
30
- console.log(date.khNewYear());
31
- document.querySelector("h1").innerHTML = date.toKhDate();
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
- .toKhDate("ថ្ងៃ 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
- .toKhDate("ថ្ងៃ 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.0",
3
+ "version": "0.1.2",
4
4
  "description": "Khmer Lunar date Plugin for Day.js",
5
5
  "main": "./src/main.ts",
6
6
  "scripts": {
@@ -23,4 +23,4 @@
23
23
  "dependencies": {
24
24
  "dayjs": "^1.11.9"
25
25
  }
26
- }
26
+ }
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
 
package/src/main.ts CHANGED
@@ -3,9 +3,9 @@ import type { PluginFunc } from "dayjs";
3
3
  import type plugin from "../types";
4
4
  import { constant } from "./constant";
5
5
 
6
- const toKhDate: PluginFunc<plugin.toKhDate> = (o, c, d) => {
6
+ const toKhmerDate: PluginFunc<plugin.toKhmerDate> = (o, c, d) => {
7
7
  const proto = c.prototype;
8
- proto.toKhDate = function (format?: string) {
8
+ proto.toKhmerDate = function (format?: string) {
9
9
  const date = constant.kh.preparse(this.format());
10
10
  return dayjskh(date).format(format);
11
11
  };
@@ -14,4 +14,4 @@ const toKhDate: PluginFunc<plugin.toKhDate> = (o, c, d) => {
14
14
  };
15
15
  };
16
16
 
17
- export default toKhDate;
17
+ export default toKhmerDate;
package/types/index.d.ts CHANGED
@@ -4,8 +4,8 @@ import type { Dayjs, PluginFunc } from "dayjs/esm/index";
4
4
  declare const plugin: PluginFunc;
5
5
  export = plugin;
6
6
  declare namespace plugin {
7
- export interface toKhDate {
8
- toKhDate(format?: string): string;
7
+ export interface toKhmerDate {
8
+ toKhmerDate(format?: string): string;
9
9
  khNewYear(): {
10
10
  date: dayjs.Dayjs;
11
11
  days: number;
@@ -15,5 +15,5 @@ declare namespace plugin {
15
15
  }
16
16
 
17
17
  declare module "dayjs" {
18
- interface Dayjs extends plugin.toKhDate {}
18
+ interface Dayjs extends plugin.toKhmerDate {}
19
19
  }