blob: 34841e01199eb2cc5e607665b22679da2eeb6a43 [file] [log] [blame]
Yang Guo4fd355c2019-09-19 08:59:031'use strict';
2
3var keys = require('object-keys');
4
5module.exports = function hasSymbols() {
6 if (typeof Symbol !== 'function' || typeof Object.getOwnPropertySymbols !== 'function') { return false; }
7 if (typeof Symbol.iterator === 'symbol') { return true; }
8
9 var obj = {};
10 var sym = Symbol('test');
11 var symObj = Object(sym);
12 if (typeof sym === 'string') { return false; }
13
14 if (Object.prototype.toString.call(sym) !== '[object Symbol]') { return false; }
15 if (Object.prototype.toString.call(symObj) !== '[object Symbol]') { return false; }
16
Tim van der Lippe16aca392020-11-13 11:37:1317 /*
18 * temp disabled per https://github.com/ljharb/object.assign/issues/17
19 * if (sym instanceof Symbol) { return false; }
20 * temp disabled per https://github.com/WebReflection/get-own-property-symbols/issues/4
21 * if (!(symObj instanceof Symbol)) { return false; }
22 */
Yang Guo4fd355c2019-09-19 08:59:0323
24 var symVal = 42;
25 obj[sym] = symVal;
Tim van der Lippe16aca392020-11-13 11:37:1326 for (sym in obj) { return false; } // eslint-disable-line no-unreachable-loop
Yang Guo4fd355c2019-09-19 08:59:0327 if (keys(obj).length !== 0) { return false; }
28 if (typeof Object.keys === 'function' && Object.keys(obj).length !== 0) { return false; }
29
30 if (typeof Object.getOwnPropertyNames === 'function' && Object.getOwnPropertyNames(obj).length !== 0) { return false; }
31
32 var syms = Object.getOwnPropertySymbols(obj);
33 if (syms.length !== 1 || syms[0] !== sym) { return false; }
34
35 if (!Object.prototype.propertyIsEnumerable.call(obj, sym)) { return false; }
36
37 if (typeof Object.getOwnPropertyDescriptor === 'function') {
38 var descriptor = Object.getOwnPropertyDescriptor(obj, sym);
39 if (descriptor.value !== symVal || descriptor.enumerable !== true) { return false; }
40 }
41
42 return true;
43};