From 2a0533b33b09abc7e481e8e2e089dd9fda69b89a Mon Sep 17 00:00:00 2001 From: "moxey.eth" Date: Sat, 4 Jun 2022 18:20:36 +1000 Subject: [PATCH 1/2] Fix case where replaceDeepEqual was returning incorrect value for non-plain arrays --- src/core/tests/utils.test.tsx | 7 +++++++ src/core/utils.ts | 6 +++++- 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/src/core/tests/utils.test.tsx b/src/core/tests/utils.test.tsx index 1a0b8dd0db..0d1b44af04 100644 --- a/src/core/tests/utils.test.tsx +++ b/src/core/tests/utils.test.tsx @@ -258,6 +258,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 } }, diff --git a/src/core/utils.ts b/src/core/utils.ts index 8fdb107b54..6499cdd1af 100644 --- a/src/core/utils.ts +++ b/src/core/utils.ts @@ -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 @@ -355,6 +355,10 @@ export function shallowEqualObjects(a: T, b: T): boolean { return true } +export function isPlainArray(a: any) { + return Array.isArray(a) && a.length === Object.keys(a).length +} + // Copied from: https://github.com/jonschlinkert/is-plain-object export function isPlainObject(o: any): o is Object { if (!hasObjectPrototype(o)) { From 1b4633a9af1f322a4cc6fb574cadcb433d709e1c Mon Sep 17 00:00:00 2001 From: "moxey.eth" Date: Mon, 6 Jun 2022 08:07:02 +1000 Subject: [PATCH 2/2] fix pr comments --- src/core/tests/utils.test.tsx | 11 +++++++++++ src/core/utils.ts | 4 ++-- 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/src/core/tests/utils.test.tsx b/src/core/tests/utils.test.tsx index 0d1b44af04..6e00a9db00 100644 --- a/src/core/tests/utils.test.tsx +++ b/src/core/tests/utils.test.tsx @@ -6,6 +6,7 @@ import { matchMutation, scheduleMicrotask, sleep, + isPlainArray, } from '../utils' import { Mutation } from '../mutation' import { createQueryClient } from '../../tests/utils' @@ -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 ' }] } diff --git a/src/core/utils.ts b/src/core/utils.ts index 6499cdd1af..cdec3d83f4 100644 --- a/src/core/utils.ts +++ b/src/core/utils.ts @@ -355,8 +355,8 @@ export function shallowEqualObjects(a: T, b: T): boolean { return true } -export function isPlainArray(a: any) { - return Array.isArray(a) && a.length === Object.keys(a).length +export function isPlainArray(value: unknown) { + return Array.isArray(value) && value.length === Object.keys(value).length } // Copied from: https://github.com/jonschlinkert/is-plain-object