Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove moment dependency to reduce bundle sizes #2

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,14 +51,13 @@
},
"dependencies": {
"astronomia": "^1.3.5",
"caldate": "^1.0.0",
"date-chinese": "^1.0.2",
"date-easter": "^0.2.2",
"date-fns": "^1.29.0",
"lodash.get": "^4.4.2",
"lodash.merge": "^4.6.0",
"lodash.omit": "^4.5.0",
"lodash.set": "^4.3.2",
"moment-timezone": "^0.5.13"
"lodash.set": "^4.3.2"
},
"devDependencies": {
"babel-cli": "^6.26.0",
Expand Down
57 changes: 39 additions & 18 deletions src/CalEvent.js
Original file line number Diff line number Diff line change
@@ -1,25 +1,37 @@
'use strict'

const {isDate} = require('./internal/utils')
const CalDate = require('caldate')
const {isDate, toTimezone} = require('./internal/utils')
const addDays = require('date-fns/add_days')
const addHours = require('date-fns/add_hours')
const compareAsc = require('date-fns/compare_asc')
const format = require('date-fns/format')
const isSameDay = require('date-fns/is_same_day')
const setYear = require('date-fns/set_year')
const startOfDay = require('date-fns/start_of_day')

class CalEvent {
constructor (opts) {
opts = opts || {}
this.substitute = opts.substitute
this.opts = opts
this.offset = opts.offset
this.offset = opts.offset || 0
this.dates = []
if (isDate(opts)) {
this.opts = new CalDate(opts)
this.opts = new Date(opts)
}
}

inYear (year) {
const d = (new CalDate(this.opts)).setOffset(this.offset)
if (!(d.year && d.year !== year)) {
d.year = year
this.dates.push(d)
let d
if (this.opts instanceof Date) {
d = toTimezone(this.opts)
} else {
d = new Date(this.opts.year || year, this.opts.month - 1, this.opts.day)
}

if (!(d.getFullYear() && d.getFullYear() !== year)) {
d = setYear(d, year)
this.dates.push(addDays(d, this.offset))
}
return this
}
Expand All @@ -32,7 +44,7 @@ class CalEvent {
let res = false
for (const thisDate of this.dates) {
for (const date of calEvent.dates) {
res |= thisDate.isEqualDate(date)
res |= isSameDay(thisDate, date)
}
}
return !!res
Expand All @@ -46,20 +58,22 @@ class CalEvent {
filter (year, active) {
function isActive (date) {
if (!active) {
if (date.year === year) {
if (date.getFullYear() === year) {
return true
} else {
return false
}
}
const _date = date.toDate()

for (let a of active) {
const {from, to} = a
const fromBeforeOrEqualToDate = (compareAsc(from, date) !== 1)
const toAfterDate = (compareAsc(to, date) === 1)
if (
date.year === year &&
((from && to && from <= _date && to > _date) ||
(from && !to && from <= _date) ||
(!from && to && to > _date))
date.getFullYear() === year &&
((from && to && fromBeforeOrEqualToDate && toAfterDate) ||
(from && !to && fromBeforeOrEqualToDate) ||
(!from && to && toAfterDate))
) {
return true
}
Expand All @@ -83,10 +97,17 @@ class CalEvent {

get (timezone) {
const arr = this.dates.map((date) => {
let endDate
if (date.duration) {
endDate = addHours(date, date.duration)
} else {
endDate = startOfDay(addDays(date, 1))
}

const o = {
date: date.toString(),
start: date.toTimezone(timezone),
end: date.toEndDate().toTimezone(timezone)
date: format(date, 'YYYY-MM-DD HH:mm:ss'),
start: toTimezone(date, timezone),
end: toTimezone(endDate, timezone)
}
this._addSubstitute(date, o)
return o
Expand Down
13 changes: 6 additions & 7 deletions src/CalEventMap.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use strict'

const CalEvent = require('./CalEvent')
const CalDate = require('caldate')
const addDays = require('date-fns/add_days')

/**
* Mapper class for mapped calenders like hijri and hebrew
Expand Down Expand Up @@ -29,13 +29,12 @@ class CalEventMap extends CalEvent {
break
}
}
const d = (new CalDate({
year: y,
month: firstDays[i] + 1,
day: firstDays[i + 1]
})).setOffset(this.opts.day - 1)
const d = addDays(
new Date(y, firstDays[i], (firstDays[i + 1])),
this.opts.day - 1
)

if (d.year === year) {
if (d.getFullYear() === year) {
this.dates.push(d)
}
}
Expand Down
6 changes: 3 additions & 3 deletions src/Chinese.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

const CalChinese = require('date-chinese')
const CalEvent = require('./CalEvent')
const CalDate = require('caldate')
const addDays = require('date-fns/add_days')

class Chinese extends CalEvent {
/**
Expand Down Expand Up @@ -32,12 +32,12 @@ class Chinese extends CalEvent {
if (opts.solarterm) {
jde = this.cal.solarTerm(opts.solarterm, year)
date = this.cal.fromJDE(jde).toGregorian()
d = new CalDate(date).setOffset(opts.day - 1)
d = addDays(new Date(date.year, date.month - 1, date.day), opts.day - 1)
} else {
this.cal.set(opts.cycle, opts.year, opts.month, opts.leapMonth, opts.day)
jde = this.cal.toJDE(year)
date = this.cal.fromJDE(jde).toGregorian()
d = new CalDate(date)
d = new Date(date.year, date.month - 1, date.day)
}

this.dates.push(d)
Expand Down
6 changes: 4 additions & 2 deletions src/Easter.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@

const easter = require('date-easter')
const CalEvent = require('./CalEvent')
const CalDate = require('caldate')
const addDays = require('date-fns/add_days')
const addMinutes = require('date-fns/add_minutes')

class Easter extends CalEvent {
/**
Expand All @@ -21,7 +22,8 @@ class Easter extends CalEvent {
}

inYear (year) {
const d = (new CalDate(this._fn(year))).setOffset(this.offset)
let d = addDays(new Date(this._fn(year)), this.offset)
d = addMinutes(d, new Date().getTimezoneOffset())
this.dates.push(d)
return this
}
Expand Down
21 changes: 6 additions & 15 deletions src/Equinox.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@ const julian = require('astronomia/lib/julian')
const planetpos = require('astronomia/lib/planetposition')
const earth = new planetpos.Planet(require('astronomia/data/vsop87Bearth'))

const moment = require('moment-timezone')
const {toTimezone} = require('./internal/utils')
const CalEvent = require('./CalEvent')
const CalDate = require('caldate')

const addDays = require('date-fns/add_days')
const startOfDay = require('date-fns/start_of_day')

class Equinox extends CalEvent {
/**
Expand Down Expand Up @@ -45,20 +47,9 @@ class Equinox extends CalEvent {
}

const str = new julian.Calendar().fromJDE(jde).toDate().toISOString()
let date
if (/^[+-]\d{2}:\d{2}?$/.test(this._timezone)) { // for '+08:00' formats
date = moment(str).utcOffset(this._timezone)
} else { // for 'Asia/Shanghai' formats
date = moment(str).tz(this._timezone) // move to timezone
}

const floorDate = {
year: year,
month: date.month() + 1,
day: date.date()
}
const date = toTimezone(new Date(str), this._timezone) // move to timezone

const d = new CalDate(floorDate).setOffset(this.offset)
const d = addDays(startOfDay(date), this.offset)
this.dates.push(d)
return this
}
Expand Down
12 changes: 9 additions & 3 deletions src/Hebrew.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@

const CalEventMap = require('./CalEventMap')
const calendar = require('./internal/hebrew-calendar')
const {toTimezone} = require('./internal/utils')

const addDays = require('date-fns/add_days')
const addHours = require('date-fns/add_hours')
const format = require('date-fns/format')

class Hebrew extends CalEventMap {
constructor (opts) {
Expand All @@ -11,10 +16,11 @@ class Hebrew extends CalEventMap {

get (timezone) {
const arr = this.dates.map((date) => {
const _date = addHours(date, -6)
const o = {
date: date.toString() + ' -0600',
start: date.setOffset(-6, 'h').toTimezone(timezone),
end: date.toEndDate().toTimezone(timezone)
date: format(date, 'YYYY-MM-DD HH:mm:ss -0600'),
start: toTimezone(_date, timezone),
end: toTimezone(addDays(_date, 1), timezone)
}
this._addSubstitute(date, o)
return o
Expand Down
12 changes: 9 additions & 3 deletions src/Hijri.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@

const CalEventMap = require('./CalEventMap')
const calendar = require('./internal/hijri-calendar')
const {toTimezone} = require('./internal/utils')

const addDays = require('date-fns/add_days')
const addHours = require('date-fns/add_hours')
const format = require('date-fns/format')

class Hijri extends CalEventMap {
constructor (opts) {
Expand All @@ -11,10 +16,11 @@ class Hijri extends CalEventMap {

get (timezone) {
const arr = this.dates.map((date) => {
const _date = addHours(date, -6)
const o = {
date: date.toString() + ' -0600',
start: date.setOffset(-6, 'h').toTimezone(timezone),
end: date.toEndDate().toTimezone(timezone)
date: format(date, 'YYYY-MM-DD HH:mm:ss -0600'),
start: toTimezone(_date, timezone),
end: toTimezone(addDays(_date, 1), timezone)
}
this._addSubstitute(date, o)
return o
Expand Down
4 changes: 2 additions & 2 deletions src/Julian.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@

const julian = require('astronomia/lib/julian')
const CalEvent = require('./CalEvent')
const CalDate = require('caldate')
const addDays = require('date-fns/add_days')

class Julian extends CalEvent {
inYear (year) {
if (this.opts.year && this.opts.year !== year) {
return this
}
const cal = new julian.CalendarJulian(year, this.opts.month, this.opts.day).toGregorian()
const d = (new CalDate(cal)).setOffset(this.offset)
const d = addDays(new Date(cal.year, cal.month - 1, cal.day), this.offset)
this.dates.push(d)
return this
}
Expand Down
Loading