From 2213ddfbe102a254fed3d9fdf7f6e9af32f477cc Mon Sep 17 00:00:00 2001 From: Sven Slootweg Date: Wed, 27 May 2020 13:33:00 +0200 Subject: [PATCH] Initial commit --- README.md | 38 ++++++++++++++++++++++++++++++++++++++ example.js | 13 +++++++++++++ index.js | 15 +++++++++++++++ package.json | 9 +++++++++ 4 files changed, 75 insertions(+) create mode 100644 README.md create mode 100644 example.js create mode 100644 index.js create mode 100644 package.json diff --git a/README.md b/README.md new file mode 100644 index 0000000..e3dc709 --- /dev/null +++ b/README.md @@ -0,0 +1,38 @@ +# do-arrays-intersect + +Checks only *whether* two given arrays intersect (ie. have values in common), but not *what* that intersection is exactly. This allows it to stop searching when it finds the first match, which can be much faster. + +## License, donations, and other boilerplate + +Licensed under either the [WTFPL](http://www.wtfpl.net/txt/copying/) or [CC0](https://creativecommons.org/publicdomain/zero/1.0/), at your choice. In practice, that means it's more or less public domain, and you can do whatever you want with it. Giving credit is *not* required, but still very much appreciated! I'd love to [hear from you](mailto:admin@cryto.net) if this module was useful to you. + +Creating and maintaining open-source modules is a lot of work. A donation is also not required, but much appreciated! You can donate [here](http://cryto.net/~joepie91/donate.html). + +## Example + +```js +const doArraysIntersect = require("do-arrays-intersect"); + +let arrayA = [1, 3, 4, 6, 7, 8]; +let arrayB = [2, 5, 6, 8, 9]; + +console.log(doArraysIntersect(arrayA, arrayB)); // true + +let arrayC = [1, 3, 4, 6, 7, 8]; +let arrayD = [2, 5, 9, 10, 11]; + +console.log(doArraysIntersect(arrayC, arrayD)); // false +``` + +## API + +### doArraysIntersect(arrayA, arrayB) + +Checks whether two arrays intersect. The equivalence function is `===`, and custom equivalence functions are not currently supported; please file an issue if you need this! + +The order that you specify the arrays in, does not matter. + +- __arrayA:__ The first array. +- __arrayB:__ The second array. + +Returns a boolean indicating whether an intersection was found (`true`) or not (`false`). diff --git a/example.js b/example.js new file mode 100644 index 0000000..c8c3e52 --- /dev/null +++ b/example.js @@ -0,0 +1,13 @@ +"use strict"; + +const doArraysIntersect = require("./"); + +let arrayA = [1, 3, 4, 6, 7, 8]; +let arrayB = [2, 5, 6, 8, 9]; + +console.log(doArraysIntersect(arrayA, arrayB)); // true + +let arrayC = [1, 3, 4, 6, 7, 8]; +let arrayD = [2, 5, 9, 10, 11]; + +console.log(doArraysIntersect(arrayC, arrayD)); // false diff --git a/index.js b/index.js new file mode 100644 index 0000000..78d9ea4 --- /dev/null +++ b/index.js @@ -0,0 +1,15 @@ +"use strict"; + +module.exports = function doArraysIntersect(arrayA, arrayB) { + let oneSet, otherArray; + + if (arrayA.length > arrayB.length) { + oneSet = new Set(arrayA); + otherArray = arrayB; + } else { + oneSet = new Set(arrayB); + otherArray = arrayA; + } + + return otherArray.some((item) => oneSet.has(item)); +}; diff --git a/package.json b/package.json new file mode 100644 index 0000000..2b8dc6f --- /dev/null +++ b/package.json @@ -0,0 +1,9 @@ +{ + "name": "do-arrays-intersect", + "version": "1.0.0", + "main": "index.js", + "description": "Check whether arrays intersect without caring about the exact intersection, which is faster", + "repository": "git@git.cryto.net:joepie91/do-arrays-intersect.git", + "author": "Sven Slootweg ", + "license": "WTFPL OR CC0-1.0" +}