Skip to content

Commit

Permalink
fix: dayjs(null) throws error, not return dayjs object as invalid date (
Browse files Browse the repository at this point in the history
iamkun#2334)

Co-authored-by: iamkun <kunhello@outlook.com>
  • Loading branch information
alexander-ulaev and iamkun authored Jun 22, 2023
1 parent 24e3038 commit c79e2f5
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/plugin/objectSupport/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
export default (o, c, dayjs) => {
const proto = c.prototype
const isObject = obj => !(obj instanceof Date) && !(obj instanceof Array)
&& !proto.$utils().u(obj) && obj !== null && (obj.constructor.name === 'Object')
const isObject = obj => obj !== null && !(obj instanceof Date) && !(obj instanceof Array)
&& !proto.$utils().u(obj) && (obj.constructor.name === 'Object')
const prettyUnit = (u) => {
const unit = proto.$utils().p(u)
return unit === 'date' ? 'day' : unit
Expand Down
6 changes: 6 additions & 0 deletions test/plugin/objectSupport.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,12 @@ it('Constructor from Object UTC', () => {
expect(moment.utc(tests[i][0]).format(fmt)).toBe(result)
}
})

it('Constructor from null should return Invalid Date', () => {
expect(dayjs(null).isValid()).toBe(false)
expect(moment(null).isValid()).toBe(false)
})

it('Set from Object', () => {
for (let i = 0; i < tests.length; i += 1) {
expect(dayjs(now).set(tests[i][0]).format(fmt)).toBe(moment(now).set(tests[i][0]).format(fmt))
Expand Down

0 comments on commit c79e2f5

Please sign in to comment.