difference-accumulator
Accumulate differences in an object, get the total difference, reset the accumulator.
install
The normal way:
npm install difference-accumulator
const accumulator = require('difference-accumulator')
use
Create a new accumulator with the original object:
const originalData = firstName: 'Bilbo' lastName: 'Baggins'const acc =
Apply a change:
acc
Get out the sum of the changes:
acc // => { firstName: 'Frodo' }
Undo the accumulated changes:
accclearacc // => {}
api
acc = accumulator(originalData)
originalData
(object, optional)
Create a new accumulator by passing in an object property.
If originalData
is not specified, an object literal {}
will
be used.
acc.accumulate(delta)
delta
(object, optional)
The change to be applied to the original data.
If delta
is not specified, no difference will be accumulated.
For example:
accacc
Will accumulate to the total delta:
const difference = firstName: 'Frodo' relationships: uncle: 'Bilbo' acc // => difference
Accumulating falsey values (including undefined
) will
yield a difference which includes those properties:
const bagginsAccumulator = bagginsAccumulatorbagginsAccumulator // => { firstName: undefined }
Accumulating values identical to the original data will yield a difference which does not include that change:
const bagginses = bagginsesbagginses // => {}
Note that the comparison is done using ===
on each property, therefore:
const justSomeGuy = justSomeGuyjustSomeGuy // => {}
acc.difference()
This will yield the current total accumulated difference.
acc.clear()
This will clear the total accumulated difference.
license
Published and released under the Very Open License.