@kalcodes/ethiopian-calendar 1.0.0 → 1.1.0

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/dist/index.d.ts CHANGED
@@ -1,5 +1,6 @@
1
- import { dateStr, type SimpleDate, isValidEthDate, isValidGregDate, isEthLeapYear, isGregLeapYear } from "./utils.js";
1
+ import { type SimpleDate } from "./utils.js";
2
2
  declare const ETH_JDN_OFFSET = 1724221;
3
+ declare const GREG_JDN_OFFSET = 1721426;
3
4
  declare function ethToJDN(date: SimpleDate): number;
4
5
  declare function jdnToEth(jdn: number): SimpleDate;
5
6
  declare function gregToJDN(date: SimpleDate): number;
@@ -10,5 +11,5 @@ declare function jdnToGreg(jdn: number): {
10
11
  };
11
12
  declare function gregToEth(date: SimpleDate): SimpleDate;
12
13
  declare function ethToGreg(date: SimpleDate): SimpleDate;
13
- export { type SimpleDate, ETH_JDN_OFFSET, dateStr, ethToJDN, jdnToEth, ethToGreg, gregToJDN, jdnToGreg, gregToEth, isValidEthDate, isValidGregDate, isEthLeapYear, isGregLeapYear, };
14
+ export { ETH_JDN_OFFSET, GREG_JDN_OFFSET, ethToJDN, jdnToEth, ethToGreg, gregToJDN, jdnToGreg, gregToEth, type SimpleDate, };
14
15
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,OAAO,EACP,KAAK,UAAU,EACf,cAAc,EACd,eAAe,EACf,aAAa,EACb,cAAc,EACf,MAAM,YAAY,CAAC;AAEpB,QAAA,MAAM,cAAc,UAAU,CAAC;AAE/B,iBAAS,QAAQ,CAAC,IAAI,EAAE,UAAU,GAAG,MAAM,CAU1C;AAED,iBAAS,QAAQ,CAAC,GAAG,EAAE,MAAM,GAAG,UAAU,CAmBzC;AAED,iBAAS,SAAS,CAAC,IAAI,EAAE,UAAU,GAAG,MAAM,CAmB3C;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ist/index.js CHANGED
@@ -1,9 +1,10 @@
1
- import { dateStr, isValidEthDate, isValidGregDate, isEthLeapYear, isGregLeapYear, } from "./utils.js";
2
- const ETH_JDN_OFFSET = 1724221;
1
+ import { toDateString, isValidEthDate, isValidGregDate, } from "./utils.js";
2
+ const ETH_JDN_OFFSET = 1_724_221;
3
+ const GREG_JDN_OFFSET = 1_721_426;
3
4
  function ethToJDN(date) {
4
5
  const { year, month, day } = date;
5
6
  if (!isValidEthDate(date))
6
- throw new Error(`The date ${dateStr(date)} is invalid!`);
7
+ throw new Error(`The date ${toDateString(date)} is invalid!`);
7
8
  const leapDays = Math.floor((year - 1) / 4);
8
9
  const daysInYear = 30 * (month - 1) + (day - 1);
9
10
  return ETH_JDN_OFFSET + 365 * (year - 1) + leapDays + daysInYear;
@@ -26,7 +27,7 @@ function jdnToEth(jdn) {
26
27
  function gregToJDN(date) {
27
28
  const { year, month, day } = date;
28
29
  if (!isValidGregDate(date))
29
- throw new Error(`The date ${dateStr(date)} is invalid!`);
30
+ throw new Error(`The date ${toDateString(date)} is invalid!`);
30
31
  const a = Math.floor((14 - month) / 12);
31
32
  const y = year + 4800 - a;
32
33
  const m = month + 12 * a - 3;
@@ -52,12 +53,12 @@ function jdnToGreg(jdn) {
52
53
  }
53
54
  function gregToEth(date) {
54
55
  if (!isValidGregDate(date))
55
- throw new Error(`The date ${dateStr(date)} is invalid!`);
56
+ throw new Error(`The date ${toDateString(date)} is invalid!`);
56
57
  return jdnToEth(gregToJDN(date));
57
58
  }
58
59
  function ethToGreg(date) {
59
60
  if (!isValidEthDate(date))
60
- throw new Error(`The date ${dateStr(date)} is invalid!`);
61
+ throw new Error(`The date ${toDateString(date)} is invalid!`);
61
62
  return jdnToGreg(ethToJDN(date));
62
63
  }
63
- export { ETH_JDN_OFFSET, dateStr, ethToJDN, jdnToEth, ethToGreg, gregToJDN, jdnToGreg, gregToEth, isValidEthDate, isValidGregDate, isEthLeapYear, isGregLeapYear, };
64
+ export { ETH_JDN_OFFSET, GREG_JDN_OFFSET, ethToJDN, jdnToEth, ethToGreg, gregToJDN, jdnToGreg, gregToEth, };
package/dist/utils.d.ts CHANGED
@@ -1,11 +1,14 @@
1
- export type SimpleDate = {
1
+ type Separator = "-" | "/";
2
+ type SimpleDate = {
2
3
  year: number;
3
4
  month: number;
4
5
  day: number;
5
6
  };
6
- export declare function isEthLeapYear(year: number): boolean;
7
- export declare function isGregLeapYear(year: number): boolean;
8
- export declare function isValidEthDate(date: SimpleDate): boolean;
9
- export declare function isValidGregDate(date: SimpleDate): boolean;
10
- export declare function dateStr(date: SimpleDate, separator?: string): string;
7
+ declare function isEthLeapYear(year: number): boolean;
8
+ declare function isGregLeapYear(year: number): boolean;
9
+ declare function isValidEthDate(date: SimpleDate): boolean;
10
+ declare function isValidGregDate(date: SimpleDate): boolean;
11
+ declare function toDateString(date: SimpleDate, separator?: Separator): string;
12
+ declare function toDateObject(dateString: string, separator?: Separator): SimpleDate;
13
+ export { type SimpleDate, type Separator, toDateObject, toDateString, isEthLeapYear, isGregLeapYear, isValidEthDate, isValidGregDate, };
11
14
  //# sourceMappingURL=utils.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"utils.d.ts","sourceRoot":"","sources":["../src/utils.ts"],"names":[],"mappings":"AAAA,MAAM,MAAM,UAAU,GAAG;IAAE,IAAI,EAAE,MAAM,CAAC;IAAC,KAAK,EAAE,MAAM,CAAC;IAAC,GAAG,EAAE,MAAM,CAAA;CAAE,CAAC;AAEtE,wBAAgB,aAAa,CAAC,IAAI,EAAE,MAAM,WAEzC;AAED,wBAAgB,cAAc,CAAC,IAAI,EAAE,MAAM,WAE1C;AAED,wBAAgB,cAAc,CAAC,IAAI,EAAE,UAAU,GAAG,OAAO,CAYxD;AAED,wBAAgB,eAAe,CAAC,IAAI,EAAE,UAAU,GAAG,OAAO,CAQzD;AAED,wBAAgB,OAAO,CAAC,IAAI,EAAE,UAAU,EAAE,SAAS,GAAE,MAAY,UAGhE"}
1
+ {"version":3,"file":"utils.d.ts","sourceRoot":"","sources":["../src/utils.ts"],"names":[],"mappings":"AAAA,KAAK,SAAS,GAAG,GAAG,GAAG,GAAG,CAAC;AAC3B,KAAK,UAAU,GAAG;IAAE,IAAI,EAAE,MAAM,CAAC;IAAC,KAAK,EAAE,MAAM,CAAC;IAAC,GAAG,EAAE,MAAM,CAAA;CAAE,CAAC;AAE/D,iBAAS,aAAa,CAAC,IAAI,EAAE,MAAM,WAElC;AAED,iBAAS,cAAc,CAAC,IAAI,EAAE,MAAM,WAEnC;AAED,iBAAS,cAAc,CAAC,IAAI,EAAE,UAAU,GAAG,OAAO,CAYjD;AAED,iBAAS,eAAe,CAAC,IAAI,EAAE,UAAU,GAAG,OAAO,CAQlD;AAED,iBAAS,YAAY,CAAC,IAAI,EAAE,UAAU,EAAE,SAAS,GAAE,SAAe,UAGjE;AAED,iBAAS,YAAY,CACnB,UAAU,EAAE,MAAM,EAClB,SAAS,GAAE,SAAe,GACzB,UAAU,CAcZ;AAED,OAAO,EACL,KAAK,UAAU,EACf,KAAK,SAAS,EACd,YAAY,EACZ,YAAY,EACZ,aAAa,EACb,cAAc,EACd,cAAc,EACd,eAAe,GAChB,CAAC"}
package/dist/utils.js CHANGED
@@ -1,10 +1,10 @@
1
- export function isEthLeapYear(year) {
1
+ function isEthLeapYear(year) {
2
2
  return year % 4 === 0;
3
3
  }
4
- export function isGregLeapYear(year) {
4
+ function isGregLeapYear(year) {
5
5
  return year % 4 === 0 && (year % 100 !== 0 || year % 400 === 0);
6
6
  }
7
- export function isValidEthDate(date) {
7
+ function isValidEthDate(date) {
8
8
  const { year, month, day } = date;
9
9
  if (day < 1 || day > 30)
10
10
  return false;
@@ -17,7 +17,7 @@ export function isValidEthDate(date) {
17
17
  }
18
18
  return true;
19
19
  }
20
- export function isValidGregDate(date) {
20
+ function isValidGregDate(date) {
21
21
  const { year, month, day } = date;
22
22
  if (month < 1 || month > 12)
23
23
  return false;
@@ -25,7 +25,18 @@ export function isValidGregDate(date) {
25
25
  const daysInMonth = [31, feb, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
26
26
  return day <= daysInMonth[month - 1];
27
27
  }
28
- export function dateStr(date, separator = "-") {
28
+ function toDateString(date, separator = "-") {
29
29
  const { year, month, day } = date;
30
30
  return `${year}${separator}${month}${separator}${day}`;
31
31
  }
32
+ function toDateObject(dateString, separator = "-") {
33
+ const pattern = new RegExp(`^\d+${separator}\d+${separator}\d+$`);
34
+ if (!dateString.match(pattern))
35
+ throw new Error(`${dateString} doesn't seem to be a valid date string! \n` +
36
+ `Date String should year-month-day or year/month/day.`);
37
+ const [year, month, day] = dateString
38
+ .split(separator)
39
+ .map((item) => Number(item));
40
+ return { year, month, day };
41
+ }
42
+ export { toDateObject, toDateString, isEthLeapYear, isGregLeapYear, isValidEthDate, isValidGregDate, };
package/package.json CHANGED
@@ -1,10 +1,14 @@
1
1
  {
2
2
  "name": "@kalcodes/ethiopian-calendar",
3
- "version": "1.0.0",
3
+ "version": "1.1.0",
4
4
  "description": "Simple ethiopian ⟵⟶ gregorian calendar converter.",
5
5
  "main": "dist/index.js",
6
6
  "type": "module",
7
- "author": "",
7
+ "author": "kalcodes",
8
+ "exports": {
9
+ ".": "./dist/index.js",
10
+ "./utils": "./dist/utils.js"
11
+ },
8
12
  "repository": {
9
13
  "type": "git",
10
14
  "url": "https://github.com/kalcodes/ethiopian-calendar.git"