No Description

_collectNonEnumProps.js 1.3KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. import { nonEnumerableProps, ObjProto } from './_setup.js';
  2. import isFunction from './isFunction.js';
  3. import has from './_has.js';
  4. // Internal helper to create a simple lookup structure.
  5. // `collectNonEnumProps` used to depend on `_.contains`, but this led to
  6. // circular imports. `emulatedSet` is a one-off solution that only works for
  7. // arrays of strings.
  8. function emulatedSet(keys) {
  9. var hash = {};
  10. for (var l = keys.length, i = 0; i < l; ++i) hash[keys[i]] = true;
  11. return {
  12. contains: function(key) { return hash[key]; },
  13. push: function(key) {
  14. hash[key] = true;
  15. return keys.push(key);
  16. }
  17. };
  18. }
  19. // Internal helper. Checks `keys` for the presence of keys in IE < 9 that won't
  20. // be iterated by `for key in ...` and thus missed. Extends `keys` in place if
  21. // needed.
  22. export default function collectNonEnumProps(obj, keys) {
  23. keys = emulatedSet(keys);
  24. var nonEnumIdx = nonEnumerableProps.length;
  25. var constructor = obj.constructor;
  26. var proto = isFunction(constructor) && constructor.prototype || ObjProto;
  27. // Constructor is a special case.
  28. var prop = 'constructor';
  29. if (has(obj, prop) && !keys.contains(prop)) keys.push(prop);
  30. while (nonEnumIdx--) {
  31. prop = nonEnumerableProps[nonEnumIdx];
  32. if (prop in obj && obj[prop] !== proto[prop] && !keys.contains(prop)) {
  33. keys.push(prop);
  34. }
  35. }
  36. }