Skip to content

Commit c9370ea

Browse files
authored
fix: Fix objectSupport collides with Duration plugin - issue iamkun#2027 (iamkun#2038)
* fix: make objectSupport ignore other object types (issue iamkun#2027) while fixing, an issue wit the handling of subtract appeared and was fixed too. * test: add tests for issue iamkun#2027
1 parent 6c2a517 commit c9370ea

File tree

3 files changed

+184
-19
lines changed

3 files changed

+184
-19
lines changed

src/plugin/objectSupport/index.js

+27-19
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
export default (o, c, dayjs) => {
22
const proto = c.prototype
3-
const isObject = obj => !(obj instanceof Date) && !(obj instanceof Array) && obj instanceof Object
3+
const isObject = obj => !(obj instanceof Date) && !(obj instanceof Array)
4+
&& !proto.$utils().u(obj) && (obj.constructor.name === 'Object')
45
const prettyUnit = (u) => {
56
const unit = proto.$utils().p(u)
67
return unit === 'date' ? 'day' : unit
@@ -39,29 +40,36 @@ export default (o, c, dayjs) => {
3940

4041
const oldSet = proto.set
4142
const oldAdd = proto.add
43+
const oldSubtract = proto.subtract
4244

4345
const callObject = function (call, argument, string, offset = 1) {
44-
if (argument instanceof Object) {
45-
const keys = Object.keys(argument)
46-
let chain = this
47-
keys.forEach((key) => {
48-
chain = call.bind(chain)(argument[key] * offset, key)
49-
})
50-
return chain
51-
}
52-
return call.bind(this)(argument * offset, string)
46+
const keys = Object.keys(argument)
47+
let chain = this
48+
keys.forEach((key) => {
49+
chain = call.bind(chain)(argument[key] * offset, key)
50+
})
51+
return chain
5352
}
5453

55-
proto.set = function (string, int) {
56-
int = int === undefined ? string : int
57-
return callObject.bind(this)(function (i, s) {
58-
return oldSet.bind(this)(s, i)
59-
}, int, string)
54+
proto.set = function (unit, value) {
55+
value = value === undefined ? unit : value
56+
if (unit.constructor.name === 'Object') {
57+
return callObject.bind(this)(function (i, s) {
58+
return oldSet.bind(this)(s, i)
59+
}, value, unit)
60+
}
61+
return oldSet.bind(this)(unit, value)
6062
}
61-
proto.add = function (number, string) {
62-
return callObject.bind(this)(oldAdd, number, string)
63+
proto.add = function (value, unit) {
64+
if (value.constructor.name === 'Object') {
65+
return callObject.bind(this)(oldAdd, value, unit)
66+
}
67+
return oldAdd.bind(this)(value, unit)
6368
}
64-
proto.subtract = function (number, string) {
65-
return callObject.bind(this)(oldAdd, number, string, -1)
69+
proto.subtract = function (value, unit) {
70+
if (value.constructor.name === 'Object') {
71+
return callObject.bind(this)(oldAdd, value, unit, -1)
72+
}
73+
return oldSubtract.bind(this)(value, unit)
6674
}
6775
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
import MockDate from 'mockdate'
2+
import dayjs from '../../src'
3+
import duration from '../../src/plugin/duration'
4+
import objectSupport from '../../src/plugin/objectSupport'
5+
6+
dayjs.extend(objectSupport)
7+
dayjs.extend(duration)
8+
9+
beforeEach(() => {
10+
MockDate.set(new Date())
11+
})
12+
13+
afterEach(() => {
14+
MockDate.reset()
15+
})
16+
17+
// issue 2027
18+
describe('issue 2027 - order objectSupport > Duration', () => {
19+
it('add Duration object returns correct date', () => {
20+
const baseDate = dayjs('2022-06-26T14:01:02.003')
21+
const durationToAdd = dayjs.duration(6, 'hours')
22+
const testDate = baseDate.add(durationToAdd)
23+
24+
expect(testDate.format('YYYY-MM-DD HH:mm:ss.SSS')).toBe('2022-06-26 20:01:02.003')
25+
})
26+
it('subtract Duration object returns correct date', () => {
27+
const baseDate = dayjs('2022-06-26T14:01:02.003')
28+
const durationToAdd = dayjs.duration(6, 'hours')
29+
const testDate = baseDate.subtract(durationToAdd)
30+
31+
expect(testDate.format('YYYY-MM-DD HH:mm:ss.SSS')).toBe('2022-06-26 08:01:02.003')
32+
})
33+
34+
it('add number with unit returns correct date', () => {
35+
const baseDate = dayjs('2022-06-26T14:01:02.003')
36+
const testDate = baseDate.add(6, 'hours')
37+
38+
expect(testDate.format('YYYY-MM-DD HH:mm:ss.SSS')).toBe('2022-06-26 20:01:02.003')
39+
})
40+
it('subtract number with unit returns correct date', () => {
41+
const baseDate = dayjs('2022-06-26T14:01:02.003')
42+
const testDate = baseDate.subtract(6, 'hours')
43+
44+
expect(testDate.format('YYYY-MM-DD HH:mm:ss.SSS')).toBe('2022-06-26 08:01:02.003')
45+
})
46+
47+
it('parse string returns correct date', () => {
48+
const testDate = dayjs('2022-06-26T14:01:02.003')
49+
50+
expect(testDate.format('YYYY-MM-DD HH:mm:ss.SSS')).toBe('2022-06-26 14:01:02.003')
51+
})
52+
it('parse object returns correct date', () => {
53+
const testDate = dayjs({
54+
year: '2022',
55+
month: '05',
56+
day: '26',
57+
hour: '14',
58+
minute: '01',
59+
second: '02',
60+
millisecond: '003'
61+
})
62+
63+
expect(testDate.format('YYYY-MM-DD HH:mm:ss.SSS')).toBe('2022-06-26 14:01:02.003')
64+
})
65+
66+
it('set hour with number returns correct date', () => {
67+
const baseDate = dayjs('2022-06-26T14:01:02.003')
68+
const testDate = baseDate.hour(10)
69+
70+
expect(testDate.format('YYYY-MM-DD HH:mm:ss.SSS')).toBe('2022-06-26 10:01:02.003')
71+
})
72+
it('set hour with object returns correct date', () => {
73+
const baseDate = dayjs('2022-06-26T14:01:02.003')
74+
const testDate = baseDate.set({ hour: '10' })
75+
76+
expect(testDate.format('YYYY-MM-DD HH:mm:ss.SSS')).toBe('2022-06-26 10:01:02.003')
77+
})
78+
})
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
import MockDate from 'mockdate'
2+
import dayjs from '../../src'
3+
import duration from '../../src/plugin/duration'
4+
import objectSupport from '../../src/plugin/objectSupport'
5+
6+
dayjs.extend(duration)
7+
dayjs.extend(objectSupport)
8+
9+
beforeEach(() => {
10+
MockDate.set(new Date())
11+
})
12+
13+
afterEach(() => {
14+
MockDate.reset()
15+
})
16+
17+
// issue 2027
18+
describe('issue 2027 - order objectSupport > Duration', () => {
19+
it('add Duration object returns correct date', () => {
20+
const baseDate = dayjs('2022-06-26T14:01:02.003')
21+
const durationToAdd = dayjs.duration(6, 'hours')
22+
const testDate = baseDate.add(durationToAdd)
23+
24+
expect(testDate.format('YYYY-MM-DD HH:mm:ss.SSS')).toBe('2022-06-26 20:01:02.003')
25+
})
26+
27+
it('subtract Duration object returns correct date', () => {
28+
const baseDate = dayjs('2022-06-26T14:01:02.003')
29+
const durationToAdd = dayjs.duration(6, 'hours')
30+
const testDate = baseDate.subtract(durationToAdd)
31+
32+
expect(testDate.format('YYYY-MM-DD HH:mm:ss.SSS')).toBe('2022-06-26 08:01:02.003')
33+
})
34+
35+
it('add number with unit returns correct date', () => {
36+
const baseDate = dayjs('2022-06-26T14:01:02.003')
37+
const testDate = baseDate.add(6, 'hours')
38+
39+
expect(testDate.format('YYYY-MM-DD HH:mm:ss.SSS')).toBe('2022-06-26 20:01:02.003')
40+
})
41+
it('subtract number with unit returns correct date', () => {
42+
const baseDate = dayjs('2022-06-26T14:01:02.003')
43+
const testDate = baseDate.subtract(6, 'hours')
44+
45+
expect(testDate.format('YYYY-MM-DD HH:mm:ss.SSS')).toBe('2022-06-26 08:01:02.003')
46+
})
47+
48+
it('parse string returns correct date', () => {
49+
const testDate = dayjs('2022-06-26T14:01:02.003')
50+
51+
expect(testDate.format('YYYY-MM-DD HH:mm:ss.SSS')).toBe('2022-06-26 14:01:02.003')
52+
})
53+
it('parse object returns correct date', () => {
54+
const testDate = dayjs({
55+
year: '2022',
56+
month: '05',
57+
day: '26',
58+
hour: '14',
59+
minute: '01',
60+
second: '02',
61+
millisecond: '003'
62+
})
63+
64+
expect(testDate.format('YYYY-MM-DD HH:mm:ss.SSS')).toBe('2022-06-26 14:01:02.003')
65+
})
66+
67+
it('set hour with number returns correct date', () => {
68+
const baseDate = dayjs('2022-06-26T14:01:02.003')
69+
const testDate = baseDate.hour(10)
70+
71+
expect(testDate.format('YYYY-MM-DD HH:mm:ss.SSS')).toBe('2022-06-26 10:01:02.003')
72+
})
73+
it('set hour with object returns correct date', () => {
74+
const baseDate = dayjs('2022-06-26T14:01:02.003')
75+
const testDate = baseDate.set({ hour: '10' })
76+
77+
expect(testDate.format('YYYY-MM-DD HH:mm:ss.SSS')).toBe('2022-06-26 10:01:02.003')
78+
})
79+
})

0 commit comments

Comments
 (0)