-
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
'use strict'; | ||
|
||
var toStr = Object.prototype.toString; | ||
var hasSymbols = typeof Symbol === 'function' && typeof Symbol() === 'symbol'; | ||
|
||
if (hasSymbols) { | ||
var symToStr = Symbol.prototype.toString; | ||
var symStringRegex = /^Symbol\(.*\)$/; | ||
var isSymbolObject = function isSymbolObject(value) { | ||
if (typeof value.valueOf() !== 'symbol') { return false; } | ||
This comment has been minimized.
Sorry, something went wrong. |
||
return symStringRegex.test(symToStr.call(value)); | ||
This comment has been minimized.
Sorry, something went wrong.
ljharb
Author
Member
|
||
}; | ||
module.exports = function isSymbol(value) { | ||
if (typeof value === 'symbol') { return true; } | ||
This comment has been minimized.
Sorry, something went wrong.
ljharb
Author
Member
|
||
if (toStr.call(value) !== '[object Symbol]') { return false; } | ||
This comment has been minimized.
Sorry, something went wrong.
ljharb
Author
Member
|
||
try { | ||
return isSymbolObject(value); | ||
This comment has been minimized.
Sorry, something went wrong.
ljharb
Author
Member
|
||
} catch (e) { | ||
return false; | ||
} | ||
}; | ||
This comment has been minimized.
Sorry, something went wrong.
jdalton
|
||
} else { | ||
module.exports = function isSymbol(value) { | ||
// this environment does not support Symbols. | ||
return false; | ||
}; | ||
} |
Symbol#valueOf
is weird in node 0.11, and 0.12 possibly, and thus in some v8's. This certainly isn't robust against someone passing a Symbol object with a fakedvalueOf
, but it's a fast negative bailout for someone who's mutated a Symbol object to have a non-symbol-producingvalueOf
. Essentially, in spec text, this is a "if Type(ToPrimitive(value)) is Symbol" check, but a "true" doesn't mean it's not a normal object with a Symbol-producingvalueOf
.