Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: dayjs(null) throws error, not return dayjs object as invalid date #2334

Merged
merged 2 commits into from
Jun 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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