Mutater
Immutable helper for Objects and Arrays.
Mutate a copy of data without changing the original source.
Natural Chained Syntax. Only makes a shallow copy of log n objects (those modified) and reuses the rest.
ES5/ES6 distributions files. Small (1.0 Ko min/gzip) and Fast (minimal copy and actions).
Of course useful with React-style libs (aka (re)render based on dom-diffing).
Usage
; // or var mutate = require('mutater'); const obj = a: b: c: 'hop' d: e:true const newObj = ; // obj == { a: { b: { c: 'hop', d:[] } }, e:true } // the original one// newObj == { a: { b: { c: 'foo', d:[123] } } }
Rem : Never forget .val() at end of sentence to return actual value.
API
// obj[key] = value // obj[key] = !obj[key]; // delete obj[key]; // object handling // obj[key] = Object.assign({}, obj[key], value); (shallow) // array handling // obj[key].push(value); // obj[key].pop() // obj[key].unshift(value); // obj[key].shift() // obj[key].splice(index, number, ...value) // remove or add value in obj[key] // deeper modifications : move to path.to.sub.object and shallow copy path's node, then apply actions // End of chain : return new object (modified one)
"From" case :
const newObj = // at "root" : use mutate(obj) call to init mutation sentence......;
Rem : inner actions does not implement .val(). (so no need to call it)
Licence
The MIT License
Copyright 2017 (c) Gilles Coomans
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the 'Software'), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.