UNPKG

1.37 kBJavaScriptView Raw
1import baseHas from './_baseHas';
2import baseKeys from './_baseKeys';
3import indexKeys from './_indexKeys';
4import isArrayLike from './isArrayLike';
5import isIndex from './_isIndex';
6import isPrototype from './_isPrototype';
7
8/**
9 * Creates an array of the own enumerable property names of `object`.
10 *
11 * **Note:** Non-object values are coerced to objects. See the
12 * [ES spec](http://ecma-international.org/ecma-262/6.0/#sec-object.keys)
13 * for more details.
14 *
15 * @static
16 * @since 0.1.0
17 * @memberOf _
18 * @category Object
19 * @param {Object} object The object to query.
20 * @returns {Array} Returns the array of property names.
21 * @example
22 *
23 * function Foo() {
24 * this.a = 1;
25 * this.b = 2;
26 * }
27 *
28 * Foo.prototype.c = 3;
29 *
30 * _.keys(new Foo);
31 * // => ['a', 'b'] (iteration order is not guaranteed)
32 *
33 * _.keys('hi');
34 * // => ['0', '1']
35 */
36function keys(object) {
37 var isProto = isPrototype(object);
38 if (!(isProto || isArrayLike(object))) {
39 return baseKeys(object);
40 }
41 var indexes = indexKeys(object),
42 skipIndexes = !!indexes,
43 result = indexes || [],
44 length = result.length;
45
46 for (var key in object) {
47 if (baseHas(object, key) &&
48 !(skipIndexes && (key == 'length' || isIndex(key, length))) &&
49 !(isProto && key == 'constructor')) {
50 result.push(key);
51 }
52 }
53 return result;
54}
55
56export default keys;