Skip to content

Commit b6ffb6d

Browse files
committed
test: add unit tests for uniqBy function to verify duplicate handling
1 parent 45a155a commit b6ffb6d

File tree

2 files changed

+20
-0
lines changed

2 files changed

+20
-0
lines changed

src/array/uniqBy.spec.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,22 @@ describe('uniqBy', () => {
66
expect(uniqBy([2.1, 1.2, 2.3], Math.floor)).toEqual([2.1, 1.2]);
77
expect(uniqBy([1.2, 1.5, 2.1, 3.2, 5.7, 5.3, 7.19], Math.floor)).toEqual([1.2, 2.1, 3.2, 5.7, 7.19]);
88
});
9+
10+
it('should keep the first occurrence when duplicates are found', () => {
11+
const items = [
12+
{ id: 1, value: 'apple' },
13+
{ id: 2, value: 'banana' },
14+
{ id: 1, value: 'avocado' },
15+
{ id: 3, value: 'cherry' },
16+
{ id: 2, value: 'blueberry' },
17+
];
18+
19+
const result = uniqBy(items, x => x.id);
20+
21+
expect(result).toEqual([
22+
{ id: 1, value: 'apple' },
23+
{ id: 2, value: 'banana' },
24+
{ id: 3, value: 'cherry' },
25+
]);
26+
});
927
});

src/array/uniqBy.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
* Returns a new array containing only the unique elements from the original array,
33
* based on the values returned by the mapper function.
44
*
5+
* When duplicates are found, the first occurrence is kept and the rest are discarded.
6+
*
57
* @template T - The type of elements in the array.
68
* @template U - The type of mapped elements.
79
* @param {T[]} arr - The array to process.

0 commit comments

Comments
 (0)