mutater

0.0.6 • Public • Published

Mutater

Travis branch npm npm-downloads licence dependecies dev-dependencies

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

import mutate from 'mutater';  // or var mutate = require('mutater');
 
const obj = { a: { b: { c: 'hop', d:[] } }, e:true }
const newObj = mutate(obj)
    .from('a.b',
        mutate.set('c', 'foo')
        .push('d', 123)
    )
    .delete(e)
    .val();
 
 
// 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

mutate(myObj)
.set(key, value) // obj[key] = value
.toggle(key)     // obj[key] = !obj[key];
.delete(key)  // delete obj[key];
 
// object handling
.merge(key, value)  // obj[key] = Object.assign({}, obj[key], value); (shallow)
 
// array handling
.push(key, value) // obj[key].push(value);
.pop(key) // obj[key].pop()
.unshift(key, value) // obj[key].unshift(value);
.shift(key) // obj[key].shift()
.splice(key, index, number, ...value) // obj[key].splice(index, number, ...value)
.toggleInArray(key, 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
.from('path.to.sub.object', mutateActions)
 
// End of chain : return new object (modified one)
.val()

"From" case :

const newObj = mutate(myObj) // at "root" : use mutate(obj) call to init mutation sentence...
.from('a.b.c', 
    mutate  // inner actions : start without call
        .set('d', myValue)
        .pop('e')
        ...
)
...
.val();
 

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.

Readme

Keywords

Package Sidebar

Install

npm i mutater

Weekly Downloads

0

Version

0.0.6

License

MIT

Last publish

Collaborators

  • nomocas