prop-types(symbols): add a helper is-symbol function
Raito Bezarius committed
Apr 2, 2016 at 10:10 UTC
13d9720804a828666d57994d5a40dc5e66ec12d0
1 file changed
+24
-4
src/isomorphic/classic/types/ReactPropTypes.js
+24
-4
@@ -407,6 +407,29 @@ function isNode(propValue) {
407
}
408
}
409
410
+function isSymbol(propType, propValue) {
411
+ if (propType === 'symbol') {
412
+ return true; // This is a native Symbol.
413
+ }
414
+
415
+ if (typeof Symbol === 'undefined') {
416
+ // No Symbol is available in the global namespace.
417
+ // We need to check if it has some spec-defined method (duck typing).
418
+
419
+ if (propValue['@@toStringTag'] === 'Symbol') {
420
+ return true;
421
+ }
422
+
423
+ if (propValue.__key__ && propValue.prototype.__key__ === undefined) {
424
+ return propValue.toString() === 'Symbol'
425
+ }
426
+ } else {
427
+ return propValue instanceof Symbol; // This is a polyfilled Symbol.
428
+ }
429
+
430
+ return false;
431
+}
432
+
433
// Equivalent of `typeof` but with special handling for array and regexp.
434
function getPropType(propValue) {
435
var propType = typeof propValue;
@@ -419,10 +442,7 @@ function getPropType(propValue) {
442
// passes PropTypes.object.
443
return 'object';
444
}
422
- if (propType === 'function' && typeof Symbol === 'function' &&
423
- propValue instanceof Symbol) {
424
- // ES6 polyfills will return 'function' rather than 'symbol' for typeof a
425
- // Symbol.
445
+ if (isSymbol(propType, propValue)) {
446
return 'symbol';
447
}
448
return propType;