From bd73739343a13cb1a83dcb9d904c501292682885 Mon Sep 17 00:00:00 2001 From: Jos de Jong Date: Sun, 26 Jul 2020 11:09:29 +0200 Subject: [PATCH] Fix existsIn not working for symbols attached to arrays --- src/utils/immutabilityHelpers.js | 9 +-------- src/utils/immutabilityHelpers.test.js | 23 ++++++++++++++++++++++- 2 files changed, 23 insertions(+), 9 deletions(-) diff --git a/src/utils/immutabilityHelpers.js b/src/utils/immutabilityHelpers.js index 0812516..d8839b7 100644 --- a/src/utils/immutabilityHelpers.js +++ b/src/utils/immutabilityHelpers.js @@ -287,12 +287,5 @@ export function existsIn (json, path) { return true } - if (Array.isArray(json)) { - // index of an array - return existsIn(json[parseInt(path[0], 10)], path.slice(1)) - } - else { // Object - // object property. find the index of this property - return existsIn(json[path[0]], path.slice(1)) - } + return existsIn(json[path[0]], path.slice(1)) } diff --git a/src/utils/immutabilityHelpers.test.js b/src/utils/immutabilityHelpers.test.js index aa9a77f..cca5ad4 100644 --- a/src/utils/immutabilityHelpers.test.js +++ b/src/utils/immutabilityHelpers.test.js @@ -72,7 +72,7 @@ describe('immutabilityHelpers', () => { assert.throws(() => setIn(obj, ['a', 'b', 'c'], 4), /Path does not exist/) }) - it.only('setIn non existing path with createPath=true', () => { + it('setIn non existing path with createPath=true', () => { const obj = {} assert.deepStrictEqual(setIn(obj, ['a', 'b', 'c'], 4, true), { @@ -366,4 +366,25 @@ describe('immutabilityHelpers', () => { assert.deepStrictEqual(existsIn(json, ['obj', 'foo', 'bar']), false) assert.deepStrictEqual(existsIn(json, []), true) }) + + it('existsIn should find symbols', () => { + const json = { + "obj": { + "arr": [1,2, {"first":3,"last":4}] + }, + "str": "hello world", + "nill": null, + "bool": false + } + + const symbol = Symbol('mySymbol') + + const arrWithSymbol = [1, 2, 3] + arrWithSymbol[symbol] = 'yes' + + assert.deepStrictEqual(existsIn(arrWithSymbol, [symbol]), true) + assert.deepStrictEqual(existsIn([1, 2, 3], [symbol]), false) + assert.deepStrictEqual(existsIn({ a: 1, [symbol]: 2 }, [symbol]), true) + assert.deepStrictEqual(existsIn({ a: 1 }, [symbol]), false) + }) }) \ No newline at end of file