GetOwnPropertyKeys.js 822 B

12345678910111213141516171819202122232425262728293031
  1. 'use strict';
  2. var GetIntrinsic = require('get-intrinsic');
  3. var hasSymbols = require('has-symbols')();
  4. var $TypeError = require('es-errors/type');
  5. var $gOPN = GetIntrinsic('%Object.getOwnPropertyNames%', true);
  6. var $gOPS = hasSymbols && GetIntrinsic('%Object.getOwnPropertySymbols%', true);
  7. var keys = require('object-keys');
  8. var esType = require('./Type');
  9. // https://262.ecma-international.org/6.0/#sec-getownpropertykeys
  10. module.exports = function GetOwnPropertyKeys(O, Type) {
  11. if (esType(O) !== 'Object') {
  12. throw new $TypeError('Assertion failed: Type(O) is not Object');
  13. }
  14. if (Type === 'Symbol') {
  15. return $gOPS ? $gOPS(O) : [];
  16. }
  17. if (Type === 'String') {
  18. if (!$gOPN) {
  19. return keys(O);
  20. }
  21. return $gOPN(O);
  22. }
  23. throw new $TypeError('Assertion failed: `Type` must be `"String"` or `"Symbol"`');
  24. };