Fix existsIn not working for symbols attached to arrays

This commit is contained in:
Jos de Jong 2020-07-26 11:09:29 +02:00
parent 23067b4638
commit bd73739343
2 changed files with 23 additions and 9 deletions

View File

@ -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))
}

View File

@ -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)
})
})