/
keysIn.ts
73 lines (67 loc) · 1.44 KB
/
keysIn.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
import isArrayLike from "./isArrayLike";
import arrayLikeKeys from "./.internal/arrayLikeKeys";
import isObject from "./isObject";
import isPrototype from "./.internal/isPrototype";
/**
* @ignore
* @private
*/
const hasOwnProperty = Object.prototype.hasOwnProperty;
/**
* @private
* @ignore
* @param object
*/
function nativeKeysIn(object: any): string[] {
const result = [];
if (object != null) {
for (const key in Object(object)) {
result.push(key);
}
}
return result;
}
/**
* @ignore
* @private
* @param object ant
*/
function baseKeysIn(object: any): string[] {
if (!isObject(object)) {
return nativeKeysIn(object);
}
const isProto = isPrototype(object);
const result = [];
for (const key in object) {
if (!(key == "constructor" && (isProto || !hasOwnProperty.call(object, key)))) {
result.push(key);
}
}
return result;
}
/**
* Creates an array of the own and inherited enumerable property names of `object`.
*
*
* @since 5.5.0
* @category Object
* @param object The object to query.
* @returns Returns the array of property names.
* @example
*
* ```js
* function Foo() {
* this.a = 1;
* this.b = 2;
* }
*
* Foo.prototype.c = 3;
*
* keysIn(new Foo);
* // => ['a', 'b', 'c'] (iteration order is not guaranteed)
* ```
*/
export function keysIn(object: any): string[] {
return isArrayLike(object) ? arrayLikeKeys(object, true) : baseKeysIn(object);
}
export default keysIn;