From 16243ace1e7e21ac2beb25c1b7c96a7a08ce5efa Mon Sep 17 00:00:00 2001 From: plerionaut-subesh <136443711+plerionaut-subesh@users.noreply.github.com> Date: Tue, 18 Jun 2024 18:28:24 +0545 Subject: [PATCH] refactor(date-config): make date-config human readable (#52) Updates the date config to me a map properly defining each year and months for human readability Co-authored-by: Subesh --- README.md | 4 + package.json | 2 +- src/date-config.ts | 1291 ++++++++++++++++++++++++++++ src/nepali-date-helper.ts | 138 +-- test/nepali-date-converter.test.ts | 27 +- 5 files changed, 1337 insertions(+), 125 deletions(-) create mode 100644 src/date-config.ts diff --git a/README.md b/README.md index f4bc3de..dfac0ac 100644 --- a/README.md +++ b/README.md @@ -321,6 +321,10 @@ $ git push ``` +## Fixing dates and adding future data + +The length of month can change for the future dates. Update the `date-config-ts` files with the number of days in respective months to fix the issue. Order the data in ascending order + ## Maintainer - [Subesh Bhandari](https://twitter.com/subesh1) diff --git a/package.json b/package.json index 7026d8a..3cf75bd 100644 --- a/package.json +++ b/package.json @@ -132,4 +132,4 @@ "typedoc": "^0.25.12", "typescript": "^4.7.4" } -} +} \ No newline at end of file diff --git a/src/date-config.ts b/src/date-config.ts new file mode 100644 index 0000000..f7f7c43 --- /dev/null +++ b/src/date-config.ts @@ -0,0 +1,1291 @@ +export const dateConfigMap: { + [year: string]: { + Baisakh: number + Jestha: number + Asar: number + Shrawan: number + Bhadra: number + Aswin: number + Kartik: number + Mangsir: number + Poush: number + Magh: number + Falgun: number + Chaitra: number + } +} = { + '2000': { + Baisakh: 30, + Jestha: 32, + Asar: 31, + Shrawan: 32, + Bhadra: 31, + Aswin: 30, + Kartik: 30, + Mangsir: 30, + Poush: 29, + Magh: 30, + Falgun: 29, + Chaitra: 31, + }, + '2001': { + Baisakh: 31, + Jestha: 31, + Asar: 32, + Shrawan: 31, + Bhadra: 31, + Aswin: 31, + Kartik: 30, + Mangsir: 29, + Poush: 30, + Magh: 29, + Falgun: 30, + Chaitra: 30, + }, + '2002': { + Baisakh: 31, + Jestha: 31, + Asar: 32, + Shrawan: 32, + Bhadra: 31, + Aswin: 30, + Kartik: 30, + Mangsir: 29, + Poush: 30, + Magh: 29, + Falgun: 30, + Chaitra: 30, + }, + '2003': { + Baisakh: 31, + Jestha: 32, + Asar: 31, + Shrawan: 32, + Bhadra: 31, + Aswin: 30, + Kartik: 30, + Mangsir: 30, + Poush: 29, + Magh: 29, + Falgun: 30, + Chaitra: 31, + }, + '2004': { + Baisakh: 30, + Jestha: 32, + Asar: 31, + Shrawan: 32, + Bhadra: 31, + Aswin: 30, + Kartik: 30, + Mangsir: 30, + Poush: 29, + Magh: 30, + Falgun: 29, + Chaitra: 31, + }, + '2005': { + Baisakh: 31, + Jestha: 31, + Asar: 32, + Shrawan: 31, + Bhadra: 31, + Aswin: 31, + Kartik: 30, + Mangsir: 29, + Poush: 30, + Magh: 29, + Falgun: 30, + Chaitra: 30, + }, + '2006': { + Baisakh: 31, + Jestha: 31, + Asar: 32, + Shrawan: 32, + Bhadra: 31, + Aswin: 30, + Kartik: 30, + Mangsir: 29, + Poush: 30, + Magh: 29, + Falgun: 30, + Chaitra: 30, + }, + '2007': { + Baisakh: 31, + Jestha: 32, + Asar: 31, + Shrawan: 32, + Bhadra: 31, + Aswin: 30, + Kartik: 30, + Mangsir: 30, + Poush: 29, + Magh: 29, + Falgun: 30, + Chaitra: 31, + }, + '2008': { + Baisakh: 31, + Jestha: 31, + Asar: 31, + Shrawan: 32, + Bhadra: 31, + Aswin: 31, + Kartik: 29, + Mangsir: 30, + Poush: 30, + Magh: 29, + Falgun: 29, + Chaitra: 31, + }, + '2009': { + Baisakh: 31, + Jestha: 31, + Asar: 32, + Shrawan: 31, + Bhadra: 31, + Aswin: 31, + Kartik: 30, + Mangsir: 29, + Poush: 30, + Magh: 29, + Falgun: 30, + Chaitra: 30, + }, + '2010': { + Baisakh: 31, + Jestha: 31, + Asar: 32, + Shrawan: 32, + Bhadra: 31, + Aswin: 30, + Kartik: 30, + Mangsir: 29, + Poush: 30, + Magh: 29, + Falgun: 30, + Chaitra: 30, + }, + '2011': { + Baisakh: 31, + Jestha: 32, + Asar: 31, + Shrawan: 32, + Bhadra: 31, + Aswin: 30, + Kartik: 30, + Mangsir: 30, + Poush: 29, + Magh: 29, + Falgun: 30, + Chaitra: 31, + }, + '2012': { + Baisakh: 31, + Jestha: 31, + Asar: 31, + Shrawan: 32, + Bhadra: 31, + Aswin: 31, + Kartik: 29, + Mangsir: 30, + Poush: 30, + Magh: 29, + Falgun: 30, + Chaitra: 30, + }, + '2013': { + Baisakh: 31, + Jestha: 31, + Asar: 32, + Shrawan: 31, + Bhadra: 31, + Aswin: 31, + Kartik: 30, + Mangsir: 29, + Poush: 30, + Magh: 29, + Falgun: 30, + Chaitra: 30, + }, + '2014': { + Baisakh: 31, + Jestha: 31, + Asar: 32, + Shrawan: 32, + Bhadra: 31, + Aswin: 30, + Kartik: 30, + Mangsir: 29, + Poush: 30, + Magh: 29, + Falgun: 30, + Chaitra: 30, + }, + '2015': { + Baisakh: 31, + Jestha: 32, + Asar: 31, + Shrawan: 32, + Bhadra: 31, + Aswin: 30, + Kartik: 30, + Mangsir: 30, + Poush: 29, + Magh: 29, + Falgun: 30, + Chaitra: 31, + }, + '2016': { + Baisakh: 31, + Jestha: 31, + Asar: 31, + Shrawan: 32, + Bhadra: 31, + Aswin: 31, + Kartik: 29, + Mangsir: 30, + Poush: 30, + Magh: 29, + Falgun: 30, + Chaitra: 30, + }, + '2017': { + Baisakh: 31, + Jestha: 31, + Asar: 32, + Shrawan: 31, + Bhadra: 31, + Aswin: 31, + Kartik: 30, + Mangsir: 29, + Poush: 30, + Magh: 29, + Falgun: 30, + Chaitra: 30, + }, + '2018': { + Baisakh: 31, + Jestha: 32, + Asar: 31, + Shrawan: 32, + Bhadra: 31, + Aswin: 30, + Kartik: 30, + Mangsir: 29, + Poush: 30, + Magh: 29, + Falgun: 30, + Chaitra: 30, + }, + '2019': { + Baisakh: 31, + Jestha: 32, + Asar: 31, + Shrawan: 32, + Bhadra: 31, + Aswin: 30, + Kartik: 30, + Mangsir: 30, + Poush: 29, + Magh: 30, + Falgun: 29, + Chaitra: 31, + }, + '2020': { + Baisakh: 31, + Jestha: 31, + Asar: 31, + Shrawan: 32, + Bhadra: 31, + Aswin: 31, + Kartik: 30, + Mangsir: 29, + Poush: 30, + Magh: 29, + Falgun: 30, + Chaitra: 30, + }, + '2021': { + Baisakh: 31, + Jestha: 31, + Asar: 32, + Shrawan: 31, + Bhadra: 31, + Aswin: 31, + Kartik: 30, + Mangsir: 29, + Poush: 30, + Magh: 29, + Falgun: 30, + Chaitra: 30, + }, + '2022': { + Baisakh: 31, + Jestha: 32, + Asar: 31, + Shrawan: 32, + Bhadra: 31, + Aswin: 30, + Kartik: 30, + Mangsir: 30, + Poush: 29, + Magh: 29, + Falgun: 30, + Chaitra: 30, + }, + '2023': { + Baisakh: 31, + Jestha: 32, + Asar: 31, + Shrawan: 32, + Bhadra: 31, + Aswin: 30, + Kartik: 30, + Mangsir: 30, + Poush: 29, + Magh: 30, + Falgun: 29, + Chaitra: 31, + }, + '2024': { + Baisakh: 31, + Jestha: 31, + Asar: 31, + Shrawan: 32, + Bhadra: 31, + Aswin: 31, + Kartik: 30, + Mangsir: 29, + Poush: 30, + Magh: 29, + Falgun: 30, + Chaitra: 30, + }, + '2025': { + Baisakh: 31, + Jestha: 31, + Asar: 32, + Shrawan: 31, + Bhadra: 31, + Aswin: 31, + Kartik: 30, + Mangsir: 29, + Poush: 30, + Magh: 29, + Falgun: 30, + Chaitra: 30, + }, + '2026': { + Baisakh: 31, + Jestha: 32, + Asar: 31, + Shrawan: 32, + Bhadra: 31, + Aswin: 30, + Kartik: 30, + Mangsir: 30, + Poush: 29, + Magh: 29, + Falgun: 30, + Chaitra: 31, + }, + '2027': { + Baisakh: 30, + Jestha: 32, + Asar: 31, + Shrawan: 32, + Bhadra: 31, + Aswin: 30, + Kartik: 30, + Mangsir: 30, + Poush: 29, + Magh: 30, + Falgun: 29, + Chaitra: 31, + }, + '2028': { + Baisakh: 31, + Jestha: 31, + Asar: 32, + Shrawan: 31, + Bhadra: 31, + Aswin: 31, + Kartik: 30, + Mangsir: 29, + Poush: 30, + Magh: 29, + Falgun: 30, + Chaitra: 30, + }, + '2029': { + Baisakh: 31, + Jestha: 31, + Asar: 32, + Shrawan: 31, + Bhadra: 32, + Aswin: 30, + Kartik: 30, + Mangsir: 29, + Poush: 30, + Magh: 29, + Falgun: 30, + Chaitra: 30, + }, + '2030': { + Baisakh: 31, + Jestha: 32, + Asar: 31, + Shrawan: 32, + Bhadra: 31, + Aswin: 30, + Kartik: 30, + Mangsir: 30, + Poush: 29, + Magh: 29, + Falgun: 30, + Chaitra: 31, + }, + '2031': { + Baisakh: 30, + Jestha: 32, + Asar: 31, + Shrawan: 32, + Bhadra: 31, + Aswin: 30, + Kartik: 30, + Mangsir: 30, + Poush: 29, + Magh: 30, + Falgun: 29, + Chaitra: 31, + }, + '2032': { + Baisakh: 31, + Jestha: 31, + Asar: 32, + Shrawan: 31, + Bhadra: 31, + Aswin: 31, + Kartik: 30, + Mangsir: 29, + Poush: 30, + Magh: 29, + Falgun: 30, + Chaitra: 30, + }, + '2033': { + Baisakh: 31, + Jestha: 31, + Asar: 32, + Shrawan: 32, + Bhadra: 31, + Aswin: 30, + Kartik: 30, + Mangsir: 29, + Poush: 30, + Magh: 29, + Falgun: 30, + Chaitra: 30, + }, + '2034': { + Baisakh: 31, + Jestha: 32, + Asar: 31, + Shrawan: 32, + Bhadra: 31, + Aswin: 30, + Kartik: 30, + Mangsir: 30, + Poush: 29, + Magh: 29, + Falgun: 30, + Chaitra: 31, + }, + '2035': { + Baisakh: 30, + Jestha: 32, + Asar: 31, + Shrawan: 32, + Bhadra: 31, + Aswin: 31, + Kartik: 29, + Mangsir: 30, + Poush: 30, + Magh: 29, + Falgun: 29, + Chaitra: 31, + }, + '2036': { + Baisakh: 31, + Jestha: 31, + Asar: 32, + Shrawan: 31, + Bhadra: 31, + Aswin: 31, + Kartik: 30, + Mangsir: 29, + Poush: 30, + Magh: 29, + Falgun: 30, + Chaitra: 30, + }, + '2037': { + Baisakh: 31, + Jestha: 31, + Asar: 32, + Shrawan: 32, + Bhadra: 31, + Aswin: 30, + Kartik: 30, + Mangsir: 29, + Poush: 30, + Magh: 29, + Falgun: 30, + Chaitra: 30, + }, + '2038': { + Baisakh: 31, + Jestha: 32, + Asar: 31, + Shrawan: 32, + Bhadra: 31, + Aswin: 30, + Kartik: 30, + Mangsir: 30, + Poush: 29, + Magh: 29, + Falgun: 30, + Chaitra: 31, + }, + '2039': { + Baisakh: 31, + Jestha: 31, + Asar: 31, + Shrawan: 32, + Bhadra: 31, + Aswin: 31, + Kartik: 29, + Mangsir: 30, + Poush: 30, + Magh: 29, + Falgun: 30, + Chaitra: 30, + }, + '2040': { + Baisakh: 31, + Jestha: 31, + Asar: 32, + Shrawan: 31, + Bhadra: 31, + Aswin: 31, + Kartik: 30, + Mangsir: 29, + Poush: 30, + Magh: 29, + Falgun: 30, + Chaitra: 30, + }, + '2041': { + Baisakh: 31, + Jestha: 31, + Asar: 32, + Shrawan: 32, + Bhadra: 31, + Aswin: 30, + Kartik: 30, + Mangsir: 29, + Poush: 30, + Magh: 29, + Falgun: 30, + Chaitra: 30, + }, + '2042': { + Baisakh: 31, + Jestha: 32, + Asar: 31, + Shrawan: 32, + Bhadra: 31, + Aswin: 30, + Kartik: 30, + Mangsir: 30, + Poush: 29, + Magh: 29, + Falgun: 30, + Chaitra: 31, + }, + '2043': { + Baisakh: 31, + Jestha: 31, + Asar: 31, + Shrawan: 32, + Bhadra: 31, + Aswin: 31, + Kartik: 29, + Mangsir: 30, + Poush: 30, + Magh: 29, + Falgun: 30, + Chaitra: 30, + }, + '2044': { + Baisakh: 31, + Jestha: 31, + Asar: 32, + Shrawan: 31, + Bhadra: 31, + Aswin: 31, + Kartik: 30, + Mangsir: 29, + Poush: 30, + Magh: 29, + Falgun: 30, + Chaitra: 30, + }, + '2045': { + Baisakh: 31, + Jestha: 32, + Asar: 31, + Shrawan: 32, + Bhadra: 31, + Aswin: 30, + Kartik: 30, + Mangsir: 29, + Poush: 30, + Magh: 29, + Falgun: 30, + Chaitra: 30, + }, + '2046': { + Baisakh: 31, + Jestha: 32, + Asar: 31, + Shrawan: 32, + Bhadra: 31, + Aswin: 30, + Kartik: 30, + Mangsir: 30, + Poush: 29, + Magh: 29, + Falgun: 30, + Chaitra: 31, + }, + '2047': { + Baisakh: 31, + Jestha: 31, + Asar: 31, + Shrawan: 32, + Bhadra: 31, + Aswin: 31, + Kartik: 30, + Mangsir: 29, + Poush: 30, + Magh: 29, + Falgun: 30, + Chaitra: 30, + }, + '2048': { + Baisakh: 31, + Jestha: 31, + Asar: 32, + Shrawan: 31, + Bhadra: 31, + Aswin: 31, + Kartik: 30, + Mangsir: 29, + Poush: 30, + Magh: 29, + Falgun: 30, + Chaitra: 30, + }, + '2049': { + Baisakh: 31, + Jestha: 32, + Asar: 31, + Shrawan: 32, + Bhadra: 31, + Aswin: 30, + Kartik: 30, + Mangsir: 30, + Poush: 29, + Magh: 29, + Falgun: 30, + Chaitra: 30, + }, + '2050': { + Baisakh: 31, + Jestha: 32, + Asar: 31, + Shrawan: 32, + Bhadra: 31, + Aswin: 30, + Kartik: 30, + Mangsir: 30, + Poush: 29, + Magh: 30, + Falgun: 29, + Chaitra: 31, + }, + '2051': { + Baisakh: 31, + Jestha: 31, + Asar: 31, + Shrawan: 32, + Bhadra: 31, + Aswin: 31, + Kartik: 30, + Mangsir: 29, + Poush: 30, + Magh: 29, + Falgun: 30, + Chaitra: 30, + }, + '2052': { + Baisakh: 31, + Jestha: 31, + Asar: 32, + Shrawan: 31, + Bhadra: 31, + Aswin: 31, + Kartik: 30, + Mangsir: 29, + Poush: 30, + Magh: 29, + Falgun: 30, + Chaitra: 30, + }, + '2053': { + Baisakh: 31, + Jestha: 32, + Asar: 31, + Shrawan: 32, + Bhadra: 31, + Aswin: 30, + Kartik: 30, + Mangsir: 30, + Poush: 29, + Magh: 29, + Falgun: 30, + Chaitra: 30, + }, + '2054': { + Baisakh: 31, + Jestha: 32, + Asar: 31, + Shrawan: 32, + Bhadra: 31, + Aswin: 30, + Kartik: 30, + Mangsir: 30, + Poush: 29, + Magh: 30, + Falgun: 29, + Chaitra: 31, + }, + '2055': { + Baisakh: 31, + Jestha: 31, + Asar: 32, + Shrawan: 31, + Bhadra: 31, + Aswin: 31, + Kartik: 30, + Mangsir: 29, + Poush: 30, + Magh: 29, + Falgun: 30, + Chaitra: 30, + }, + '2056': { + Baisakh: 31, + Jestha: 31, + Asar: 32, + Shrawan: 31, + Bhadra: 32, + Aswin: 30, + Kartik: 30, + Mangsir: 29, + Poush: 30, + Magh: 29, + Falgun: 30, + Chaitra: 30, + }, + '2057': { + Baisakh: 31, + Jestha: 32, + Asar: 31, + Shrawan: 32, + Bhadra: 31, + Aswin: 30, + Kartik: 30, + Mangsir: 30, + Poush: 29, + Magh: 29, + Falgun: 30, + Chaitra: 31, + }, + '2058': { + Baisakh: 30, + Jestha: 32, + Asar: 31, + Shrawan: 32, + Bhadra: 31, + Aswin: 30, + Kartik: 30, + Mangsir: 30, + Poush: 29, + Magh: 30, + Falgun: 29, + Chaitra: 31, + }, + '2059': { + Baisakh: 31, + Jestha: 31, + Asar: 32, + Shrawan: 31, + Bhadra: 31, + Aswin: 31, + Kartik: 30, + Mangsir: 29, + Poush: 30, + Magh: 29, + Falgun: 30, + Chaitra: 30, + }, + '2060': { + Baisakh: 31, + Jestha: 31, + Asar: 32, + Shrawan: 32, + Bhadra: 31, + Aswin: 30, + Kartik: 30, + Mangsir: 29, + Poush: 30, + Magh: 29, + Falgun: 30, + Chaitra: 30, + }, + '2061': { + Baisakh: 31, + Jestha: 32, + Asar: 31, + Shrawan: 32, + Bhadra: 31, + Aswin: 30, + Kartik: 30, + Mangsir: 30, + Poush: 29, + Magh: 29, + Falgun: 30, + Chaitra: 31, + }, + '2062': { + Baisakh: 30, + Jestha: 32, + Asar: 31, + Shrawan: 32, + Bhadra: 31, + Aswin: 31, + Kartik: 29, + Mangsir: 30, + Poush: 29, + Magh: 30, + Falgun: 29, + Chaitra: 31, + }, + '2063': { + Baisakh: 31, + Jestha: 31, + Asar: 32, + Shrawan: 31, + Bhadra: 31, + Aswin: 31, + Kartik: 30, + Mangsir: 29, + Poush: 30, + Magh: 29, + Falgun: 30, + Chaitra: 30, + }, + '2064': { + Baisakh: 31, + Jestha: 31, + Asar: 32, + Shrawan: 32, + Bhadra: 31, + Aswin: 30, + Kartik: 30, + Mangsir: 29, + Poush: 30, + Magh: 29, + Falgun: 30, + Chaitra: 30, + }, + '2065': { + Baisakh: 31, + Jestha: 32, + Asar: 31, + Shrawan: 32, + Bhadra: 31, + Aswin: 30, + Kartik: 30, + Mangsir: 30, + Poush: 29, + Magh: 29, + Falgun: 30, + Chaitra: 31, + }, + '2066': { + Baisakh: 31, + Jestha: 31, + Asar: 31, + Shrawan: 32, + Bhadra: 31, + Aswin: 31, + Kartik: 29, + Mangsir: 30, + Poush: 30, + Magh: 29, + Falgun: 29, + Chaitra: 31, + }, + '2067': { + Baisakh: 31, + Jestha: 31, + Asar: 32, + Shrawan: 31, + Bhadra: 31, + Aswin: 31, + Kartik: 30, + Mangsir: 29, + Poush: 30, + Magh: 29, + Falgun: 30, + Chaitra: 30, + }, + '2068': { + Baisakh: 31, + Jestha: 31, + Asar: 32, + Shrawan: 32, + Bhadra: 31, + Aswin: 30, + Kartik: 30, + Mangsir: 29, + Poush: 30, + Magh: 29, + Falgun: 30, + Chaitra: 30, + }, + '2069': { + Baisakh: 31, + Jestha: 32, + Asar: 31, + Shrawan: 32, + Bhadra: 31, + Aswin: 30, + Kartik: 30, + Mangsir: 30, + Poush: 29, + Magh: 29, + Falgun: 30, + Chaitra: 31, + }, + '2070': { + Baisakh: 31, + Jestha: 31, + Asar: 31, + Shrawan: 32, + Bhadra: 31, + Aswin: 31, + Kartik: 29, + Mangsir: 30, + Poush: 30, + Magh: 29, + Falgun: 30, + Chaitra: 30, + }, + '2071': { + Baisakh: 31, + Jestha: 31, + Asar: 32, + Shrawan: 31, + Bhadra: 31, + Aswin: 31, + Kartik: 30, + Mangsir: 29, + Poush: 30, + Magh: 29, + Falgun: 30, + Chaitra: 30, + }, + '2072': { + Baisakh: 31, + Jestha: 32, + Asar: 31, + Shrawan: 32, + Bhadra: 31, + Aswin: 30, + Kartik: 30, + Mangsir: 29, + Poush: 30, + Magh: 29, + Falgun: 30, + Chaitra: 30, + }, + '2073': { + Baisakh: 31, + Jestha: 32, + Asar: 31, + Shrawan: 32, + Bhadra: 31, + Aswin: 30, + Kartik: 30, + Mangsir: 30, + Poush: 29, + Magh: 29, + Falgun: 30, + Chaitra: 31, + }, + '2074': { + Baisakh: 31, + Jestha: 31, + Asar: 31, + Shrawan: 32, + Bhadra: 31, + Aswin: 31, + Kartik: 30, + Mangsir: 29, + Poush: 30, + Magh: 29, + Falgun: 30, + Chaitra: 30, + }, + '2075': { + Baisakh: 31, + Jestha: 31, + Asar: 32, + Shrawan: 31, + Bhadra: 31, + Aswin: 31, + Kartik: 30, + Mangsir: 29, + Poush: 30, + Magh: 29, + Falgun: 30, + Chaitra: 30, + }, + '2076': { + Baisakh: 31, + Jestha: 32, + Asar: 31, + Shrawan: 32, + Bhadra: 31, + Aswin: 30, + Kartik: 30, + Mangsir: 30, + Poush: 29, + Magh: 29, + Falgun: 30, + Chaitra: 30, + }, + '2077': { + Baisakh: 31, + Jestha: 32, + Asar: 31, + Shrawan: 32, + Bhadra: 31, + Aswin: 30, + Kartik: 30, + Mangsir: 30, + Poush: 29, + Magh: 30, + Falgun: 29, + Chaitra: 31, + }, + '2078': { + Baisakh: 31, + Jestha: 31, + Asar: 31, + Shrawan: 32, + Bhadra: 31, + Aswin: 31, + Kartik: 30, + Mangsir: 29, + Poush: 30, + Magh: 29, + Falgun: 30, + Chaitra: 30, + }, + '2079': { + Baisakh: 31, + Jestha: 31, + Asar: 32, + Shrawan: 31, + Bhadra: 31, + Aswin: 31, + Kartik: 30, + Mangsir: 29, + Poush: 30, + Magh: 29, + Falgun: 30, + Chaitra: 30, + }, + '2080': { + Baisakh: 31, + Jestha: 32, + Asar: 31, + Shrawan: 32, + Bhadra: 31, + Aswin: 30, + Kartik: 30, + Mangsir: 30, + Poush: 29, + Magh: 29, + Falgun: 30, + Chaitra: 30, + }, + '2081': { + Baisakh: 31, + Jestha: 32, + Asar: 31, + Shrawan: 32, + Bhadra: 31, + Aswin: 30, + Kartik: 30, + Mangsir: 30, + Poush: 29, + Magh: 30, + Falgun: 29, + Chaitra: 31, + }, + '2082': { + Baisakh: 30, + Jestha: 32, + Asar: 31, + Shrawan: 32, + Bhadra: 31, + Aswin: 30, + Kartik: 30, + Mangsir: 30, + Poush: 29, + Magh: 30, + Falgun: 30, + Chaitra: 30, + }, + '2083': { + Baisakh: 31, + Jestha: 31, + Asar: 32, + Shrawan: 31, + Bhadra: 31, + Aswin: 30, + Kartik: 30, + Mangsir: 30, + Poush: 29, + Magh: 30, + Falgun: 30, + Chaitra: 30, + }, + '2084': { + Baisakh: 31, + Jestha: 31, + Asar: 32, + Shrawan: 31, + Bhadra: 31, + Aswin: 30, + Kartik: 30, + Mangsir: 30, + Poush: 29, + Magh: 30, + Falgun: 30, + Chaitra: 30, + }, + '2085': { + Baisakh: 31, + Jestha: 32, + Asar: 31, + Shrawan: 32, + Bhadra: 30, + Aswin: 31, + Kartik: 30, + Mangsir: 30, + Poush: 29, + Magh: 30, + Falgun: 30, + Chaitra: 30, + }, + '2086': { + Baisakh: 30, + Jestha: 32, + Asar: 31, + Shrawan: 32, + Bhadra: 31, + Aswin: 30, + Kartik: 30, + Mangsir: 30, + Poush: 29, + Magh: 30, + Falgun: 30, + Chaitra: 30, + }, + '2087': { + Baisakh: 31, + Jestha: 31, + Asar: 32, + Shrawan: 31, + Bhadra: 31, + Aswin: 31, + Kartik: 30, + Mangsir: 30, + Poush: 29, + Magh: 30, + Falgun: 30, + Chaitra: 30, + }, + '2088': { + Baisakh: 30, + Jestha: 31, + Asar: 32, + Shrawan: 32, + Bhadra: 30, + Aswin: 31, + Kartik: 30, + Mangsir: 30, + Poush: 29, + Magh: 30, + Falgun: 30, + Chaitra: 30, + }, + '2089': { + Baisakh: 30, + Jestha: 32, + Asar: 31, + Shrawan: 32, + Bhadra: 31, + Aswin: 30, + Kartik: 30, + Mangsir: 30, + Poush: 29, + Magh: 30, + Falgun: 30, + Chaitra: 30, + }, + '2090': { + Baisakh: 30, + Jestha: 32, + Asar: 31, + Shrawan: 32, + Bhadra: 31, + Aswin: 30, + Kartik: 30, + Mangsir: 30, + Poush: 29, + Magh: 30, + Falgun: 30, + Chaitra: 30, + }, +} diff --git a/src/nepali-date-helper.ts b/src/nepali-date-helper.ts index f3e8600..89a6e25 100644 --- a/src/nepali-date-helper.ts +++ b/src/nepali-date-helper.ts @@ -1,6 +1,8 @@ +import { dateConfigMap } from './date-config' + export enum Language { np = 'np', - en = 'en' + en = 'en', } export interface IYearMonthDate { year: number @@ -17,99 +19,9 @@ export interface IAdBs { /** * The constant storing nepali date month days mappings for each year starting from 2000 BS */ -const yearMonthDaysMapping: number[][] = [ - [30, 32, 31, 32, 31, 30, 30, 30, 29, 30, 29, 31], - [31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30], - [31, 31, 32, 32, 31, 30, 30, 29, 30, 29, 30, 30], - [31, 32, 31, 32, 31, 30, 30, 30, 29, 29, 30, 31], - [30, 32, 31, 32, 31, 30, 30, 30, 29, 30, 29, 31], - [31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30], - [31, 31, 32, 32, 31, 30, 30, 29, 30, 29, 30, 30], - [31, 32, 31, 32, 31, 30, 30, 30, 29, 29, 30, 31], - [31, 31, 31, 32, 31, 31, 29, 30, 30, 29, 29, 31], - [31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30], - [31, 31, 32, 32, 31, 30, 30, 29, 30, 29, 30, 30], - [31, 32, 31, 32, 31, 30, 30, 30, 29, 29, 30, 31], - [31, 31, 31, 32, 31, 31, 29, 30, 30, 29, 30, 30], - [31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30], - [31, 31, 32, 32, 31, 30, 30, 29, 30, 29, 30, 30], - [31, 32, 31, 32, 31, 30, 30, 30, 29, 29, 30, 31], - [31, 31, 31, 32, 31, 31, 29, 30, 30, 29, 30, 30], - [31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30], - [31, 32, 31, 32, 31, 30, 30, 29, 30, 29, 30, 30], - [31, 32, 31, 32, 31, 30, 30, 30, 29, 30, 29, 31], - [31, 31, 31, 32, 31, 31, 30, 29, 30, 29, 30, 30], - [31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30], - [31, 32, 31, 32, 31, 30, 30, 30, 29, 29, 30, 30], - [31, 32, 31, 32, 31, 30, 30, 30, 29, 30, 29, 31], - [31, 31, 31, 32, 31, 31, 30, 29, 30, 29, 30, 30], - [31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30], - [31, 32, 31, 32, 31, 30, 30, 30, 29, 29, 30, 31], - [30, 32, 31, 32, 31, 30, 30, 30, 29, 30, 29, 31], - [31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30], - [31, 31, 32, 31, 32, 30, 30, 29, 30, 29, 30, 30], - [31, 32, 31, 32, 31, 30, 30, 30, 29, 29, 30, 31], - [30, 32, 31, 32, 31, 30, 30, 30, 29, 30, 29, 31], - [31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30], - [31, 31, 32, 32, 31, 30, 30, 29, 30, 29, 30, 30], - [31, 32, 31, 32, 31, 30, 30, 30, 29, 29, 30, 31], - [30, 32, 31, 32, 31, 31, 29, 30, 30, 29, 29, 31], - [31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30], - [31, 31, 32, 32, 31, 30, 30, 29, 30, 29, 30, 30], - [31, 32, 31, 32, 31, 30, 30, 30, 29, 29, 30, 31], - [31, 31, 31, 32, 31, 31, 29, 30, 30, 29, 30, 30], - [31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30], - [31, 31, 32, 32, 31, 30, 30, 29, 30, 29, 30, 30], - [31, 32, 31, 32, 31, 30, 30, 30, 29, 29, 30, 31], - [31, 31, 31, 32, 31, 31, 29, 30, 30, 29, 30, 30], - [31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30], - [31, 32, 31, 32, 31, 30, 30, 29, 30, 29, 30, 30], - [31, 32, 31, 32, 31, 30, 30, 30, 29, 29, 30, 31], - [31, 31, 31, 32, 31, 31, 30, 29, 30, 29, 30, 30], - [31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30], - [31, 32, 31, 32, 31, 30, 30, 30, 29, 29, 30, 30], - [31, 32, 31, 32, 31, 30, 30, 30, 29, 30, 29, 31], - [31, 31, 31, 32, 31, 31, 30, 29, 30, 29, 30, 30], - [31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30], - [31, 32, 31, 32, 31, 30, 30, 30, 29, 29, 30, 30], - [31, 32, 31, 32, 31, 30, 30, 30, 29, 30, 29, 31], - [31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30], - [31, 31, 32, 31, 32, 30, 30, 29, 30, 29, 30, 30], - [31, 32, 31, 32, 31, 30, 30, 30, 29, 29, 30, 31], - [30, 32, 31, 32, 31, 30, 30, 30, 29, 30, 29, 31], - [31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30], - [31, 31, 32, 32, 31, 30, 30, 29, 30, 29, 30, 30], - [31, 32, 31, 32, 31, 30, 30, 30, 29, 29, 30, 31], - [30, 32, 31, 32, 31, 31, 29, 30, 29, 30, 29, 31], - [31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30], - [31, 31, 32, 32, 31, 30, 30, 29, 30, 29, 30, 30], - [31, 32, 31, 32, 31, 30, 30, 30, 29, 29, 30, 31], - [31, 31, 31, 32, 31, 31, 29, 30, 30, 29, 29, 31], - [31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30], - [31, 31, 32, 32, 31, 30, 30, 29, 30, 29, 30, 30], - [31, 32, 31, 32, 31, 30, 30, 30, 29, 29, 30, 31], - [31, 31, 31, 32, 31, 31, 29, 30, 30, 29, 30, 30], - [31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30], - [31, 32, 31, 32, 31, 30, 30, 29, 30, 29, 30, 30], - [31, 32, 31, 32, 31, 30, 30, 30, 29, 29, 30, 31], - [31, 31, 31, 32, 31, 31, 30, 29, 30, 29, 30, 30], - [31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30], - [31, 32, 31, 32, 31, 30, 30, 30, 29, 29, 30, 30], - [31, 32, 31, 32, 31, 30, 30, 30, 29, 30, 29, 31], - [31, 31, 31, 32, 31, 31, 30, 29, 30, 29, 30, 30], - [31, 31, 32, 31, 31, 31, 30, 29, 30, 29, 30, 30], - [31, 32, 31, 32, 31, 30, 30, 30, 29, 29, 30, 30], - [31, 32, 31, 32, 31, 30, 30, 30, 29, 30, 29, 31], // 2081 BS - [30, 32, 31, 32, 31, 30, 30, 30, 29, 30, 30, 30], - [31, 31, 32, 31, 31, 30, 30, 30, 29, 30, 30, 30], - [31, 31, 32, 31, 31, 30, 30, 30, 29, 30, 30, 30], - [31, 32, 31, 32, 30, 31, 30, 30, 29, 30, 30, 30], - [30, 32, 31, 32, 31, 30, 30, 30, 29, 30, 30, 30], - [31, 31, 32, 31, 31, 31, 30, 30, 29, 30, 30, 30], - [30, 31, 32, 32, 30, 31, 30, 30, 29, 30, 30, 30], - [30, 32, 31, 32, 31, 30, 30, 30, 29, 30, 30, 30], - [30, 32, 31, 32, 31, 30, 30, 30, 29, 30, 30, 30] -] +const yearMonthDaysMapping: number[][] = Object.values(dateConfigMap).map((year) => + Object.values(year) +) /** * Memoizing the days passed for each month in year for faster calculation @@ -197,7 +109,7 @@ export const formatObj = { en: { day: { short: ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'], - long: ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'] + long: ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'], }, month: { short: ['Bai', 'Jes', 'Asa', 'Shr', 'Bhd', 'Asw', 'Kar', 'Man', 'Pou', 'Mag', 'Fal', 'Cha'], @@ -213,15 +125,15 @@ export const formatObj = { 'Poush', 'Magh', 'Falgun', - 'Chaitra' - ] + 'Chaitra', + ], }, - date: ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9'] + date: ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9'], }, np: { day: { short: ['आइत', 'सोम', 'मंगल', 'बुध', 'बिहि', 'शुक्र', 'शनि'], - long: ['आइतबार', 'सोमबार', 'मंगलबार', 'बुधबार', 'बिहिबार', 'शुक्रबार', 'शनिबार'] + long: ['आइतबार', 'सोमबार', 'मंगलबार', 'बुधबार', 'बिहिबार', 'शुक्रबार', 'शनिबार'], }, month: { short: ['बै', 'जे', 'अ', 'श्रा', 'भा', 'आ', 'का', 'मं', 'पौ', 'मा', 'फा', 'चै'], @@ -237,11 +149,11 @@ export const formatObj = { 'पौष', 'माघ', 'फाल्गुण', - 'चैत्र' - ] + 'चैत्र', + ], }, - date: ['०', '१', '२', '३', '४', '५', '६', '७', '८', '९'] - } + date: ['०', '१', '२', '३', '४', '५', '६', '७', '८', '९'], + }, } /** @@ -251,7 +163,7 @@ const beginEnglish = { year: 1943, month: 3, date: 13, - day: 3 + day: 3, } /** @@ -299,12 +211,12 @@ export function mapDaysToDate(daysPassed: number): IYearMonthDate { } const yearIndex = yearDaysMapping.findIndex( - year => + (year) => daysPassed > year[COMPLETED_DAYS] && daysPassed <= year[COMPLETED_DAYS] + year[TOTAL_DAYS] ) const monthRemainder = daysPassed - yearDaysMapping[yearIndex][COMPLETED_DAYS] const monthIndex = monthDaysMappings[yearIndex].findIndex( - month => + (month) => monthRemainder > month[COMPLETED_DAYS] && monthRemainder <= month[COMPLETED_DAYS] + month[TOTAL_DAYS] ) @@ -313,7 +225,7 @@ export function mapDaysToDate(daysPassed: number): IYearMonthDate { return { year: getYearFromIndex(yearIndex), month: monthIndex, - date: date + date: date, } } @@ -331,7 +243,7 @@ export function mapDaysToDateAD(daysPassed: number) { year: mappedDate.getUTCFullYear(), month: mappedDate.getUTCMonth(), date: mappedDate.getUTCDate(), - day: mappedDate.getUTCDay() + day: mappedDate.getUTCDay(), } } @@ -343,7 +255,7 @@ export function convertToAD(bsDateObject: IYearMonthDate): IAdBs { return { AD, - BS: { ...BS, day: AD.day } + BS: { ...BS, day: AD.day }, } } catch { throw new Error("The date doesn't fall within 2000/01/01 - 2090/12/30") @@ -362,7 +274,7 @@ export function convertToBS(adDateObject: Date): IAdBs { return { AD, - BS: { ...BS, day: AD.day } + BS: { ...BS, day: AD.day }, } } catch { throw new Error("The date doesn't fall within 2000/01/01 - 2090/12/30") @@ -372,7 +284,7 @@ export function convertToBS(adDateObject: Date): IAdBs { function mapLanguageNumber(dateNumber: string, language: 'en' | 'np'): string { return dateNumber .split('') - .map(num => formatObj[language].date[parseInt(num, 10)]) + .map((num) => formatObj[language].date[parseInt(num, 10)]) .join('') } @@ -424,7 +336,7 @@ export function parse(dateString: string): IYearMonthDate { return { year: parseInt(match[1], 10), month: parseInt(match[3], 10) - 1, - date: parseInt(match[5], 10) + date: parseInt(match[5], 10), } } match = dateString.match(GEORGIAN_FORMAT) @@ -432,7 +344,7 @@ export function parse(dateString: string): IYearMonthDate { return { year: parseInt(match[5], 10), month: parseInt(match[3], 10) - 1, - date: parseInt(match[1], 10) + date: parseInt(match[1], 10), } } throw new Error('Invalid date format') diff --git a/test/nepali-date-converter.test.ts b/test/nepali-date-converter.test.ts index 5002527..4bf7a0c 100644 --- a/test/nepali-date-converter.test.ts +++ b/test/nepali-date-converter.test.ts @@ -7,27 +7,32 @@ describe('NepaliDate to English', () => { date: now.getDate(), day: now.getDay(), year: now.getFullYear(), - month: now.getMonth() + month: now.getMonth(), }) expect(new NepaliDate(2054, 10, 10).getDateObject()).toEqual({ AD: { date: 22, day: 0, month: 1, year: 1998 }, - BS: { date: 10, day: 0, month: 10, year: 2054 } + BS: { date: 10, day: 0, month: 10, year: 2054 }, }) expect(new NepaliDate('2054 11 10').getDateObject()).toEqual({ AD: { date: 22, day: 0, month: 1, year: 1998 }, - BS: { date: 10, day: 0, month: 10, year: 2054 } + BS: { date: 10, day: 0, month: 10, year: 2054 }, }) expect(new NepaliDate('2054/11/10').getDateObject()).toEqual({ AD: { date: 22, day: 0, month: 1, year: 1998 }, - BS: { date: 10, day: 0, month: 10, year: 2054 } + BS: { date: 10, day: 0, month: 10, year: 2054 }, }) expect(new NepaliDate(new Date(1998, 1, 22)).getDateObject()).toEqual({ AD: { date: 22, day: 0, month: 1, year: 1998 }, - BS: { date: 10, day: 0, month: 10, year: 2054 } + BS: { date: 10, day: 0, month: 10, year: 2054 }, }) expect(new NepaliDate(new Date(1998, 1, 22).getTime()).getDateObject()).toEqual({ AD: { date: 22, day: 0, month: 1, year: 1998 }, - BS: { date: 10, day: 0, month: 10, year: 2054 } + BS: { date: 10, day: 0, month: 10, year: 2054 }, + }) + + expect(new NepaliDate(new Date(2024, 5, 14).getTime()).getDateObject()).toEqual({ + AD: { date: 14, day: 5, month: 5, year: 2024 }, + BS: { date: 32, day: 5, month: 1, year: 2081 }, }) }) @@ -39,7 +44,7 @@ describe('NepaliDate to English', () => { expect(date.getYear()).toEqual(2077) expect(date.getDateObject()).toEqual({ AD: { date: 22, day: 1, month: 1, year: 2021 }, - BS: { date: 10, day: 1, month: 10, year: 2077 } + BS: { date: 10, day: 1, month: 10, year: 2077 }, }) expect(date.getBS()).toEqual({ date: 10, day: 1, month: 10, year: 2077 }) expect(date.getAD()).toEqual({ date: 22, day: 1, month: 1, year: 2021 }) @@ -68,19 +73,19 @@ describe('NepaliDate to English', () => { date: now.getDate(), day: now.getDay(), year: now.getFullYear(), - month: now.getMonth() + month: now.getMonth(), }) expect(NepaliDate.parse('2054 11 10').getDateObject()).toEqual({ AD: { date: 22, day: 0, month: 1, year: 1998 }, - BS: { date: 10, day: 0, month: 10, year: 2054 } + BS: { date: 10, day: 0, month: 10, year: 2054 }, }) expect(NepaliDate.parse('2054/11/10').getDateObject()).toEqual({ AD: { date: 22, day: 0, month: 1, year: 1998 }, - BS: { date: 10, day: 0, month: 10, year: 2054 } + BS: { date: 10, day: 0, month: 10, year: 2054 }, }) expect(NepaliDate.fromAD(new Date(1998, 1, 22)).getDateObject()).toEqual({ AD: { date: 22, day: 0, month: 1, year: 1998 }, - BS: { date: 10, day: 0, month: 10, year: 2054 } + BS: { date: 10, day: 0, month: 10, year: 2054 }, }) })