shimKeys.js 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. import isArguments from '../lang/isArguments';
  2. import isArray from '../lang/isArray';
  3. import isIndex from './isIndex';
  4. import isLength from './isLength';
  5. import keysIn from '../object/keysIn';
  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. * A fallback implementation of `Object.keys` which creates an array of the
  12. * own enumerable property names of `object`.
  13. *
  14. * @private
  15. * @param {Object} object The object to query.
  16. * @returns {Array} Returns the array of property names.
  17. */
  18. function shimKeys(object) {
  19. var props = keysIn(object),
  20. propsLength = props.length,
  21. length = propsLength && object.length;
  22. var allowIndexes = !!length && isLength(length) &&
  23. (isArray(object) || isArguments(object));
  24. var index = -1,
  25. result = [];
  26. while (++index < propsLength) {
  27. var key = props[index];
  28. if ((allowIndexes && isIndex(key, length)) || hasOwnProperty.call(object, key)) {
  29. result.push(key);
  30. }
  31. }
  32. return result;
  33. }
  34. export default shimKeys;