Skip to content

fix: replaceDeepEqual special case for non-plain arrays #3669

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

Merged
merged 2 commits into from
Jun 6, 2022
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
18 changes: 18 additions & 0 deletions src/core/tests/utils.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
matchMutation,
scheduleMicrotask,
sleep,
isPlainArray,
} from '../utils'
import { Mutation } from '../mutation'
import { createQueryClient } from '../../tests/utils'
Expand Down Expand Up @@ -56,6 +57,16 @@ describe('core/utils', () => {
})
})

describe('isPlainArray', () => {
it('should return `true` for plain arrays', () => {
expect(isPlainArray([1, 2])).toEqual(true)
})

it('should return `false` for non plain arrays', () => {
expect(isPlainArray(Object.assign([1, 2], { a: 'b' }))).toEqual(false)
})
})

describe('partialDeepEqual', () => {
it('should return `true` if a includes b', () => {
const a = { a: { b: 'b' }, c: 'c', d: [{ d: 'd ' }] }
Expand Down Expand Up @@ -258,6 +269,13 @@ describe('core/utils', () => {
expect(result[1]).toBe(prev[1])
})

it('should support objects which are not plain arrays', () => {
const prev = Object.assign([1, 2], { a: { b: 'b' }, c: 'c' })
const next = Object.assign([1, 2], { a: { b: 'b' }, c: 'c' })
const result = replaceEqualDeep(prev, next)
expect(result).toBe(next)
})

it('should replace all parent objects if some nested value changes', () => {
const prev = {
todo: { id: '1', meta: { createdAt: 0 }, state: { done: false } },
Expand Down
6 changes: 5 additions & 1 deletion src/core/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -314,7 +314,7 @@ export function replaceEqualDeep(a: any, b: any): any {
return a
}

const array = Array.isArray(a) && Array.isArray(b)
const array = isPlainArray(a) && isPlainArray(b)

if (array || (isPlainObject(a) && isPlainObject(b))) {
const aSize = array ? a.length : Object.keys(a).length
Expand Down Expand Up @@ -355,6 +355,10 @@ export function shallowEqualObjects<T>(a: T, b: T): boolean {
return true
}

export function isPlainArray(value: unknown) {
return Array.isArray(value) && value.length === Object.keys(value).length
}

// Copied from: https://github.com/jonschlinkert/is-plain-object
export function isPlainObject(o: any): o is Object {
if (!hasObjectPrototype(o)) {
Expand Down