Skip to content

Commit

Permalink
fix(): add() & parse() bug fix & add locale de, pt-br
Browse files Browse the repository at this point in the history
Refactor and fix
  • Loading branch information
iamkun authored May 18, 2018
2 parents cdacf2d + 6b619ce commit bf1331e
Show file tree
Hide file tree
Showing 7 changed files with 20 additions and 20 deletions.
2 changes: 1 addition & 1 deletion index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ declare namespace dayjs {

export type PluginFunc = (option: ConfigType, d1: Dayjs, d2: Dayjs) => void

export function extend(plugin: PluginFunc, option: ConfigType): Dayjs
export function extend(plugin: PluginFunc, option?: ConfigType): Dayjs

export function local(arg1: any, arg2?: any): void
}
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@
"pre-commit": "^1.2.2",
"rollup": "^0.57.1",
"rollup-plugin-babel": "^4.0.0-beta.4",
"rollup-plugin-uglify": "^3.0.0"
"rollup-plugin-uglify": "^3.0.0",
"typescript": "^2.8.3"
}
}
2 changes: 1 addition & 1 deletion src/constant.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export const DATE = 'date'
export const FORMAT_DEFAULT = 'YYYY-MM-DDTHH:mm:ssZ'

// regex
export const REGEX_PARSE = /^(\d{4})-?(\d{0,2})-?(\d{0,2})(.*?(\d{1,2}):(\d{1,2}):(\d{1,2}))?.?(\d{1,3})?$/
export const REGEX_PARSE = /^(\d{4})-?(\d{1,2})-?(\d{0,2})(.*?(\d{1,2}):(\d{1,2}):(\d{1,2}))?.?(\d{1,3})?$/
export const REGEX_FORMAT = /\[.*?\]|Y{2,4}|M{1,4}|D{1,2}|d{1,4}|H{1,2}|h{1,2}|a|A|m{1,2}|s{1,2}|Z{1,2}|SSS/g

export const en = {
Expand Down
14 changes: 7 additions & 7 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ const parseDate = (date) => {
if ((typeof date === 'string') && (reg = date.match(C.REGEX_PARSE))) {
// 2018-08-08 or 20180808
return new Date(
reg[1], (reg[2] - 1) || 0, reg[3] || 1,
reg[1], reg[2] - 1, reg[3] || 1,
reg[5] || 0, reg[6] || 0, reg[7] || 0, reg[8] || 0
)
}
Expand Down Expand Up @@ -230,15 +230,15 @@ class Dayjs {
number = Number(number) // eslint-disable-line no-param-reassign
// units === 'ms' hard code here, will update in next release
const unit = (units && (units.length === 1 || units === 'ms')) ? units : Utils.prettyUnit(units)
const instanceFactory = (u, n) => {
const date = this.set(C.DATE, 1).set(u, n + number)
return date.set(C.DATE, Math.min(this.$D, date.daysInMonth()))
}
if (['M', C.M].indexOf(unit) > -1) {
let date = this.set(C.DATE, 1).set(C.M, this.$M + number)
date = date.set(C.DATE, Math.min(this.$D, date.daysInMonth()))
return date
return instanceFactory(C.M, this.$M)
}
if (['y', C.Y].indexOf(unit) > -1) {
let date = this.set(C.DATE, 1).set(C.Y, this.$y + number)
date = date.set(C.DATE, Math.min(this.$D, date.daysInMonth()))
return date
return instanceFactory(C.Y, this.$y)
}
let step
switch (unit) {
Expand Down
13 changes: 3 additions & 10 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,9 @@ const monthDiff = (a, b) => {
// function from moment.js in order to keep the same result
const wholeMonthDiff = ((b.year() - a.year()) * 12) + (b.month() - a.month())
const anchor = a.clone().add(wholeMonthDiff, 'months')
let anchor2
let adjust
if (b - anchor < 0) {
anchor2 = a.clone().add(wholeMonthDiff - 1, 'months')
adjust = (b - anchor) / (anchor - anchor2)
} else {
anchor2 = a.clone().add(wholeMonthDiff + 1, 'months')
adjust = (b - anchor) / (anchor2 - anchor)
}
return Number(-(wholeMonthDiff + adjust))
const c = b - anchor < 0
const anchor2 = a.clone().add(wholeMonthDiff + (c ? -1 : 1), 'months')
return Number(-(wholeMonthDiff + ((b - anchor) / (c ? (anchor - anchor2) : (anchor2 - anchor)))))
}

const absFloor = n => (n < 0 ? Math.ceil(n) || 0 : Math.floor(n))
Expand Down
2 changes: 2 additions & 0 deletions test/manipulate.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ it('Add Time days', () => {
expect(dayjs().add(1, 'M').valueOf()).toBe(moment().add(1, 'M').valueOf())
expect(dayjs().add(1, 'y').valueOf()).toBe(moment().add(1, 'y').valueOf())
expect(dayjs('20111031').add(1, 'months').valueOf()).toBe(moment('20111031').add(1, 'months').valueOf())
expect(dayjs('20160131').add(1, 'months').valueOf()).toBe(moment('20160131').add(1, 'months').valueOf())
expect(dayjs('20160229').add(1, 'year').valueOf()).toBe(moment('20160229').add(1, 'year').valueOf())

expect(dayjs().add('2', 'years').valueOf()).toBe(moment().add('2', 'years').valueOf())
})
Expand Down
4 changes: 4 additions & 0 deletions test/parse.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ describe('Parse', () => {
expect(dayjs(d).valueOf()).toBe(moment(d).valueOf()) // not recommend
d = '2018-4-1 1:1:1:223'
expect(dayjs(d).valueOf()).toBe(moment(d).valueOf()) // not recommend
d = '2018-01'
expect(dayjs(d).valueOf()).toBe(moment(d).valueOf()) // not recommend
d = '2018'
expect(dayjs(d).format()).toBe(moment(d).format()) // not recommend
})

it('String ISO 8601 date, time and zone', () => {
Expand Down

0 comments on commit bf1331e

Please sign in to comment.