Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 26 additions & 16 deletions src/utils/groupBy.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,46 +2,56 @@

// 어떤 것을 해볼까요?
const groupBy = (arr, callback) => {
return arr;
return arr.reduce((acc, val) => {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

reduce도 for..of 처럼 iterator로 돌아가나요?!

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] });
Expand Down
16 changes: 10 additions & 6 deletions src/utils/unique.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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],
Expand All @@ -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],
Expand All @@ -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 },
Expand Down