From 7206211d3960fb151c475cb28e11f9b7be42470b Mon Sep 17 00:00:00 2001 From: subeshb1 Date: Sat, 13 Oct 2018 00:35:53 +0545 Subject: [PATCH] Releasing First Version --- package.json | 26 ++++++ src/index.js | 239 +++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 265 insertions(+) create mode 100644 package.json create mode 100644 src/index.js diff --git a/package.json b/package.json new file mode 100644 index 0000000..1b8e902 --- /dev/null +++ b/package.json @@ -0,0 +1,26 @@ +{ + "name": "nepali-date", + "version": "1.0.0", + "description": "A small Js library to convert English date to Nepali and Vice Versa", + "main": "src/index.js", + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1" + }, + "repository": { + "type": "git", + "url": "git+https://github.com/subeshb1/Nepali-Date.git" + }, + "keywords": [ + "nepali", + "date", + "english", + "date", + "converter" + ], + "author": "'Subesh <'subeshb1@gmail.com'>", + "license": "MIT", + "bugs": { + "url": "https://github.com/subeshb1/Nepali-Date/issues" + }, + "homepage": "https://github.com/subeshb1/Nepali-Date#readme" +} diff --git a/src/index.js b/src/index.js new file mode 100644 index 0000000..37d961c --- /dev/null +++ b/src/index.js @@ -0,0 +1,239 @@ +const compose = (...fns) => (...args) => + fns.reduceRight((acc, next) => [next.call(null, ...acc)], args)[0]; +const map = fn => x => x.map(fn); +const add = (a, b) => a + b; +const reduce = fn => acc => x => x.reduce(fn, acc); +const calculateDaysInEachYearD = compose(map(reduce(add)(0))); + +const nepaliYears = [ + [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, 31, 32, 32, 31, 30, 30, 30, 29, 30, 30, 30], + [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 begin = { + year: 2000, + month: 0, + date: 1, + day: 3 +}; +const beginEnglish = { + year: 1943, + month: 3, + date: 14, + day: 3 +}; +const initTime = new Date( + beginEnglish.year, + beginEnglish.month, + beginEnglish.date +); +//2054/06/24 +const dayDiff = (date1, date2) => { + const timeDiff = Math.abs(date2.getTime() - date1.getTime()); + const diffDays = Math.ceil(timeDiff / (1000 * 3600 * 24)); + return diffDays; +}; + +const bsFromDays = difference => { + let bsYear = begin.year; + let bsMonth; + let bsDate = begin.date; + let days = 0; + for (let year of nepaliYears) { + bsMonth = 0; + for (let month of year) { + if (days + month < difference) { + days += month; + } else { + bsDate = difference - days + 1 || month; + return [bsYear, bsMonth, bsDate]; + } + bsMonth++; + } + bsYear++; + } +}; + +const monthsName = [ + "Baisakh", + "Jestha", + "Asadh", + "Shrawan", + "Bhadra", + "Ashwin", + "Kartik", + "Mangshir", + "Poush", + "Magh", + "Falgun", + "Chaitra" +]; +const weekDay = [ + "Sunday", + "Monday", + "Tuesday", + "Wednesday", + "Thursday", + "Friday", + "Saturday" +]; + +class NepaliDate { + constructor(year = begin.year, month = begin.month, date = begin.date) { + this.year = year || 2000; + this.month = month % 12 || 0; + this.date = date !== 0 ? date : nepaliYears[year % 100][month]; + this.convertBStoAD(); + } + + convertBStoAD() { + const diff = { + year: this.year - begin.year, + month: this.month - begin.month, + date: this.date - begin.date + }; + if (diff.year < 0 || diff.year > 90) + throw new TypeError("Can only support from 2000BS to 2090BS"); + const arr = nepaliYears + .slice(0, diff.year) + .concat([[...nepaliYears[diff.year].slice(0, diff.month), diff.date]]); + let diffDays = reduce(add)(0)(calculateDaysInEachYearD(arr)); + this.ad = new Date( + beginEnglish.year, + beginEnglish.month, + beginEnglish.date + diffDays + ); + this.day = this.ad.getDay(); + } + setMonth(month) { + this.month = month; + } + setMonth(year) { + this.year = year; + } + + toAD() { + return { + year: this.ad.getFullYear(), + month: this.ad.getMonth(), + date: this.ad.getDate() + }; + } + toJSDate() { + return new Date(this.ad.getTime()); + } + getDateAD() { + return this.ad.getDate(); + } + getDay() { + return this.day; + } + getMonthAD() { + return this.ad.getMonth(); + } + static fromAD( + year = beginEnglish.year, + month = beginEnglish.month, + date = beginEnglish.date + ) { + const difference = dayDiff(initTime, new Date(year, month, date)); + return new NepaliDate(...bsFromDays(difference)); + } + static now() {} + toString() { + return `${weekDay[this.day]}, ${monthsName[this.month]} ${this.date}, ${ + this.year + } BS`; + } +} + +module.exports = NepaliDate;