-
Notifications
You must be signed in to change notification settings - Fork 5
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
keys 유틸을 추가합니다. #5
Merged
Merged
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 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,9 @@ | ||
// only for vite, tsup | ||
// remember, this is not barrel file. | ||
const moduleMap = { | ||
isEmpty: './src/isEmpty.ts', | ||
size: './src/size.ts', | ||
keys: './src/keys.ts', | ||
} as const | ||
|
||
export default moduleMap |
This file contains 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
This file contains 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
This file contains 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,89 @@ | ||
import _keys from 'lodash/keys' | ||
import {bench, describe} from 'vitest' | ||
|
||
import {keys} from './keys' | ||
|
||
const testCases = [ | ||
// null & undefined | ||
null, | ||
undefined, | ||
|
||
// Arrays | ||
[], | ||
[1, 2, 3], | ||
Array(1000).fill(1), | ||
[...Array(1000).keys()], | ||
|
||
// Objects | ||
{}, | ||
{a: 1, b: 2}, | ||
Object.create(null), | ||
Object.create( | ||
{}, | ||
{ | ||
a: {value: 1, enumerable: true}, | ||
b: {value: 2, enumerable: false}, | ||
}, | ||
), | ||
|
||
// Array-like objects | ||
{length: 2, 0: 'a', 1: 'b'}, | ||
// eslint-disable-next-line @typescript-eslint/ban-ts-comment | ||
// @ts-ignore | ||
{length: 1000, ...Array(1000).fill(1)}, | ||
|
||
// String objects | ||
Object(''), | ||
Object('hello'), | ||
|
||
// Other cases | ||
new Date(), | ||
new Map([ | ||
['a', 1], | ||
['b', 2], | ||
]), | ||
new Set([1, 2, 3]), | ||
|
||
// Objects with prototype | ||
Object.create( | ||
{inherited: true}, | ||
{ | ||
own: {value: true, enumerable: true}, | ||
}, | ||
), | ||
|
||
// Large object | ||
{ | ||
...Object.fromEntries( | ||
Array(1000) | ||
.fill(0) | ||
.map((_, i) => [`key${i}`, i]), | ||
), | ||
}, | ||
|
||
// Object with symbols | ||
{[Symbol('test')]: 'value', regular: 'key'}, | ||
|
||
// Sparse array | ||
Object.assign(Array(1000), {1: 'a', 500: 'b', 999: 'c'}), | ||
] | ||
|
||
const ITERATIONS = 1000 | ||
|
||
describe('keys performance', () => { | ||
bench('hidash', () => { | ||
for (let i = 0; i < ITERATIONS; i++) { | ||
testCases.forEach((testCase) => { | ||
keys(testCase) | ||
}) | ||
} | ||
}) | ||
|
||
bench('lodash', () => { | ||
for (let i = 0; i < ITERATIONS; i++) { | ||
testCases.forEach((testCase) => { | ||
_keys(testCase) | ||
}) | ||
} | ||
}) | ||
}) |
This file contains 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,133 @@ | ||
import _keys from 'lodash/keys' | ||
import {describe, test, expect} from 'vitest' | ||
|
||
import {keys} from './keys' | ||
|
||
describe('keys function', () => { | ||
test('returns empty array for null and undefined', () => { | ||
expect(keys(null)).toEqual([]) | ||
expect(keys(undefined)).toEqual([]) | ||
}) | ||
|
||
test('returns array indices for array', () => { | ||
expect(keys([])).toEqual([]) | ||
expect(keys(['a', 'b', 'c'])).toEqual(['0', '1', '2']) | ||
expect(keys(new Array(3))).toEqual(['0', '1', '2']) | ||
}) | ||
|
||
test('returns own enumerable keys for objects', () => { | ||
expect(keys({})).toEqual([]) | ||
expect(keys({a: 1, b: 2})).toEqual(['a', 'b']) | ||
expect(keys(Object.create(null))).toEqual([]) | ||
}) | ||
|
||
test('handles array-like objects correctly', () => { | ||
const arrayLike = {0: 'a', 1: 'b', 2: 'c', length: 3} | ||
expect(keys(arrayLike)).toEqual(['0', '1', '2']) | ||
|
||
const htmlCollection = { | ||
0: 'div', | ||
1: 'span', | ||
length: 2, | ||
item: (_: number) => null, | ||
} | ||
expect(keys(htmlCollection)).toEqual(['0', '1', 'item']) | ||
}) | ||
|
||
test('returns empty array for primitive values', () => { | ||
expect(keys(42)).toEqual([]) | ||
expect(keys(true)).toEqual([]) | ||
expect(keys(Symbol('test'))).toEqual([]) | ||
}) | ||
|
||
test('handles string objects correctly', () => { | ||
const strObj = Object('hello') | ||
expect(keys(strObj)).toEqual(['0', '1', '2', '3', '4']) | ||
}) | ||
|
||
test('handles prototype properties correctly', () => { | ||
function TestClass() { | ||
this.a = 1 | ||
this.b = 2 | ||
} | ||
TestClass.prototype.c = 3 | ||
|
||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
const obj = new (TestClass as any)() | ||
expect(keys(obj)).toEqual(['a', 'b']) | ||
}) | ||
|
||
test('handles non-enumerable properties correctly', () => { | ||
const obj = Object.create( | ||
{}, | ||
{ | ||
a: {value: 1, enumerable: true}, | ||
b: {value: 2, enumerable: false}, | ||
}, | ||
) | ||
expect(keys(obj)).toEqual(['a']) | ||
}) | ||
|
||
test('handles sparse arrays correctly', () => { | ||
const sparseArray = Array(3) | ||
sparseArray[1] = 'b' | ||
expect(keys(sparseArray)).toEqual(['0', '1', '2']) | ||
}) | ||
|
||
test('handles array with additional properties', () => { | ||
const arr = ['a', 'b'] | ||
|
||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
;(arr as any).prop = 'value' | ||
expect(keys(arr)).toEqual(['0', '1', 'prop']) | ||
}) | ||
|
||
test('handles large objects efficiently', () => { | ||
const largeObj: Record<string, number> = {} | ||
for (let i = 0; i < 10000; i++) { | ||
largeObj[`key${i}`] = i | ||
} | ||
expect(keys(largeObj).length).toBe(10000) | ||
expect(keys(largeObj)[0]).toBe('key0') | ||
expect(keys(largeObj)[9999]).toBe('key9999') | ||
}) | ||
|
||
test('handles large arrays efficiently', () => { | ||
const largeArray = new Array(10000).fill(0) | ||
const result = keys(largeArray) | ||
expect(result.length).toBe(10000) | ||
expect(result[0]).toBe('0') | ||
expect(result[9999]).toBe('9999') | ||
}) | ||
|
||
test('handles inherited enumerable properties correctly', () => { | ||
const proto = {inherited: true} | ||
const obj = Object.create(proto) | ||
obj.own = true | ||
expect(keys(obj)).toEqual(['own']) | ||
}) | ||
|
||
test('handles Object.prototype modifications safely', () => { | ||
const originalToString = Object.prototype.toString | ||
try { | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
;(Object.prototype as any).malicious = 'bad' | ||
const obj = {a: 1, b: 2} | ||
expect(keys(obj)).toEqual(['a', 'b']) | ||
} finally { | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
delete (Object.prototype as any).malicious | ||
// eslint-disable-next-line no-extend-native | ||
Object.prototype.toString = originalToString | ||
} | ||
}) | ||
|
||
test('handles objects with symbols correctly', () => { | ||
const symbol = Symbol('test') | ||
const obj = { | ||
[symbol]: 'symbol', | ||
regular: 'value', | ||
} | ||
expect(keys(obj)).toEqual(['regular']) | ||
}) | ||
}) |
This file contains 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,89 @@ | ||
import {isArrayLike} from './internal' | ||
|
||
const nativeKeys = Object.keys | ||
const isArray = Array.isArray | ||
|
||
export function keys(object: unknown): string[] { | ||
// null/undefined 빠른 처리 | ||
if (object == null) { | ||
return [] | ||
} | ||
|
||
const obj = Object(object) | ||
|
||
// 순수 배열과 추가 속성이 있는 배열 처리 | ||
if (isArray(obj)) { | ||
const plainKeys = nativeKeys(obj) | ||
const len = obj.length | ||
|
||
// 배열 길이와 키 개수가 같으면 추가 속성이 없는 경우 | ||
if (plainKeys.length === len) { | ||
const result = new Array(len) | ||
let idx = 0 | ||
while (idx < len) { | ||
result[idx] = String(idx++) | ||
} | ||
return result | ||
} | ||
|
||
const result = new Array(plainKeys.length) | ||
let idx = 0 | ||
|
||
while (idx < len) { | ||
result[idx] = String(idx++) | ||
} | ||
|
||
// 추가 속성 채우기 | ||
for (const key of plainKeys) { | ||
const keyNum = +key | ||
if (isNaN(keyNum) || keyNum >= len) { | ||
result[idx++] = key | ||
} | ||
} | ||
|
||
return result | ||
} | ||
|
||
if (isArrayLike(obj)) { | ||
const plainKeys = nativeKeys(obj) | ||
const len = obj.length | ||
|
||
if (plainKeys.length === 1 && plainKeys[0] === 'length') { | ||
const result = new Array(len) | ||
let idx = 0 | ||
while (idx < len) { | ||
result[idx] = String(idx++) | ||
} | ||
return result | ||
} | ||
|
||
let extraKeysCount = 0 | ||
|
||
for (const key of plainKeys) { | ||
const keyNum = +key | ||
if (key !== 'length' && (isNaN(keyNum) || keyNum >= len)) { | ||
extraKeysCount++ | ||
} | ||
} | ||
|
||
const result = new Array(len + extraKeysCount) | ||
let idx = 0 | ||
|
||
while (idx < len) { | ||
result[idx] = String(idx++) | ||
} | ||
|
||
if (extraKeysCount > 0) { | ||
for (const key of plainKeys) { | ||
const keyNum = +key | ||
if (key !== 'length' && (isNaN(keyNum) || keyNum >= len)) { | ||
result[idx++] = key | ||
} | ||
} | ||
} | ||
|
||
return result | ||
} | ||
|
||
return nativeKeys(obj) | ||
} |
This file contains 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
This file contains 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
This file contains 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
Oops, something went wrong.
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.
근데 이거 vite plugin dts 안쓰신 이유 이쓸까여?
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에서 바꿀게요