compare-collections

0.0.3 • Public • Published

compare-collections

Small util to compare collections

installation

npm install compare-collections --save

usage

var compare = require('compare-collections')

var collA = [{
  _id: 0,
  name: 'Level 1'
}, {
  _id: 1,
  name: 'Level 2'
}]

var collB = [{
  _id: 1,
  name: 'Level 2',
  parent: '0'
}, {
  _id: 2,
  name: 'Level 3'
  parent: '0'
}]

compare(collA, collB, function (id, newItem, oldItem) {
  var newDefined = newItem !== undefined
  var oldDefined = oldItem !== undefined
  
  if (newDefined && oldDefined) {
    console.log('updated', id, newItem, oldItem)
    return
  }
  if (newDefined) {
    console.log('added', id, newItem)
    return
  }
  if (oldDefined) {
    console.log('removed', id, oldItem)
  }
})

/* 
  Prints:
  
  'updated', 1, {_id: 1, name: 'Level 2', parent: '0'}, {_id: 1, name: 'Level 2', parent: '0'}
  'added', 2, {_id: 2, name: 'Level 3', parent: '0'}, {_id: 2, name: 'Level 3'}
  'removed', 0, undefined, {_id: 0, name: 'Level 1'}
*/

Readme

Keywords

none

Package Sidebar

Install

npm i compare-collections

Weekly Downloads

2

Version

0.0.3

License

ISC

Last publish

Collaborators

  • pakastin