hasSymbols.js 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. 'use strict';
  2. var keys = require('object-keys');
  3. module.exports = function hasSymbols() {
  4. if (typeof Symbol !== 'function' || typeof Object.getOwnPropertySymbols !== 'function') { return false; }
  5. if (typeof Symbol.iterator === 'symbol') { return true; }
  6. var obj = {};
  7. var sym = Symbol('test');
  8. var symObj = Object(sym);
  9. if (typeof sym === 'string') { return false; }
  10. if (Object.prototype.toString.call(sym) !== '[object Symbol]') { return false; }
  11. if (Object.prototype.toString.call(symObj) !== '[object Symbol]') { return false; }
  12. /*
  13. * temp disabled per https://github.com/ljharb/object.assign/issues/17
  14. * if (sym instanceof Symbol) { return false; }
  15. * temp disabled per https://github.com/WebReflection/get-own-property-symbols/issues/4
  16. * if (!(symObj instanceof Symbol)) { return false; }
  17. */
  18. var symVal = 42;
  19. obj[sym] = symVal;
  20. for (sym in obj) { return false; } // eslint-disable-line no-unreachable-loop
  21. if (keys(obj).length !== 0) { return false; }
  22. if (typeof Object.keys === 'function' && Object.keys(obj).length !== 0) { return false; }
  23. if (typeof Object.getOwnPropertyNames === 'function' && Object.getOwnPropertyNames(obj).length !== 0) { return false; }
  24. var syms = Object.getOwnPropertySymbols(obj);
  25. if (syms.length !== 1 || syms[0] !== sym) { return false; }
  26. if (!Object.prototype.propertyIsEnumerable.call(obj, sym)) { return false; }
  27. if (typeof Object.getOwnPropertyDescriptor === 'function') {
  28. var descriptor = Object.getOwnPropertyDescriptor(obj, sym);
  29. if (descriptor.value !== symVal || descriptor.enumerable !== true) { return false; }
  30. }
  31. return true;
  32. };