Skip to content

Commit c79e2f5

Browse files
alexander-ulaeviamkun
andauthoredJun 22, 2023
fix: dayjs(null) throws error, not return dayjs object as invalid date (#2334)
Co-authored-by: iamkun <kunhello@outlook.com>
1 parent 24e3038 commit c79e2f5

File tree

2 files changed

+8
-2
lines changed

2 files changed

+8
-2
lines changed
 

‎src/plugin/objectSupport/index.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
export default (o, c, dayjs) => {
22
const proto = c.prototype
3-
const isObject = obj => !(obj instanceof Date) && !(obj instanceof Array)
4-
&& !proto.$utils().u(obj) && obj !== null && (obj.constructor.name === 'Object')
3+
const isObject = obj => obj !== null && !(obj instanceof Date) && !(obj instanceof Array)
4+
&& !proto.$utils().u(obj) && (obj.constructor.name === 'Object')
55
const prettyUnit = (u) => {
66
const unit = proto.$utils().p(u)
77
return unit === 'date' ? 'day' : unit

‎test/plugin/objectSupport.test.js

+6
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,12 @@ it('Constructor from Object UTC', () => {
140140
expect(moment.utc(tests[i][0]).format(fmt)).toBe(result)
141141
}
142142
})
143+
144+
it('Constructor from null should return Invalid Date', () => {
145+
expect(dayjs(null).isValid()).toBe(false)
146+
expect(moment(null).isValid()).toBe(false)
147+
})
148+
143149
it('Set from Object', () => {
144150
for (let i = 0; i < tests.length; i += 1) {
145151
expect(dayjs(now).set(tests[i][0]).format(fmt)).toBe(moment(now).set(tests[i][0]).format(fmt))

0 commit comments

Comments
 (0)
Please sign in to comment.