-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Arrays/isItemInCommon function (#127)
- Loading branch information
1 parent
f664a55
commit f0856d0
Showing
5 changed files
with
50 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
/** | ||
* Checks if an item is in common between two arrays | ||
* | ||
* @example | ||
* isItemInCommon([1, 2, 3], [2, 3, 4]); // true | ||
* isItemInCommon(["A", "B", "C"], ["B", "C", "D"]) // true | ||
* isItemInCommon([1, 2, 3], [4, 5, 6]); // false | ||
* | ||
* @param {any[]} arr1 - The first array | ||
* @param {any[]} arr2 - The second array | ||
* @returns {boolean} true if the item is in common | ||
* | ||
* @function isItemInCommon | ||
* @memberof Arrays | ||
*/ | ||
export function isItemInCommon(arr1, arr2) { | ||
return arr1.some(item => arr2.includes(item)); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import { Arrays } from "./src/main"; | ||
const { isItemInCommon } = Arrays; | ||
|
||
describe("Arrays/isItemInCommon.js", () => { | ||
it("should return true for items in common", () => { | ||
expect(isItemInCommon([1, 2, 3], [2, 3, 4])).toEqual(true); | ||
expect(isItemInCommon(["A", "B", "C"], ["B", "C", "D"])).toEqual(true); | ||
expect(isItemInCommon([0], [0])).toEqual(true); | ||
expect(isItemInCommon([0], [1, 2, 3, 0, 4])).toEqual(true); | ||
}); | ||
|
||
it("should return false for items not in common", () => { | ||
expect(isItemInCommon([1, 2, 3], [4, 5, 6])).toEqual(false); | ||
expect(isItemInCommon(["A", "B", "C"], ["D", "E", "F"])).toEqual(false); | ||
expect(isItemInCommon([0], [1])).toEqual(false); | ||
expect(isItemInCommon([], [])).toEqual(false); | ||
}); | ||
}); |