|
| 1 | +/** |
| 2 | + * Copyright (c) 2015-present, Facebook, Inc. |
| 3 | + * |
| 4 | + * This source code is licensed under the MIT license found in the |
| 5 | + * LICENSE file in the root directory of this source tree. |
| 6 | + * |
| 7 | + * @flow strict |
| 8 | + */ |
| 9 | + |
| 10 | +/** |
| 11 | + * A function that can either simply determine if `Symbol`s are allowed as |
| 12 | + * well as, optionally, determine whether or not a property of symbol |
| 13 | + * exists. |
| 14 | + * |
| 15 | + * If the name of a specific `Symbol` property is supplied, the resulting |
| 16 | + * value will only be true if property is mapped to an instance of `Symbol` |
| 17 | + * such as `.toStringTag`, `.iterator`, `.species`, `.isConcatSpreadable`, |
| 18 | + * etc... |
| 19 | + * |
| 20 | + * @method hasSymbolSupport |
| 21 | + * |
| 22 | + * @param {string} specificSymbol an optional name of a property on the |
| 23 | + * `Symbol` class itself that statically refers to a predefined `Symbol`. If |
| 24 | + * properly specified and the value is set, then true will be returned. |
| 25 | + * @return {bool} true if `Symbol` is both defined and a function. Optionally |
| 26 | + * true if the `Symbol` class is true, a predefined symbol name such as |
| 27 | + * `toStringTag` is set on the `Symbol` class and it maps to an instance of |
| 28 | + * `Symbol`. False in all other cases |
| 29 | + */ |
| 30 | +export function hasSymbolSupport(specificSymbol?: string): boolean { |
| 31 | + const hasSymbols: boolean = typeof Symbol === 'function'; |
| 32 | + |
| 33 | + if (!hasSymbols) { |
| 34 | + return false; |
| 35 | + } |
| 36 | + |
| 37 | + if (specificSymbol) { |
| 38 | + // NOTE: The capture of type and string comparison over a few lines is |
| 39 | + // necessary to appease the lint and flowtype gods. |
| 40 | + // |
| 41 | + // ((typeof Symbol[specificSymbol]): string) !== 'symbol' makes lint angry |
| 42 | + // and typeof Symbol[specificSymbol] !== 'symbol' makes flow angry |
| 43 | + // |
| 44 | + // The former thinks everything is too verbose the later thinks I am |
| 45 | + // comparing Symbol instance rather than the string resulting from a call |
| 46 | + // to typeof. |
| 47 | + // |
| 48 | + // Le sigh.... |
| 49 | + const type: string = typeof Symbol[specificSymbol]; |
| 50 | + |
| 51 | + if (type !== 'symbol') { |
| 52 | + return false; |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + return true; |
| 57 | +} |
| 58 | + |
| 59 | +/** |
| 60 | + * The `applyToStringTag()` function checks first to see if the runtime |
| 61 | + * supports the `Symbol` class and then if the `Symbol.toStringTag` constant |
| 62 | + * is defined as a `Symbol` instance. If both conditions are met, the |
| 63 | + * Symbol.toStringTag property is defined as a getter that returns the |
| 64 | + * supplied class constructor's name. |
| 65 | + * |
| 66 | + * @method applyToStringTag |
| 67 | + * |
| 68 | + * @param {Class<*>} classObject a class such as Object, String, Number but |
| 69 | + * typically one of your own creation through the class keyword; `class A {}`, |
| 70 | + * for example. |
| 71 | + */ |
| 72 | +export function applyToStringTag(classObject: Class<*>): void { |
| 73 | + if (hasSymbolSupport('toStringTag')) { |
| 74 | + Object.defineProperty(classObject.prototype, Symbol.toStringTag, { |
| 75 | + get() { |
| 76 | + return this.constructor.name; |
| 77 | + }, |
| 78 | + }); |
| 79 | + } |
| 80 | +} |
0 commit comments