Initial commit
commit
2213ddfbe1
@ -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`).
|
@ -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
|
@ -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));
|
||||
};
|
@ -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 <admin@cryto.net>",
|
||||
"license": "WTFPL OR CC0-1.0"
|
||||
}
|
Loading…
Reference in New Issue