@@ -2,12 +2,12 @@ import { describe, expect, it } from 'vitest';
22import { uniqBy } from './uniqBy' ;
33
44describe ( 'uniqBy' , ( ) => {
5- it ( 'should work with a ` mapper` ' , ( ) => {
5+ it ( 'should work with a selector function ( mapper) ' , ( ) => {
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 } ) ;
99
10- it ( 'should keep the first occurrence when duplicates are found' , ( ) => {
10+ it ( 'should keep the first occurrence when duplicates are found (selector function) ' , ( ) => {
1111 const items = [
1212 { id : 1 , value : 'apple' } ,
1313 { id : 2 , value : 'banana' } ,
@@ -24,4 +24,42 @@ describe('uniqBy', () => {
2424 { id : 3 , value : 'cherry' } ,
2525 ] ) ;
2626 } ) ;
27+ it ( 'should work with a property key selector' , ( ) => {
28+ const items = [
29+ { id : 1 , value : 'jinho' } ,
30+ { id : 2 , value : 'dohee' } ,
31+ { id : 1 , value : 'jihun' } ,
32+ { id : 3 , value : 'taeho' } ,
33+ { id : 2 , value : 'eunji' } ,
34+ ] ;
35+
36+ const result = uniqBy ( items , 'id' ) ;
37+
38+ expect ( result ) . toEqual ( [
39+ { id : 1 , value : 'jinho' } ,
40+ { id : 2 , value : 'dohee' } ,
41+ { id : 3 , value : 'taeho' } ,
42+ ] ) ;
43+ } ) ;
44+
45+ it ( 'should work with a property key selector for string fields' , ( ) => {
46+ const items = [
47+ { category : 'fruit' , name : 'apple' } ,
48+ { category : 'fruit' , name : 'banana' } ,
49+ { category : 'vegetable' , name : 'carrot' } ,
50+ { category : 'vegetable' , name : 'onion' } ,
51+ ] ;
52+
53+ const result = uniqBy ( items , 'category' ) ;
54+
55+ expect ( result ) . toEqual ( [
56+ { category : 'fruit' , name : 'apple' } ,
57+ { category : 'vegetable' , name : 'carrot' } ,
58+ ] ) ;
59+ } ) ;
60+
61+ it ( 'should return an empty array when input is empty' , ( ) => {
62+ expect ( uniqBy ( [ ] , Math . floor ) ) . toEqual ( [ ] ) ;
63+ expect ( uniqBy ( [ ] as Array < { id : number } > , 'id' ) ) . toEqual ( [ ] ) ;
64+ } ) ;
2765} ) ;
0 commit comments