-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathkeys.ts
48 lines (41 loc) · 1.09 KB
/
keys.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
import {isArrayLike} from './internal/array'
const nativeKeys = Object.keys
const isArray = Array.isArray
/**
* @description https://unpkg.com/browse/lodash.keys@4.2.0/index.js
*/
export function keys(object: unknown): string[] {
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)) {
return nativeKeys(obj)
}
return nativeKeys(obj)
}
export default keys