simple-update-in
A lightweight updateIn
for immutable objects.
We love ImmutableJS. But sometimes, we want to start something from small. Thus, we created this package with zero dependencies.
Under the cover, we use Rest Operator to do most of the heavylifting.
Install
For latest stable, run npm install simple-update-in
.
For active development (master
branch), run npm install simple-update-in@master
.
How to use
We share similar signature as ImmutableJS.updateIn:
updateIn(
target: Array|Map,
path: (Number|String)[],
updater?: (value: any) => any
)
To make updateIn
efficient, especially, when paired with React. It will return a mixed deep/shallow clone of the target
. It only deep clone on objects that it modified along the path
, and shallow clone objects that it did not modify.
Example
Just like ImmutableJS, we want to make both Array
and Map
a first-class citizen. To work on a map, use a string
as key. For arrays, use a number
as key.
Map
import updateIn from 'simple-update-in';
const from = { one: 1, two: { number: 2 }, thirty: 3 };
const actual = updateIn(from, ['thirty'], three => three * 10);
expect(actual).toEqual({ one: 1, two: { number: 2 }, thirty: 30 });
expect(actual).not.toBe(from);
expect(actual.two).toBe(to.two);
expect(actual.thirty).toBe(30);
This is in fact an "upsert" operation.
Array in map
const from = { one: [1.1, 1.2, 1.3], two: [2] };
const actual = updateIn(from, ['one', 1], value => 'one point two');
expect(actual).toEqual({ one: [1.1, 'one point two', 1.3], two: [2] });
Remove a key
You can also use updateIn
to remove a key by passing a falsy value to the updater
argument.
const from = { one: 1, two: 2 };
const actual = updateIn(from, ['two']);
expect(actual).toEqual({ one: 1 });
expect(actual).not.toBe(from);
expect(actual).not.toHaveProperty('two');
Remove an item in array
const from = ['zero', 'one', 'two'];
const actual = updateIn(from, [1]);
expect(actual).toEqual(['zero', 'two']);
Automatic expansion
const from = {};
const actual = updateIn(from, ['one', 'two'], 1.2);
expect(actual).toEqual({ one: { two: 1.2 } });
Replace incompatible types
If incompatible types is found along the walk, they will be replaced. For example, in the following example, an Array
is replaced by a Map
.
const from = [0, 1, 2];
const actual = updateIn(from, ['one'], 1);
expect(actual).toEqual({ one: 1 });
In the path, 'one'
is a string, it implies that user want a Map
instead of Array
It will also replace number
with Map
.
const from = { one: 1 };
const actual = updateIn(from, ['one', 'two'], 1.2);
expect(actual).toEqual({ one: { two: 1.2 } });
Adding an item to array
You can use special index value -1
to indicate an append to the array.
const from = [0, 1];
const actual = updateIn(from, [-1], 2);
expect(actual).toEqual([0, 1, 2]);
There is no support on prepend or insertion, however, you can use Rest Operator for array manipulation.
const from = { numbers: ['one', 'two'] };
const actual = updateIn(from, ['numbers'], array => ['zero', ...array]);
expect(actual).toEqual({ numbers: ['zero', 'one', 'two'] });
Contributions
Like us? Star us.
Want to make it better? File us an issue.
Don't like something you see? Submit a pull request.