UNPKG

@feugene/mu

Version:

Helpful TS utilities without dependencies

47 lines (38 loc) 1.16 kB
import root from './root' /** Built-in value references. */ const symToStringTag = root.Symbol ? root.Symbol.toStringTag : undefined /** Used for built-in method references. */ const objectProto = Object.prototype /** Used to check objects for own properties. */ const hasOwnProperty = objectProto.hasOwnProperty /** * Used to resolve the * [`toStringTag`](http://ecma-international.org/ecma-262/7.0/#sec-object.prototype.tostring) * of values. */ const nativeObjectToString = objectProto.toString /** * A specialized version of `baseGetTag` which ignores `Symbol.toStringTag` values. * * @private * @param {*} value The value to query. * @returns {string} Returns the raw `toStringTag`. */ export default function getRawTag(value: any): string { const isOwn = hasOwnProperty.call(value, symToStringTag), tag = value[symToStringTag] let unmasked = false try { value[symToStringTag] = undefined unmasked = true } catch (error) {} const result = nativeObjectToString.call(value) if (unmasked) { if (isOwn) { value[symToStringTag] = tag } else { delete value[symToStringTag] } } return result }