index.js 773 B

123456789101112131415161718192021222324252627
  1. 'use strict';
  2. var toStr = Object.prototype.toString;
  3. var hasSymbols = typeof Symbol === 'function' && typeof Symbol() === 'symbol';
  4. if (hasSymbols) {
  5. var symToStr = Symbol.prototype.toString;
  6. var symStringRegex = /^Symbol\(.*\)$/;
  7. var isSymbolObject = function isSymbolObject(value) {
  8. if (typeof value.valueOf() !== 'symbol') { return false; }
  9. return symStringRegex.test(symToStr.call(value));
  10. };
  11. module.exports = function isSymbol(value) {
  12. if (typeof value === 'symbol') { return true; }
  13. if (toStr.call(value) !== '[object Symbol]') { return false; }
  14. try {
  15. return isSymbolObject(value);
  16. } catch (e) {
  17. return false;
  18. }
  19. };
  20. } else {
  21. module.exports = function isSymbol(value) {
  22. // this environment does not support Symbols.
  23. return false;
  24. };
  25. }