keysIn.js 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. import isArguments from '../lang/isArguments';
  2. import isArray from '../lang/isArray';
  3. import isIndex from '../internal/isIndex';
  4. import isLength from '../internal/isLength';
  5. import isObject from '../lang/isObject';
  6. /** Used for native method references. */
  7. var objectProto = Object.prototype;
  8. /** Used to check objects for own properties. */
  9. var hasOwnProperty = objectProto.hasOwnProperty;
  10. /**
  11. * Creates an array of the own and inherited enumerable property names of `object`.
  12. *
  13. * **Note:** Non-object values are coerced to objects.
  14. *
  15. * @static
  16. * @memberOf _
  17. * @category Object
  18. * @param {Object} object The object to query.
  19. * @returns {Array} Returns the array of property names.
  20. * @example
  21. *
  22. * function Foo() {
  23. * this.a = 1;
  24. * this.b = 2;
  25. * }
  26. *
  27. * Foo.prototype.c = 3;
  28. *
  29. * _.keysIn(new Foo);
  30. * // => ['a', 'b', 'c'] (iteration order is not guaranteed)
  31. */
  32. function keysIn(object) {
  33. if (object == null) {
  34. return [];
  35. }
  36. if (!isObject(object)) {
  37. object = Object(object);
  38. }
  39. var length = object.length;
  40. length = (length && isLength(length) &&
  41. (isArray(object) || isArguments(object)) && length) || 0;
  42. var Ctor = object.constructor,
  43. index = -1,
  44. isProto = typeof Ctor == 'function' && Ctor.prototype === object,
  45. result = Array(length),
  46. skipIndexes = length > 0;
  47. while (++index < length) {
  48. result[index] = (index + '');
  49. }
  50. for (var key in object) {
  51. if (!(skipIndexes && isIndex(key, length)) &&
  52. !(key == 'constructor' && (isProto || !hasOwnProperty.call(object, key)))) {
  53. result.push(key);
  54. }
  55. }
  56. return result;
  57. }
  58. export default keysIn;