From ca9463dd692ddcf85073bca921fd05f970bba6cf Mon Sep 17 00:00:00 2001 From: Tim Dorr Date: Tue, 3 Aug 2021 13:01:54 -0400 Subject: [PATCH] Backport kindOf fix from #4090 --- src/utils/kindOf.ts | 104 ++++++++++++++++++++++---------------------- 1 file changed, 52 insertions(+), 52 deletions(-) diff --git a/src/utils/kindOf.ts b/src/utils/kindOf.ts index 2c600bc702..6288051e98 100644 --- a/src/utils/kindOf.ts +++ b/src/utils/kindOf.ts @@ -1,64 +1,64 @@ -export function kindOf(val: any): string { - let typeOfVal: string = typeof val +// Inlined / shortened version of `kindOf` from https://github.com/jonschlinkert/kind-of +export function miniKindOf(val: any): string { + if (val === void 0) return 'undefined' + if (val === null) return 'null' - if (process.env.NODE_ENV !== 'production') { - // Inlined / shortened version of `kindOf` from https://github.com/jonschlinkert/kind-of - function miniKindOf(val: any) { - if (val === void 0) return 'undefined' - if (val === null) return 'null' + const type = typeof val + switch (type) { + case 'boolean': + case 'string': + case 'number': + case 'symbol': + case 'function': { + return type + } + } - const type = typeof val - switch (type) { - case 'boolean': - case 'string': - case 'number': - case 'symbol': - case 'function': { - return type - } - } + if (Array.isArray(val)) return 'array' + if (isDate(val)) return 'date' + if (isError(val)) return 'error' - if (Array.isArray(val)) return 'array' - if (isDate(val)) return 'date' - if (isError(val)) return 'error' + const constructorName = ctorName(val) + switch (constructorName) { + case 'Symbol': + case 'Promise': + case 'WeakMap': + case 'WeakSet': + case 'Map': + case 'Set': + return constructorName + } - const constructorName = ctorName(val) - switch (constructorName) { - case 'Symbol': - case 'Promise': - case 'WeakMap': - case 'WeakSet': - case 'Map': - case 'Set': - return constructorName - } + // other + return type.slice(8, -1).toLowerCase().replace(/\s/g, '') +} - // other - return type.slice(8, -1).toLowerCase().replace(/\s/g, '') - } +function ctorName(val: any): string | null { + return typeof val.constructor === 'function' ? val.constructor.name : null +} - function ctorName(val: any): string | null { - return typeof val.constructor === 'function' ? val.constructor.name : null - } +function isError(val: any) { + return ( + val instanceof Error || + (typeof val.message === 'string' && + val.constructor && + typeof val.constructor.stackTraceLimit === 'number') + ) +} - function isError(val: any) { - return ( - val instanceof Error || - (typeof val.message === 'string' && - val.constructor && - typeof val.constructor.stackTraceLimit === 'number') - ) - } +function isDate(val: any) { + if (val instanceof Date) return true + return ( + typeof val.toDateString === 'function' && + typeof val.getDate === 'function' && + typeof val.setDate === 'function' + ) +} - function isDate(val: any) { - if (val instanceof Date) return true - return ( - typeof val.toDateString === 'function' && - typeof val.getDate === 'function' && - typeof val.setDate === 'function' - ) - } +export function kindOf(val: any) { + let typeOfVal: string = typeof val + if (process.env.NODE_ENV !== 'production') { typeOfVal = miniKindOf(val) }