Skip to content

Commit

Permalink
feat: Arrays/isItemInCommon function (#127)
Browse files Browse the repository at this point in the history
  • Loading branch information
201flaviosilva authored May 29, 2024
1 parent f664a55 commit f0856d0
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 9 deletions.
15 changes: 11 additions & 4 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,17 @@ This file was based on [this template](https://gist.github.com/juampynr/4c18214a
----
## [1.4 - New utils](https://github.com/201flaviosilva/utilidades/milestone/3)

### [1.4.1] - 16-05-2023
- Move repository to a new Github organization;
### [1.4.2] - XX-XX-2024 (WIP)
#### Added
- `Arrays/isItemInCommon` - Checks if an item is in common between two arrays


### [1.4.1] - 16-05-2024

- Move repository to a new Github organization;

### [1.4.0] - 16-05-2023
-----
### [1.4.0] - 16-05-2024

## Fixed
- DataStructures/BinarySearchTree;
Expand All @@ -26,7 +33,7 @@ This file was based on [this template](https://gist.github.com/juampynr/4c18214a
----
## [1.3 - Tests and some Data Structures](https://github.com/201flaviosilva/utilidades/milestone/4)

### [1.3.3] - 13-05-2023
### [1.3.3] - 13-05-2024

#### Added
- randomWalk function;
Expand Down
1 change: 0 additions & 1 deletion src/Arrays/chunk.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@

/**
*
* Splits an array into smaller arrays of a specified size.
*
* @example chunk(["A", "B", "C", "D", "E", "F", "G"], 2); // [["A", "B"], ["C", "D"], ["E", "F"], ["G"]]
Expand Down
7 changes: 3 additions & 4 deletions src/Arrays/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { choice } from "./choice.js";
import { chunk } from "./chunk.js";
import { findBigObject } from "./findBigObject.js";
import { findLowObject } from "./findLowObject.js";
import { isItemInCommon } from "./isItemInCommon.js";
import { isSorted } from "./isSorted.js";
import { moveLeft, moveRight } from "./move.js";
import { removeDuplicatesObj } from "./removeDuplicatesObj.js";
Expand All @@ -22,11 +23,9 @@ export {
choice,
chunk,
findBigObject,
findLowObject,
isSorted,
moveLeft, moveRight,
findLowObject, isItemInCommon, isSorted, moveLeft, moveRight,
removeDuplicatesObj,
shuffle,
sortAscending, sortAscendingObject,
sortDescending, sortDescendingObject,
sortDescending, sortDescendingObject
};
18 changes: 18 additions & 0 deletions src/Arrays/isItemInCommon.js
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));
}
18 changes: 18 additions & 0 deletions tests/Arrays/isItemInCommon.test.js
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);
});
});

0 comments on commit f0856d0

Please sign in to comment.