compare-collections
v0.0.3
Published
Small util to compare collections
Downloads
6
Readme
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'}
*/