Skip to content
Open
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
3 changes: 3 additions & 0 deletions packages/shared/src/__tests__/index.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import moment from 'moment'
import dayjs from 'dayjs'
import { Map as ImmutableMap } from 'immutable'
import { isEqual } from '../compare'
import {
Expand Down Expand Up @@ -374,6 +375,8 @@ describe('clone and compare', () => {
expect(clone(regexp) === regexp).toBeTruthy()
const promise = Promise.resolve(1)
expect(clone(promise) === promise).toBeTruthy()
const day = dayjs()
expect(clone(day).toISOString() === day.toISOString()).toBeTruthy()
})

test('shallowClone', () => {
Expand Down
20 changes: 20 additions & 0 deletions packages/shared/src/clone.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,24 @@ export const shallowClone = (values: any) => {
if (values['_isAMomentObject']) {
return values
}

// >= dayjs@1.11.10 has `$isDayjsObject` check logic
// following the `moment` convention, return the original object directly
if (values['$isDayjsObject']) {
return values
}
Comment thread
nanyuantingfeng marked this conversation as resolved.
if (values['_isJSONSchemaObject']) {
return values
}
if (isFn(values['toJS'])) {
return values
}
// < dayjs@1.11.10 has `clone` method, directly return a new object from `clone`, which won't cause logic issues.
// This check needs to be done before `toJSON` to avoid executing dayjs's `toJSON` method, which could cause type errors
// This logic check also applies to other similar objects, because the current method expects the return value to be the same as the input
if (isFn(values['clone'])) {
return values['clone']()
}
if (isFn(values['toJSON'])) {
return values
}
Expand Down Expand Up @@ -48,12 +60,20 @@ export const clone = (values: any) => {
if (values['_isAMomentObject']) {
return values
}

if (values['$isDayjsObject']) {
return values
}

if (values['_isJSONSchemaObject']) {
return values
}
if (isFn(values['toJS'])) {
return values['toJS']()
}
if (isFn(values['clone'])) {
return values['clone']()
}
if (isFn(values['toJSON'])) {
return values['toJSON']()
}
Expand Down
Loading