Skip to content

Commit bf1331e

Browse files
authored
fix(): add() & parse() bug fix & add locale de, pt-br
Refactor and fix
2 parents cdacf2d + 6b619ce commit bf1331e

7 files changed

+20
-20
lines changed

index.d.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ declare namespace dayjs {
8787

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

90-
export function extend(plugin: PluginFunc, option: ConfigType): Dayjs
90+
export function extend(plugin: PluginFunc, option?: ConfigType): Dayjs
9191

9292
export function local(arg1: any, arg2?: any): void
9393
}

package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@
7171
"pre-commit": "^1.2.2",
7272
"rollup": "^0.57.1",
7373
"rollup-plugin-babel": "^4.0.0-beta.4",
74-
"rollup-plugin-uglify": "^3.0.0"
74+
"rollup-plugin-uglify": "^3.0.0",
75+
"typescript": "^2.8.3"
7576
}
7677
}

src/constant.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ export const DATE = 'date'
2424
export const FORMAT_DEFAULT = 'YYYY-MM-DDTHH:mm:ssZ'
2525

2626
// regex
27-
export const REGEX_PARSE = /^(\d{4})-?(\d{0,2})-?(\d{0,2})(.*?(\d{1,2}):(\d{1,2}):(\d{1,2}))?.?(\d{1,3})?$/
27+
export const REGEX_PARSE = /^(\d{4})-?(\d{1,2})-?(\d{0,2})(.*?(\d{1,2}):(\d{1,2}):(\d{1,2}))?.?(\d{1,3})?$/
2828
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
2929

3030
export const en = {

src/index.js

+7-7
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ const parseDate = (date) => {
5252
if ((typeof date === 'string') && (reg = date.match(C.REGEX_PARSE))) {
5353
// 2018-08-08 or 20180808
5454
return new Date(
55-
reg[1], (reg[2] - 1) || 0, reg[3] || 1,
55+
reg[1], reg[2] - 1, reg[3] || 1,
5656
reg[5] || 0, reg[6] || 0, reg[7] || 0, reg[8] || 0
5757
)
5858
}
@@ -230,15 +230,15 @@ class Dayjs {
230230
number = Number(number) // eslint-disable-line no-param-reassign
231231
// units === 'ms' hard code here, will update in next release
232232
const unit = (units && (units.length === 1 || units === 'ms')) ? units : Utils.prettyUnit(units)
233+
const instanceFactory = (u, n) => {
234+
const date = this.set(C.DATE, 1).set(u, n + number)
235+
return date.set(C.DATE, Math.min(this.$D, date.daysInMonth()))
236+
}
233237
if (['M', C.M].indexOf(unit) > -1) {
234-
let date = this.set(C.DATE, 1).set(C.M, this.$M + number)
235-
date = date.set(C.DATE, Math.min(this.$D, date.daysInMonth()))
236-
return date
238+
return instanceFactory(C.M, this.$M)
237239
}
238240
if (['y', C.Y].indexOf(unit) > -1) {
239-
let date = this.set(C.DATE, 1).set(C.Y, this.$y + number)
240-
date = date.set(C.DATE, Math.min(this.$D, date.daysInMonth()))
241-
return date
241+
return instanceFactory(C.Y, this.$y)
242242
}
243243
let step
244244
switch (unit) {

src/utils.js

+3-10
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,9 @@ const monthDiff = (a, b) => {
1515
// function from moment.js in order to keep the same result
1616
const wholeMonthDiff = ((b.year() - a.year()) * 12) + (b.month() - a.month())
1717
const anchor = a.clone().add(wholeMonthDiff, 'months')
18-
let anchor2
19-
let adjust
20-
if (b - anchor < 0) {
21-
anchor2 = a.clone().add(wholeMonthDiff - 1, 'months')
22-
adjust = (b - anchor) / (anchor - anchor2)
23-
} else {
24-
anchor2 = a.clone().add(wholeMonthDiff + 1, 'months')
25-
adjust = (b - anchor) / (anchor2 - anchor)
26-
}
27-
return Number(-(wholeMonthDiff + adjust))
18+
const c = b - anchor < 0
19+
const anchor2 = a.clone().add(wholeMonthDiff + (c ? -1 : 1), 'months')
20+
return Number(-(wholeMonthDiff + ((b - anchor) / (c ? (anchor - anchor2) : (anchor2 - anchor)))))
2821
}
2922

3023
const absFloor = n => (n < 0 ? Math.ceil(n) || 0 : Math.floor(n))

test/manipulate.test.js

+2
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@ it('Add Time days', () => {
4343
expect(dayjs().add(1, 'M').valueOf()).toBe(moment().add(1, 'M').valueOf())
4444
expect(dayjs().add(1, 'y').valueOf()).toBe(moment().add(1, 'y').valueOf())
4545
expect(dayjs('20111031').add(1, 'months').valueOf()).toBe(moment('20111031').add(1, 'months').valueOf())
46+
expect(dayjs('20160131').add(1, 'months').valueOf()).toBe(moment('20160131').add(1, 'months').valueOf())
47+
expect(dayjs('20160229').add(1, 'year').valueOf()).toBe(moment('20160229').add(1, 'year').valueOf())
4648

4749
expect(dayjs().add('2', 'years').valueOf()).toBe(moment().add('2', 'years').valueOf())
4850
})

test/parse.test.js

+4
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,10 @@ describe('Parse', () => {
2929
expect(dayjs(d).valueOf()).toBe(moment(d).valueOf()) // not recommend
3030
d = '2018-4-1 1:1:1:223'
3131
expect(dayjs(d).valueOf()).toBe(moment(d).valueOf()) // not recommend
32+
d = '2018-01'
33+
expect(dayjs(d).valueOf()).toBe(moment(d).valueOf()) // not recommend
34+
d = '2018'
35+
expect(dayjs(d).format()).toBe(moment(d).format()) // not recommend
3236
})
3337

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

0 commit comments

Comments
 (0)