From 554ef642428935a5e35ca7dcfb64e2d8ba5b1646 Mon Sep 17 00:00:00 2001 From: David Bonnet Date: Fri, 14 Aug 2020 15:11:55 +0200 Subject: [PATCH] fix(immutables): handle nil values in `same` tool --- src/immutables.js | 8 +++++++- src/tests/immutables.js | 3 +++ 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/src/immutables.js b/src/immutables.js index 524ee38e..31bcbadb 100644 --- a/src/immutables.js +++ b/src/immutables.js @@ -179,7 +179,10 @@ export function setPath(target, path, value, index = 0) { export function same( a, b, - properties = a !== b && uniq(concat(keys(a), keys(b))), + properties = a !== b && + a != null && + b != null && + uniq(concat(keys(a), keys(b))), deep = false, ) { /* @@ -190,6 +193,9 @@ export function same( if (a === b) { return true } + if (a == null || b == null) { + return false + } const { length } = properties for (let i = 0; i < length; i++) { const property = properties[i] diff --git a/src/tests/immutables.js b/src/tests/immutables.js index 4fd7b1cb..af4d7539 100644 --- a/src/tests/immutables.js +++ b/src/tests/immutables.js @@ -249,6 +249,9 @@ test('same', (assert) => { same({ a }, { a: { b: 1 } }, ['a.b'], true), 'deeply dissimilar object', ) + assert.false(same(a, null), 'left different to null') + assert.false(same(null, b), 'right different to null') + assert.true(same(null, null), 'similar if both are null') }) test('different', (assert) => {