Coder Social home page Coder Social logo

difference-accumulator's Introduction

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 = accumulator(originalData)

Apply a change:

acc.accumulate({ firstName: 'Frodo' })

Get out the sum of the changes:

acc.difference() // => { firstName: 'Frodo' }

Undo the accumulated changes:

acc.clear()
acc.difference() // => {}

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:

acc.accumulate({
	firstName: 'Frodo'
})
acc.accumulate({
	relationships: {
		uncle: 'Bilbo'
	}
})

Will accumulate to the total delta:

const difference = {
	firstName: 'Frodo',
	relationships: {
		uncle: 'Bilbo'
	}
}
acc.difference() // => difference

Accumulating falsey values (including undefined) will yield a difference which includes those properties:

const bagginsAccumulator = accumulator({ firstName: 'Bilbo' })
bagginsAccumulator.accumulate({ firstName: undefined })
bagginsAccumulator.difference() // => { firstName: undefined }

Accumulating values identical to the original data will yield a difference which does not include that change:

const bagginses = accumulator({ firstName: 'Bilbo' })
bagginses.accumulate({ firstName: 'Bilbo' })
bagginses.difference() // => {}

Note that the comparison is done using === on each property, therefore:

const justSomeGuy = accumulator({})
justSomeGuy.accumulate({ firstName: undefined })
justSomeGuy.difference() // => {}

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.

difference-accumulator's People

Contributors

saibotsivad avatar tehshrike avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

difference-accumulator's Issues

Differences inside objects are not properly detected

what happens

Consider this example:

const accumulator = require('difference-accumulator')
const acc = accumulator({ a: { b: 'x', c: 'y' } })

If we accumulate a change on the object, the difference is collected:

acc.accumulate({ a: { c: 'z' } })
acc.difference() // => { a: { c: 'z' } }

However, if we then accumulate a reverse change on the object, the difference is not collected:

acc.accumulate({ a: { c: 'y' } })
acc.difference() // => { a: { c: 'y' } }

what I expect to happen

My expectation is that the reversed change should yield an empty difference:

const accumulator = require('difference-accumulator')
const acc = accumulator({ a: { b: 'x', c: 'y' } })
acc.accumulate({ a: { c: 'z' } })
acc.difference() // => { a: { c: 'z' } }
acc.accumulate({ a: { c: 'y' } })
acc.difference() // => {}

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.