Skip to content

Commit f95ac15

Browse files
authored
fix: Fix objectSupport plugin to get the correct result (zero-based month) (#1089)
1 parent 51360dd commit f95ac15

File tree

2 files changed

+59
-33
lines changed

2 files changed

+59
-33
lines changed

src/plugin/objectSupport/index.js

+4-3
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,13 @@ export default (o, c) => {
99
const { date, utc } = cfg
1010
const $d = {}
1111
if (isObject(date)) {
12+
const now = new Date()
1213
Object.keys(date).forEach((k) => {
1314
$d[prettyUnit(k)] = date[k]
1415
})
15-
const y = $d.year || 1970
16-
const M = $d.month - 1 || 0
17-
const d = $d.day || 1
16+
const d = $d.day || ((!$d.year && !($d.month >= 0)) ? now.getDate() : 1)
17+
const y = $d.year || now.getFullYear()
18+
const M = $d.month >= 0 ? $d.month : ((!$d.year && !$d.day) ? now.getMonth() : 0)// eslint-disable-line no-nested-ternary,max-len
1819
const h = $d.hour || 0
1920
const m = $d.minute || 0
2021
const s = $d.second || 0

test/plugin/objectSupport.test.js

+55-30
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1-
import moment from 'moment'
21
import MockDate from 'mockdate'
2+
import moment from 'moment'
33
import dayjs from '../../src'
44
import objectSupport from '../../src/plugin/objectSupport'
55
import quarterOfYear from '../../src/plugin/quarterOfYear'
66
import utc from '../../src/plugin/utc'
7+
import utils from '../../src/utils'
78

89
dayjs.extend(utc)
910
dayjs.extend(quarterOfYear)
@@ -17,40 +18,43 @@ afterEach(() => {
1718
MockDate.reset()
1819
})
1920
const now = new Date()
21+
const currentYear = now.getFullYear()
22+
const currentMonth = utils.s(now.getMonth() + 1, 2, '0')
23+
const currentDate = utils.s(now.getDate(), 2, '0')
2024
const fmt = 'YYYY-MM-DD HH:mm:ss.SSS'
2125
const tests = [
2226
[{ year: 2010 }, '2010-01-01 00:00:00.000'],
23-
[{ year: 2010, month: 1 }, '2010-01-01 00:00:00.000'],
24-
[{ year: 2010, month: 1, day: 12 }, '2010-01-12 00:00:00.000'],
25-
[{ year: 2010, month: 1, date: 12 }, '2010-01-12 00:00:00.000'],
27+
[{ year: 2010, month: 1 }, '2010-02-01 00:00:00.000'],
28+
[{ year: 2010, month: 1, day: 12 }, '2010-02-12 00:00:00.000'],
29+
[{ year: 2010, month: 1, date: 12 }, '2010-02-12 00:00:00.000'],
2630
[
2731
{
2832
hour: 15, minute: 25, second: 50, millisecond: 125
2933
},
30-
'1970-01-01 15:25:50.125'],
34+
`${currentYear}-${currentMonth}-${currentDate} 15:25:50.125`],
3135
[
3236
{
3337
year: 2010, month: 1, day: 12, hours: 1
3438
},
35-
'2010-01-12 01:00:00.000'
39+
'2010-02-12 01:00:00.000'
3640
],
3741
[
3842
{
3943
year: 2010, month: 1, date: 12, hours: 1
4044
},
41-
'2010-01-12 01:00:00.000'
45+
'2010-02-12 01:00:00.000'
4246
],
4347
[
4448
{
4549
year: 2010, month: 1, day: 12, hours: 1, minutes: 1
4650
},
47-
'2010-01-12 01:01:00.000'
51+
'2010-02-12 01:01:00.000'
4852
],
4953
[
5054
{
5155
year: 2010, month: 1, date: 12, hours: 1, minutes: 1
5256
},
53-
'2010-01-12 01:01:00.000'
57+
'2010-02-12 01:01:00.000'
5458
],
5559
[
5660
{
@@ -61,7 +65,7 @@ const tests = [
6165
minutes: 1,
6266
seconds: 1
6367
},
64-
'2010-01-12 01:01:01.000'
68+
'2010-02-12 01:01:01.000'
6569
],
6670
[
6771
{
@@ -73,7 +77,7 @@ const tests = [
7377
seconds: 1,
7478
milliseconds: 1
7579
},
76-
'2010-01-12 01:01:01.001'
80+
'2010-02-12 01:01:01.001'
7781
],
7882
[
7983
{
@@ -85,7 +89,7 @@ const tests = [
8589
seconds: 50,
8690
milliseconds: 125
8791
},
88-
'2010-01-14 15:25:50.125'
92+
'2010-02-14 15:25:50.125'
8993
],
9094
[
9195
{
@@ -97,24 +101,26 @@ const tests = [
97101
second: 50,
98102
millisecond: 125
99103
},
100-
'2010-01-14 15:25:50.125'
104+
'2010-02-14 15:25:50.125'
101105
],
102106
[
103107
{
104108
y: 2010, M: 1, d: 14, h: 15, m: 25, s: 50, ms: 125
105109
},
106-
'2010-01-14 15:25:50.125'
110+
'2010-02-14 15:25:50.125'
107111
]
108112
]
109113
it('Constructor from Object', () => {
110114
for (let i = 0; i < tests.length; i += 1) {
111115
expect(dayjs(tests[i][0]).format(fmt)).toBe(tests[i][1])
116+
expect(moment(tests[i][0]).format(fmt)).toBe(tests[i][1])
112117
}
113118
})
114119

115120
it('Constructor from Object UTC', () => {
116121
for (let i = 0; i < tests.length; i += 1) {
117122
expect(dayjs.utc(tests[i][0]).format(fmt)).toBe(tests[i][1])
123+
expect(moment.utc(tests[i][0]).format(fmt)).toBe(tests[i][1])
118124
}
119125
})
120126
it('Set from Object', () => {
@@ -139,9 +145,28 @@ it('add short reverse args', () => {
139145
expect(a.add({ h: 1 }).hour()).toBe(7)
140146
expect(a.add({ d: 1 }).date()).toBe(13)
141147
expect(a.add({ w: 1 }).date()).toBe(19)
142-
expect(a.add({ M: 1 }).month()).toBe(9)
148+
expect(a.add({ M: 1 }).month()).toBe(10)
143149
expect(a.add({ y: 1 }).year()).toBe(2012)
144-
expect(a.add({ Q: 1 }).month()).toBe(11)
150+
expect(a.add({ Q: 1 }).month()).toBe(0)
151+
152+
const aM = moment({
153+
year: 2011,
154+
month: 9,
155+
date: 12,
156+
hour: 6,
157+
minute: 7,
158+
second: 8,
159+
millisecond: 500
160+
})
161+
expect(aM.clone().add({ ms: 50 }).millisecond()).toBe(550)
162+
expect(aM.clone().add({ s: 1 }).second()).toBe(9)
163+
expect(aM.clone().add({ m: 1 }).minute()).toBe(8)
164+
expect(aM.clone().add({ h: 1 }).hour()).toBe(7)
165+
expect(aM.clone().add({ d: 1 }).date()).toBe(13)
166+
expect(aM.clone().add({ w: 1 }).date()).toBe(19)
167+
expect(aM.clone().add({ M: 1 }).month()).toBe(10)
168+
expect(aM.clone().add({ y: 1 }).year()).toBe(2012)
169+
expect(aM.clone().add({ Q: 1 }).month()).toBe(0)
145170

146171
const b = dayjs([2010, 1, 31]).add({ M: 1 })
147172
const c = dayjs([2010, 2, 28]).subtract({ M: 1 })
@@ -173,9 +198,9 @@ it('add long reverse args', () => {
173198
expect(a.add({ hours: 1 }).hour()).toBe(7)
174199
expect(a.add({ days: 1 }).date()).toBe(13)
175200
expect(a.add({ weeks: 1 }).date()).toBe(19)
176-
expect(a.add({ months: 1 }).month()).toBe(9)
201+
expect(a.add({ months: 1 }).month()).toBe(10)
177202
expect(a.add({ years: 1 }).year()).toBe(2012)
178-
expect(a.add({ quarters: 1 }).month()).toBe(11)
203+
expect(a.add({ quarters: 1 }).month()).toBe(0)
179204
})
180205

181206
it('add long singular reverse args', () => {
@@ -195,9 +220,9 @@ it('add long singular reverse args', () => {
195220
expect(a.add({ hour: 1 }).hour()).toBe(7)
196221
expect(a.add({ day: 1 }).date()).toBe(13)
197222
expect(a.add({ week: 1 }).date()).toBe(19)
198-
expect(a.add({ month: 1 }).month()).toBe(9)
223+
expect(a.add({ month: 1 }).month()).toBe(10)
199224
expect(a.add({ year: 1 }).year()).toBe(2012)
200-
expect(a.add({ quarter: 1 }).month()).toBe(11)
225+
expect(a.add({ quarter: 1 }).month()).toBe(0)
201226
})
202227

203228
it('add string long', () => {
@@ -217,9 +242,9 @@ it('add string long', () => {
217242
expect(a.add(1, 'hour').hour()).toBe(7)
218243
expect(a.add(1, 'day').date()).toBe(13)
219244
expect(a.add(1, 'week').date()).toBe(19)
220-
expect(a.add(1, 'month').month()).toBe(9)
245+
expect(a.add(1, 'month').month()).toBe(10)
221246
expect(a.add(1, 'year').year()).toBe(2012)
222-
expect(a.add(1, 'quarter').month()).toBe(11)
247+
expect(a.add(1, 'quarter').month()).toBe(0)
223248
})
224249

225250
it('add string long singular', () => {
@@ -239,9 +264,9 @@ it('add string long singular', () => {
239264
expect(a.add(1, 'hours').hour()).toBe(7)
240265
expect(a.add(1, 'days').date()).toBe(13)
241266
expect(a.add(1, 'weeks').date()).toBe(19)
242-
expect(a.add(1, 'months').month()).toBe(9)
267+
expect(a.add(1, 'months').month()).toBe(10)
243268
expect(a.add(1, 'years').year()).toBe(2012)
244-
expect(a.add(1, 'quarters').month()).toBe(11)
269+
expect(a.add(1, 'quarters').month()).toBe(0)
245270
})
246271

247272
it('add string short', () => {
@@ -261,9 +286,9 @@ it('add string short', () => {
261286
expect(a.add(1, 'h').hour()).toBe(7)
262287
expect(a.add(1, 'd').date()).toBe(13)
263288
expect(a.add(1, 'w').date()).toBe(19)
264-
expect(a.add(1, 'M').month()).toBe(9)
289+
expect(a.add(1, 'M').month()).toBe(10)
265290
expect(a.add(1, 'y').year()).toBe(2012)
266-
expect(a.add(1, 'Q').month()).toBe(11)
291+
expect(a.add(1, 'Q').month()).toBe(0)
267292
})
268293

269294
it('add strings string short', () => {
@@ -283,9 +308,9 @@ it('add strings string short', () => {
283308
expect(a.add('1', 'h').hour()).toBe(7)
284309
expect(a.add('1', 'd').date()).toBe(13)
285310
expect(a.add('1', 'w').date()).toBe(19)
286-
expect(a.add('1', 'M').month()).toBe(9)
311+
expect(a.add('1', 'M').month()).toBe(10)
287312
expect(a.add('1', 'y').year()).toBe(2012)
288-
expect(a.add('1', 'Q').month()).toBe(11)
313+
expect(a.add('1', 'Q').month()).toBe(0)
289314
})
290315

291316
it('add no string with milliseconds default', () => {
@@ -318,9 +343,9 @@ it('subtract strings string short', () => {
318343
expect(a.subtract('1', 'h').hour()).toBe(5)
319344
expect(a.subtract('1', 'd').date()).toBe(11)
320345
expect(a.subtract('1', 'w').date()).toBe(5)
321-
expect(a.subtract('1', 'M').month()).toBe(7)
346+
expect(a.subtract('1', 'M').month()).toBe(8)
322347
expect(a.subtract('1', 'y').year()).toBe(2010)
323-
expect(a.subtract('1', 'Q').month()).toBe(5)
348+
expect(a.subtract('1', 'Q').month()).toBe(6)
324349
})
325350

326351
it('add decimal values of days and months', () => {

0 commit comments

Comments
 (0)