-
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.
- added new differenceBy utility
- Loading branch information
Showing
6 changed files
with
77 additions
and
2 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,19 @@ | ||
/** | ||
* Returns an array of values from `arr` that are not present in `values`, | ||
* based on the result of applying `iteratee` function to each value. | ||
* | ||
* @template T The type of elements in the input array. | ||
* @template U The type of elements returned by the `iteratee` function. | ||
* @param {T[]} arr The input array to compare against. | ||
* @param {T[]} values The array of values to exclude from the result. | ||
* @param {(value: T) => U} iteratee The function to transform each value before comparison. | ||
* @returns {T[]} An array of values from `arr` that are not present in `values`. | ||
*/ | ||
export function differenceBy<T, U>( | ||
arr: T[], | ||
values: T[], | ||
iteratee: (value: T) => U, | ||
): T[] { | ||
const set = new Set(values.map(iteratee)); | ||
return arr.filter((a) => !set.has(iteratee(a))); | ||
} |
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,36 @@ | ||
import { describe, expect, it } from "bun:test"; | ||
import { differenceBy } from "../../src/array/differenceBy"; | ||
|
||
describe("differenceBy", () => { | ||
it("should return an array of values from `arr` that are not present in `values` based on the result of applying `iteratee` function", () => { | ||
const arr = [1, 2, 3, 4, 5]; | ||
const values = [2, 4, 6]; | ||
const iteratee = (value: number) => value % 2; | ||
const expected = [1, 3, 5]; | ||
expect(differenceBy(arr, values, iteratee)).toEqual(expected); | ||
}); | ||
|
||
it("should return an empty array if `arr` is empty", () => { | ||
const arr: number[] = []; | ||
const values = [2, 4, 6]; | ||
const iteratee = (value: number) => value % 2; | ||
const expected: number[] = []; | ||
expect(differenceBy(arr, values, iteratee)).toEqual(expected); | ||
}); | ||
|
||
it("should return `arr` if `values` is empty", () => { | ||
const arr = [1, 2, 3, 4, 5]; | ||
const values: number[] = []; | ||
const iteratee = (value: number) => value % 2; | ||
const expected = [1, 2, 3, 4, 5]; | ||
expect(differenceBy(arr, values, iteratee)).toEqual(expected); | ||
}); | ||
|
||
it("should return an empty array if `arr` and `values` are empty", () => { | ||
const arr: number[] = []; | ||
const values: number[] = []; | ||
const iteratee = (value: number) => value % 2; | ||
const expected: number[] = []; | ||
expect(differenceBy(arr, values, iteratee)).toEqual(expected); | ||
}); | ||
}); |