New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

array-subtract

Package Overview
Dependencies
Maintainers
1
Versions
7
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

array-subtract

Compute the difference between two arrays with an optional custom equality comparison method.

  • 2.0.0
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
62
decreased by-47.46%
Maintainers
1
Weekly downloads
 
Created
Source

array-subtract-functional-compare

Build Status Code Climate Test Coverage Dependency Status devDependency Status

js-standard-style NPM

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 arrays
[1, 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-subtract
var Subtract = require('array-subtract')
var subtract = new Subtract((a, b) => { return a === b })
subtract.sub([1, 2, 2, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 5], [1, 3, 4])
// => [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 = require('array-subtract')
/**
 * Arguments of comparator function passed to new Subtract()
 *
 * @param {*} itemA - Any element of an array argument passed to subtract.sub
 * @param {*} itemB - Any element of an array argument passed to subtract.sub
 */
var subtract = new Subtract((itemA, itemB) => { return itemA.name === itemB.name })

// namesA - namesB
var namesC = subtract.sub(namesA, namesB)
// => [{ 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

Keywords

FAQs

Package last updated on 14 Jan 2016

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc