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: fix isDayjs check logic #2383

Merged
merged 4 commits into from
Jul 27, 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
8 changes: 6 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@ let L = 'en' // global locale
const Ls = {} // global loaded locale
Ls[L] = en

const isDayjs = d => d instanceof Dayjs // eslint-disable-line no-use-before-define
const IS_DAYJS = '$isDayjsObject'

// eslint-disable-next-line no-use-before-define
const isDayjs = d => d instanceof Dayjs || !!(d && d[IS_DAYJS])

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have a problem related to this change. We have a Dayjs object that we store in location.state where it get's serialized. Upon retrieving said object we use isDayjs to check whether the object is actually of type Dayjs or if we have to parse it, something along the lines of

const maybeBrokenDayjs(obj: any) => {
  if (isDayjs(obj) {
    return obj;
  } else if (obj?.$d) {
    return dayjs(obj.$d)
  }
}

This now obviously breaks as isDayjs will suddenly yield true. While this is of course easily fixable it is a breaking change and thus quite unexpected in a Patch-Release :(


const parseLocale = (preset, object, isLocal) => {
let l
Expand Down Expand Up @@ -72,7 +75,7 @@ const parseDate = (cfg) => {
|| 1, d[4] || 0, d[5] || 0, d[6] || 0, ms))
}
return new Date(d[1], m, d[3]
|| 1, d[4] || 0, d[5] || 0, d[6] || 0, ms)
|| 1, d[4] || 0, d[5] || 0, d[6] || 0, ms)
}
}

Expand All @@ -83,6 +86,7 @@ class Dayjs {
constructor(cfg) {
this.$L = parseLocale(cfg.locale, null, true)
this.parse(cfg) // for plugin
this[IS_DAYJS] = true
}

parse(cfg) {
Expand Down
8 changes: 8 additions & 0 deletions test/constructor.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,14 @@ it('supports instanceof dayjs', () => {
expect(dayjs() instanceof dayjs).toBeTruthy()
})

it('$isDayjsObject', () => {
const mockOtherVersionDayjsObj = {
$isDayjsObject: true
}
expect(dayjs.isDayjs(mockOtherVersionDayjsObj)).toBeTruthy()
})

it('does not break isDayjs', () => {
expect(dayjs.isDayjs(dayjs())).toBeTruthy()
expect(dayjs.isDayjs(new Date())).toBeFalsy()
})