array-subtract-functional-compare
Compute the difference between two arrays with an optional custom equality comparison method.
Inspired by Ruby's built in Array Difference: Ruby Docs: Array-Difference
array-subtract-functional-compare
is slightly different from the NPM package
array-difference. This module will subtract one array from another array, and return a new
array with a subset of the values in the first array. array-difference
computes the
symmetric difference (XOR) of two arrays.
Symmetric Difference vs Array Subtraction
A: [1, 2, 3, 4, 5]
B: [3, 4, 5, 6, 7]
Symmetric difference: A XOR B = [1, 2, 6, 7]
Array subtraction: A - B = [1, 2]
// In Ruby the subtraction operator is overloaded to work with arrays1 2 2 3 3 3 4 4 4 4 5 5 5 5 5 - 1 3 4// => [2, 2, 5, 5, 5, 5, 5] // Equivalent operation in JavaScript using array-subtractvar Subtract = var subtract = { return a === b }subtract// => [2, 2, 5, 5, 5, 5, 5]
Usage
var namesA = name: 'David' name: 'Jessica' name: 'Sam' name: 'Jessica' var namesB = name: 'Sam' name: 'Tim' var Subtract = /** * Arguments of comparator function passed to new Subtract() * * @param * @param */var subtract = { return itemAname === itemBname } // namesA - namesBvar namesC = subtract// => [{ name: 'David' }, { name: 'Jessica' }, { name: 'Jessica' }]
Testing
# Run using Node v4.0.0 or higher
$ npm run test
$ npm run html-test-cov # Will generate HTML coverage report and attempt to auto-open Chrome (OSX)
License
MIT