Can use an array as root object?
Closed this issue · 1 comments
spacejack commented
Hi, great little library. I wasn't sure if it was intended to work with an array as the root object, but it seems to be ok in my project.
I also added this to set
tests and it passed:
it('can use array as root', () => {
const tree = [
{ name: 'John', age: 26 },
{ name: 'Jenny', age: 33 },
{ name: 'Marvin', age: 42 }
]
const updatedTree = set(tree, _ => _[0].name)('Bobby')
expect(updatedTree).to.be.an('array')
expect(updatedTree.length).to.equal(3)
expect(updatedTree[0].name).to.equal('Bobby')
expect(updatedTree[1].name).to.equal('Jenny')
expect(updatedTree[2].name).to.equal('Marvin')
const updatedTree2 = set(tree, _ => _[2].age)(55)
expect(updatedTree2).to.be.an('array')
expect(updatedTree2.length).to.equal(3)
expect(updatedTree2[0].age).to.equal(26)
expect(updatedTree2[1].age).to.equal(33)
expect(updatedTree2[2].age).to.equal(55)
})
Would there be any gotchas to watch out for?
kube commented
Yes working with an Array as root object should not be a problem.
The only issue here will be in performance, as I don't provide nor handle any specific data structure in this library for now.