implementation.js 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. 'use strict';
  2. var ES = require('es-abstract/es7');
  3. var defineProperty = Object.defineProperty;
  4. var getDescriptor = Object.getOwnPropertyDescriptor;
  5. var getOwnNames = Object.getOwnPropertyNames;
  6. var getSymbols = Object.getOwnPropertySymbols;
  7. var concat = Function.call.bind(Array.prototype.concat);
  8. var reduce = Function.call.bind(Array.prototype.reduce);
  9. var getAll = getSymbols ? function (obj) {
  10. return concat(getOwnNames(obj), getSymbols(obj));
  11. } : getOwnNames;
  12. var isES5 = ES.IsCallable(getDescriptor) && ES.IsCallable(getOwnNames);
  13. var safePut = function put(obj, prop, val) { // eslint-disable-line max-params
  14. if (defineProperty && prop in obj) {
  15. defineProperty(obj, prop, {
  16. configurable: true,
  17. enumerable: true,
  18. value: val,
  19. writable: true
  20. });
  21. } else {
  22. obj[prop] = val;
  23. }
  24. };
  25. module.exports = function getOwnPropertyDescriptors(value) {
  26. ES.RequireObjectCoercible(value);
  27. if (!isES5) {
  28. throw new TypeError('getOwnPropertyDescriptors requires Object.getOwnPropertyDescriptor');
  29. }
  30. var O = ES.ToObject(value);
  31. return reduce(getAll(O), function (acc, key) {
  32. var descriptor = getDescriptor(O, key);
  33. if (typeof descriptor !== 'undefined') {
  34. safePut(acc, key, descriptor);
  35. }
  36. return acc;
  37. }, {});
  38. };