-
Notifications
You must be signed in to change notification settings - Fork 32
[1-team] ✨ june 과제 #43
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
june6723
wants to merge
1
commit into
pagers-org:1-team
Choose a base branch
from
june6723:june/lazy-eval
base: 1-team
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,54 @@ | ||
// FIXME: npm test /src/utils/groupBy.test.js | ||
|
||
const { _groupBy } = require('./util'); | ||
|
||
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', () => { | ||
const array = [ | ||
[1, 'a'], | ||
[2, 'a'], | ||
[2, 'b'], | ||
]; | ||
|
||
// 두 번째 인자가 index | ||
const [groupedFirstIndex, groupedSecondIndex] = [ | ||
_groupBy(array, item => item[0]), | ||
_groupBy(array, item => item[1]), | ||
]; | ||
|
||
expect(groupedFirstIndex).toEqual({ | ||
1: [[1, 'a']], | ||
2: [ | ||
[2, 'a'], | ||
[2, 'b'], | ||
], | ||
}); | ||
|
||
expect(groupedSecondIndex).toEqual({ | ||
a: [ | ||
[1, 'a'], | ||
[2, 'a'], | ||
], | ||
b: [[2, 'b']], | ||
}); | ||
}); | ||
|
||
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] }); | ||
}); | ||
}); | ||
|
||
describe('lazy', () => { | ||
// 여기에 Lazy 테스트 코드를 작성해보자!!! | ||
}); | ||
}); |
This file contains hidden or 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,20 @@ | ||
const { getMockData, timer } = require('../lib'); | ||
const { pipe, map, filter, take } = require('./util'); | ||
|
||
const users = getMockData(); | ||
|
||
timer.start(); | ||
const job = pipe( | ||
map(user => user.age), | ||
filter(age => age > 50), | ||
take(2), | ||
); | ||
job(users); | ||
timer.end(); | ||
|
||
timer.start(); | ||
users | ||
.map(user => user.age) | ||
.filter(age => age > 50) | ||
.slice(2); | ||
timer.end(); |
This file contains hidden or 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,47 @@ | ||
// FIXME: npm test /src/utils/unique.test.js | ||
const { unique } = require('./util'); | ||
|
||
describe('unique 테스트', () => { | ||
describe('non-lazy', () => { | ||
it('case: 1, Normal', () => { | ||
const [firstArray, secondArray] = [ | ||
[2, 1, 2], | ||
[1, 2, 1], | ||
]; | ||
const firstUniqueArray = unique(firstArray); | ||
const secondUniqueArray = unique(secondArray); | ||
|
||
expect(firstUniqueArray).toEqual([2, 1]); | ||
expect(secondUniqueArray).toEqual([1, 2]); | ||
}); | ||
|
||
it('case: 2, Advanced', () => { | ||
const [firstArray, secondArray, thirdArray] = [ | ||
[1, 2, 3], | ||
[1, 1, 2, 2, 3], | ||
[1, 2, 3, 3, 3, 3, 3], | ||
]; | ||
const firstUniqueArray = unique(firstArray, undefined, true); | ||
const secondUniqueArray = unique(secondArray, undefined, true); | ||
const thirdUniqueArray = unique(thirdArray, undefined, true); | ||
|
||
expect(firstUniqueArray).toEqual([1, 2, 3]); | ||
expect(secondUniqueArray).toEqual([1, 2, 3]); | ||
expect(thirdUniqueArray).toEqual([1, 2, 3]); | ||
}); | ||
|
||
it('case: 3, Advanced', () => { | ||
const objects = [ | ||
{ x: 1, y: 2 }, | ||
{ x: 2, y: 1 }, | ||
{ x: 1, y: 2 }, | ||
]; | ||
const uniqueObjects = unique(objects); | ||
|
||
expect(uniqueObjects).toEqual([ | ||
{ x: 1, y: 2 }, | ||
{ x: 2, y: 1 }, | ||
]); | ||
}); | ||
}); | ||
}); |
This file contains hidden or 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,85 @@ | ||
const map = callback => { | ||
return function* (iter) { | ||
for (const item of iter) { | ||
yield callback(item); | ||
} | ||
}; | ||
}; | ||
|
||
const filter = callback => { | ||
return function* (iter) { | ||
for (const item of iter) { | ||
if (callback(item)) yield item; | ||
} | ||
}; | ||
}; | ||
|
||
const take = n => { | ||
return function* (iter) { | ||
let i = 0; | ||
for (const item of iter) { | ||
if (i >= n) return; | ||
yield item; | ||
i += 1; | ||
} | ||
}; | ||
}; | ||
|
||
const pipe = | ||
(...fns) => | ||
iter => | ||
fns.reduce((result, fn) => fn(result), iter); | ||
|
||
const _groupBy = (iter, fn) => { | ||
const result = {}; | ||
if (!iter[Symbol.iterator]) { | ||
iter[Symbol.iterator] = function* () { | ||
for (const key of Object.keys(iter)) yield iter[key]; | ||
}; | ||
} | ||
for (const item of iter) { | ||
const key = fn(item); | ||
const values = result[key]; | ||
result[key] = values ? [...values, item] : [item]; | ||
} | ||
return result; | ||
}; | ||
|
||
const shallowEqual = (a, b) => { | ||
for (const key of Object.keys(a)) { | ||
if (a[key] !== b[key]) return false; | ||
} | ||
return true; | ||
}; | ||
|
||
const unique = (arr, callback, isSorted) => { | ||
const uniqueSet = new Set([]); | ||
const newArr = [...arr]; | ||
uniqueSet.add(newArr.shift()); | ||
for (const item of newArr) { | ||
const transformed = callback ? callback(item) : item; | ||
const isObject = transformed !== null && typeof transformed === 'object'; | ||
if (isObject) { | ||
let included = false; | ||
for (const value of uniqueSet.values()) { | ||
if (shallowEqual(value, transformed)) included = true; | ||
} | ||
if (!included) uniqueSet.add(transformed); | ||
} else { | ||
if (!uniqueSet.has(transformed)) uniqueSet.add(transformed); | ||
} | ||
} | ||
const result = Array.from(uniqueSet); | ||
if (isSorted === false) result.sort((a, b) => a - b); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 오 isSorted로 sort할 지 여부를 결정하는군요! |
||
return result; | ||
}; | ||
|
||
module.exports = { | ||
map, | ||
filter, | ||
take, | ||
pipe, | ||
_groupBy, | ||
unique, | ||
shallowEqual, | ||
}; |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
저도 typeof로 object인지 체크하려 했는데 transformed가 배열일 경우에도 해당 조건에 걸릴 것 같더라구요!
그래서
Array.isArray()
(roy가 알려준 방법을 좀 응용해보았어요...!)로 false값이 나오면 다르게 동작하도록 했습니다!!object를 판단하는 더 좋은 방법이 있다면 공유 부탁드려요 ㅎㅎ
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@areumsheep 여러가지가 있는데요
item instanceof Object
: 마찬가지로 배열도 객체기 때문에 true가 떨어져서 실패;item.constructor === Object
: 요건 성공! 단 누군가 constructor를 바꿔치기한 경우라면 실패할수도..Object.getPrototypeOf(item) === Object.prototype
: 2번과 마찬가지.반대로 item이 다음 방식으로 만들어진 경우라면
const item = Object.create(null)
오히려 1번으로만 그나마 판별이 가능하고, 2, 3번으로는 불가능해집니다;;
아무튼 어떤 방법을 쓰더라도 중간에 누군가의 개입이 있다는 전제하에서는
객체인지를 정확히 알아내는 묘수는 없습니다.
다만 '변경을 시도하지 않은 순수 데이터'임이 확실하다면, 2번 3번 정도로 충분할 거예요.
다행인 점은, typeof가 object를 반환하는 경우는 객체, 배열, null 정도입니다.
따라서 세가지를 모두 비교하면 그나마 정확한 판별이 가능해져요.
typeof item === 'object' && item !== null && !Array.isArray(item)
근데 얘도 문제인게,
new Regexp()
,new Number()
,new String()
등의 기본 생성자함수로 생성한 녀석들은여전히 type이 'object'라는;;;
추가로, 이런 방법도 있어요.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
필받아서 만들어봤어요
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
아주 관대한 js....
ㅋㅋㅋㅋ 두 분다 리뷰 넘나 감사합니다
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
와우... 양질의 pr.... 흡수하겠습니다.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
우와우,,,, toString으로 문자열을 가져와서 판별하다니.... 너무 좋은 아이디어네요!!!! 감사해요 로이