dot-prop-immutable
Immutable version of dot-prop with some extensions.
npm install dot-prop-immutable
The motivation for this module is to have a simple utility for changing state in a React-Redux application without mutating the existing state of plain JavaScript objects.
If you are going for real immutable data collections take a look at the cool library Immutable.js.
A good practice is not to mix the immutable data collections with mutable objects because it can lead to confusion. Immutable objects are not accessed by the default semantics, but implemented by setters and getters.
This library implements 3 helper functions:
get(object, path) --> value
set(object, path, value) --> object
delete(object, path) --> object
None of the functions mutate the input object. For efficiency, the returned object is not a deep clone of the original, but a shallow copy of the objects in the mutated path.
Usage
const dotProp = require('dot-prop-immutable');
let state = { todos: [] }, index = 0;
state = dotProp.set(state, 'todos', list => [...list, {text: 'cleanup', complete: false}])
state = {...state, todos: [...state.todos, {text: 'cleanup', complete: false}]};
state = dotProp.set(state, `todos.${index}.complete`, true)
state = {...state, todos: [
...state.todos.slice(0, index),
{...state.todos[index], complete: true},
...state.todos.slice(index + 1)
]};
state = dotProp.delete(state, `todos.${index}`)
state = {...state, todos: [
...state.todos.slice(0, index),
...state.todos.slice(index + 1)
]};
get
Access a nested property by a dot path
dotProp.get({foo: {bar: 'unicorn'}}, 'foo.bar')
dotProp.get({foo: {bar: 'a'}}, 'foo.notDefined.deep')
dotProp.get({foo: {bar: 'a'}}, 'foo.notDefined.deep', 'default value')
dotProp.get({foo: {'dot.dot': 'unicorn'}}, 'foo.dot\\.dot')
or use a property array as a path.
dotProp.get({foo: {'dot.dot': 'unicorn'}}, ['foo', 'dot.dot'])
It is also possible to index into an array where the special index $end
refers to the last element of the array.
const obj = {foo: [{ bar: 'gold-unicorn'}, 'white-unicorn', 'silver-unicorn']};
dotProp.get(obj, 'foo.1')
dotProp.get(obj, 'foo.0.bar')
dotProp.get(obj, 'foo.$end')
dotProp.get([{ bar: 'gold-unicorn'}, 'white-unicorn', 'silver-unicorn'], '0.bar')
set
Modify a nested property by a dot path
const obj = {foo: {bar: 'a'}};
const obj1 = dotProp.set(obj, 'foo.bar', 'b');
const obj2 = dotProp.set(obj1 , 'foo.baz', 'x');
where obj
, obj1
, obj2
, obj3
all are different objects.
Use a function to modify the selected property, where first argument is the old value.
dotProp.set({foo: {bar: 'a'}}, 'foo.bar', v => v + 'bc')
Modify a nested array
const obj = {foo: [{ bar: 'gold-unicorn'}, 'white-unicorn', 'silver-unicorn']};
dotProp.set(obj, 'foo.1', 'platin-unicorn')
dotProp.set(obj, 'foo.0.bar', 'platin-unicorn')
dotProp.set(obj, 'foo.$end', 'platin-unicorn')
delete
Delete a nested property/array by a dot path
const obj = {foo: [{ bar: 'gold-unicorn'}, 'white-unicorn', 'silver-unicorn']};
dotProp.delete(obj, 'foo.$end');
dotProp.delete(obj, 'foo.0.bar');
toggle
Toggle a boolean a value by a dot path.
const obj = {foo: { bar: true } };
dotProp.toggle(obj, 'foo.bar');
merge
Merge a value by a dot path.
The target value must be an object, array, null, or undefined.
- If target is an object, Object.assign({}, target, param) is used.
- If target an array, target.concat(param) is used.
- If target is null or undefined, the value is simply set.
const obj = {foo: { bar: {a:1, b:2 } };
dotProp.merge(obj, 'foo.bar', {c:3} );
var arr = {foo: { bar: [1, 2] } };
dotProp.merge(arr, 'foo.bar', [3, 4] );
License
MIT