diff --git a/src/utils/groupBy.test.js b/src/utils/groupBy.test.js index eddd03a..8b055d9 100644 --- a/src/utils/groupBy.test.js +++ b/src/utils/groupBy.test.js @@ -2,46 +2,56 @@ // 어떤 것을 해볼까요? const groupBy = (arr, callback) => { - return arr; + return arr.reduce((acc, val) => { + const key = callback(val); + + if (acc[key] === undefined) acc[key] = []; + + acc[key].push(val); + return acc; + }, {}); }; -describe('groupBy 테스트', () => { - describe('non-lazy', () => { - it('case: 1, Normal', () => { +describe("groupBy 테스트", () => { + describe("non-lazy", () => { + it("case: 1, Normal", () => { const array = [6.1, 4.2, 6.3]; const grouped = groupBy(array, Math.floor); expect(grouped).toEqual({ 4: [4.2], 6: [6.1, 6.3] }); }); - it('case: 2, Advanced', () => { + it("case: 2, Advanced", () => { const array = [ - [1, 'a'], - [2, 'a'], - [2, 'b'], + [1, "a"], + [2, "a"], + [2, "b"], ]; // 두 번째 인자가 index - const [groupedFirstIndex, groupedSecondIndex] = [groupBy(array, 0), groupBy(array, 1)]; + const [groupedFirstIndex, groupedSecondIndex] = [ + groupBy(array, 0), + groupBy(array, 1), + ]; expect(groupedFirstIndex).toEqual({ - 1: [[1, 'a']], + 1: [[1, "a"]], 2: [ - [2, 'a'], - [2, 'b'], + [2, "a"], + [2, "b"], ], }); expect(groupedSecondIndex).toEqual({ a: [ - [1, 'a'], - [2, 'a'], + [1, "a"], + [2, "a"], ], - b: [[2, 'b']], + b: [[2, "b"]], }); }); - it('case: 3, Advanced', () => { + it("case: 3, Advanced", () => { const grouped = groupBy({ a: 6.1, b: 4.2, c: 6.3 }, Math.floor); expect(grouped).toEqual({ 4: [4.2], 6: [6.1, 6.3] }); diff --git a/src/utils/unique.test.js b/src/utils/unique.test.js index b5f3a92..5d793d4 100644 --- a/src/utils/unique.test.js +++ b/src/utils/unique.test.js @@ -2,12 +2,16 @@ // 어떤 것을 해볼까요? const unique = (arr, callback, isSorted) => { - return arr; + const result = arr.reduce((acc, v) => { + return acc.includes(v) ? acc : [...acc, v]; + }, []); + + return result; }; -describe('unique 테스트', () => { - describe('non-lazy', () => { - it('case: 1, Normal', () => { +describe("unique 테스트", () => { + describe("non-lazy", () => { + it("case: 1, Normal", () => { const [firstArray, secondArray] = [ [2, 1, 2], [1, 2, 1], @@ -19,7 +23,7 @@ describe('unique 테스트', () => { expect(secondUniqueArray).toEqual([1, 2]); }); - it('case: 2, Advanced', () => { + it("case: 2, Advanced", () => { const [firstArray, secondArray, thirdArray] = [ [1, 2, 3], [1, 1, 2, 2, 3], @@ -34,7 +38,7 @@ describe('unique 테스트', () => { expect(thirdUniqueArray).toEqual([1, 2, 3]); }); - it('case: 3, Advanced', () => { + it("case: 3, Advanced", () => { const objects = [ { x: 1, y: 2 }, { x: 2, y: 1 },