0.0.0 • Published 6 years ago
condom v0.0.0
muta
Mutate an object without affecting the original, and without cloning
muta
lets you wrap an object and make mutations to it, while only keeping in memory the unmodified original object and the mutation "patch". The wrapper can be accessed as if the mutations had made to the original object, and the mutations can be eventually committed to the original object if desired.
This module provides similar functionality to the jsondiffpatch
module, although it is more efficient for large objects since we build the patch as the data is mutated rather than cloning and diffing by iterating through all the keys.
Usage
npm install muta
let muta = require('muta')
let originalData = { foo: { bar: 123 } }
// `virtualData` appears to be the same as `originalData`,
// but changes you make to it don't affect `originalData`
let virtualData = muta(originalData)
virtualData.foo.bar += 1
console.log(originalData) // { foo: { bar: 123 } }
console.log(virtualData) // { foo: { bar: 124 } }
// if you want to apply the changes,
// call muta.commit on the wrapper
muta.commit(virtualData)
console.log(originalData) // { foo: { bar: 124 } }
0.0.0
6 years ago