EnumerableOwnProperties.js 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. 'use strict';
  2. var GetIntrinsic = require('get-intrinsic');
  3. var $TypeError = GetIntrinsic('%TypeError%');
  4. var objectKeys = require('object-keys');
  5. var callBound = require('call-bind/callBound');
  6. var callBind = require('call-bind');
  7. var $isEnumerable = callBound('Object.prototype.propertyIsEnumerable');
  8. var $pushApply = callBind.apply(GetIntrinsic('%Array.prototype.push%'));
  9. var forEach = require('../helpers/forEach');
  10. var Type = require('./Type');
  11. // https://262.ecma-international.org/8.0/#sec-enumerableownproperties
  12. module.exports = function EnumerableOwnProperties(O, kind) {
  13. if (Type(O) !== 'Object') {
  14. throw new $TypeError('Assertion failed: Type(O) is not Object');
  15. }
  16. var keys = objectKeys(O);
  17. if (kind === 'key') {
  18. return keys;
  19. }
  20. if (kind === 'value' || kind === 'key+value') {
  21. var results = [];
  22. forEach(keys, function (key) {
  23. if ($isEnumerable(O, key)) {
  24. $pushApply(results, [
  25. kind === 'value' ? O[key] : [key, O[key]]
  26. ]);
  27. }
  28. });
  29. return results;
  30. }
  31. throw new $TypeError('Assertion failed: "kind" is not "key", "value", or "key+value": ' + kind);
  32. };