Update by path
Intent
It is a function that allows to make immutable alterations to objects. It's meant to reduce the code needed to update a deep value without any mutations. It brings readability by reducing the verbosity.
Installation
As simple as that
npm i --save update-by-path
;
Real-world example
I find this package extremely useful with redux reducers, so I'm going to use one of those as an example.
First off, let's take a look at what a simple reducer would look like without this package.
const INITIAL_STATE = isFetching: false list: error: null ; { const type payload = action; };
It can be a lot shorter if we use update from the package
... case typesFETCH_ATTEMPT: return ;case typesFETCH_SUCCESS: return ;default: return state; ...};
Same result. What about a bit more complex alterations?
Here we got to replace a list element by id:
... case typesREPLACE_BY_ID return ...state list: statelist; ; ...
Let's apply the update function:
... case typesREPLACE_BY_ID: return ; ...
Still a single line, and still completely immutable.
API
It accepts either 2, or 3 arguments.
; ;
They are completely identical, though the second option allows to make multiple insertions at a time.
Create new nodes
If the path contains nodes that do not exist in the source object, they are going to be created
;// { deep: { path: 'value' } }
Use function as a value
If you need to generate next value based on the previous one, use a function
const user = name: 'John'; ;// { name: 'JOHN' }
Go deep inside arrays
By value
const user = skills: 'html' 'javascript'; ;// { skills: ['html', 'JavaScript' ] }
By index
const user = skills: 'html' 'javascript'; ;// { skills: ['HTML', 'javascript' ] }
By property value
const user = skills: name: 'html' name: 'javascript' ; ;// { skills: [{ name: 'html'}, { name: 'JavaScript' }] },
If there are no matches for your query the source object remains intact.
Go even deeper inside array elements.
Alter a property of the matching array element. It doesn't metter which kind of query to use to match the element.
const user = skills: name: 'html' name: 'javascript' ; ;// { skills: [{ name: 'HTML'}, { name: 'javascript' }] },
Contribute
First off, thanks for taking the time to contribute! Now, take a moment to be sure your contributions make sense to everyone else.
LICENSE
This project is licensed under the MIT License - see the LICENSE file for details.