frptools / collectable

High-performance immutable data structures for modern JavaScript and TypeScript applications. Functional interfaces, deep/composite operations API, mixed mutability API, TypeScript definitions, ES2015 module exports.
MIT License
273 stars 14 forks source link

setIn() second parameter is treated as a value #61

Closed ariesshrimp closed 7 years ago

ariesshrimp commented 7 years ago

In #55 I alluded to this, but currently setIn() has this API:

setIn([path to your destination in target collection], valueToSet, targetCollection)

thus:

setIn(['a', 'b', 'c', 'd', 'e'], true, from({}))
// expected: from({a: {b: {c: {d: {e: true}}}}}) πŸ‘ŒπŸ»

However, the README describes the API like so:

setIn(['foo', 'bar'], x => 'baz', from({})); 
// expected: <{foo: <{bar: 'baz'}>, xyz: ...> ❌
// actual: <{foo: <{bar: x => 'baz'}>, xyz: ...> 😱

Note that in the above example, the expected value of the resulting collection at bar is 'baz'. In actuality, the resulting value will be x => 'baz', because setIn() will set the target value as a reference to the function, rather than evaluating the function to get 'baz'

To get the results described in the README, the example would read:

setIn(['foo', 'bar'], 'baz', from({})); 
// expected: <{foo: <{bar: 'baz'}>, xyz: ...> πŸ‘ŒπŸ»

I think this might just be a typo in the README, but I want to make sure first.

ariesshrimp commented 7 years ago

If the issue is just a typo, the attached PR makes the described change.

axefrog commented 7 years ago

Yep, I suspect the fact that the example followed the updateIn signature had something to do with it...