-
Notifications
You must be signed in to change notification settings - Fork 32
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
[1-team] ✨ june 과제 #43
base: 1-team
Are you sure you want to change the base?
Conversation
} | ||
} | ||
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 comment
The reason will be displayed to describe this comment to others. Learn more.
오 isSorted로 sort할 지 여부를 결정하는군요!
uniqueSet.add(newArr.shift()); | ||
for (const item of newArr) { | ||
const transformed = callback ? callback(item) : item; | ||
const isObject = transformed !== null && typeof transformed === '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.
저도 typeof로 object인지 체크하려 했는데 transformed가 배열일 경우에도 해당 조건에 걸릴 것 같더라구요!
그래서 Array.isArray()
(roy가 알려준 방법을 좀 응용해보았어요...!)로 false값이 나오면 다르게 동작하도록 했습니다!!
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.
@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'라는;;;
추가로, 이런 방법도 있어요.
Object.prototype.toString.call(item)
// 배열: [object Array]
// Map: [object Map]
// RegExp: [object RegExp]
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.
필받아서 만들어봤어요
const getTag = variable => {
const str = Object.prototype.toString.call(variable)
return /^\[object (.{1,})\]$/.exec(str)[1]
}
const isType = (variable, typeName) => getTag(variable) === typeName
isType([], 'Array') // true
isType({}, 'Object') // true
isType(new RegExp(), 'RegExp') // true
isType(new Date(), 'Date') // true
isType(new Map(), 'Map') // true
isType(new String(), 'String') // true
isType(function(){}, 'Function') // true
isType(BigInt(123), 'BigInt') // true
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.... 흡수하겠습니다.
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으로 문자열을 가져와서 판별하다니.... 너무 좋은 아이디어네요!!!! 감사해요 로이
No description provided.