From 4ecd69e4facae9a4debe17c2b28438d2a7d4568c Mon Sep 17 00:00:00 2001 From: John-David Dalton Date: Tue, 10 Jan 2017 13:44:02 -0800 Subject: [PATCH] Simplify isType modules. --- .internal/baseIsArrayBuffer.js | 17 ---------- .internal/baseIsDate.js | 18 ---------- .internal/baseIsMap.js | 18 ---------- .internal/baseIsRegExp.js | 18 ---------- .internal/baseIsSet.js | 18 ---------- .internal/baseIsTypedArray.js | 60 ---------------------------------- isArguments.js | 5 +-- isArrayBuffer.js | 5 +-- isBoolean.js | 5 +-- isDate.js | 5 +-- isEmpty.js | 6 +--- isError.js | 6 +--- isFunction.js | 9 ++--- isMap.js | 5 +-- isNative.js | 8 +---- isNumber.js | 5 +-- isPlainObject.js | 5 +-- isRegExp.js | 5 +-- isSet.js | 5 +-- isString.js | 5 +-- isSymbol.js | 5 +-- isTypedArray.js | 24 ++++++++++++-- isWeakMap.js | 5 +-- isWeakSet.js | 5 +-- 24 files changed, 50 insertions(+), 217 deletions(-) delete mode 100644 .internal/baseIsArrayBuffer.js delete mode 100644 .internal/baseIsDate.js delete mode 100644 .internal/baseIsMap.js delete mode 100644 .internal/baseIsRegExp.js delete mode 100644 .internal/baseIsSet.js delete mode 100644 .internal/baseIsTypedArray.js diff --git a/.internal/baseIsArrayBuffer.js b/.internal/baseIsArrayBuffer.js deleted file mode 100644 index 7cd875641..000000000 --- a/.internal/baseIsArrayBuffer.js +++ /dev/null @@ -1,17 +0,0 @@ -import baseGetTag from './.internal/baseGetTag.js'; -import isObjectLike from './isObjectLike.js'; - -const arrayBufferTag = '[object ArrayBuffer]'; - -/** - * The base implementation of `isArrayBuffer` without Node.js optimizations. - * - * @private - * @param {*} value The value to check. - * @returns {boolean} Returns `true` if `value` is an array buffer, else `false`. - */ -function baseIsArrayBuffer(value) { - return isObjectLike(value) && baseGetTag(value) == arrayBufferTag; -} - -export default baseIsArrayBuffer; diff --git a/.internal/baseIsDate.js b/.internal/baseIsDate.js deleted file mode 100644 index cb737279b..000000000 --- a/.internal/baseIsDate.js +++ /dev/null @@ -1,18 +0,0 @@ -import baseGetTag from './.internal/baseGetTag.js'; -import isObjectLike from './isObjectLike.js'; - -/** `Object#toString` result references. */ -const dateTag = '[object Date]'; - -/** - * The base implementation of `isDate` without Node.js optimizations. - * - * @private - * @param {*} value The value to check. - * @returns {boolean} Returns `true` if `value` is a date object, else `false`. - */ -function baseIsDate(value) { - return isObjectLike(value) && baseGetTag(value) == dateTag; -} - -export default baseIsDate; diff --git a/.internal/baseIsMap.js b/.internal/baseIsMap.js deleted file mode 100644 index b9c6a8554..000000000 --- a/.internal/baseIsMap.js +++ /dev/null @@ -1,18 +0,0 @@ -import getTag from './.internal/getTag.js'; -import isObjectLike from './isObjectLike.js'; - -/** `Object#toString` result references. */ -const mapTag = '[object Map]'; - -/** - * The base implementation of `isMap` without Node.js optimizations. - * - * @private - * @param {*} value The value to check. - * @returns {boolean} Returns `true` if `value` is a map, else `false`. - */ -function baseIsMap(value) { - return isObjectLike(value) && getTag(value) == mapTag; -} - -export default baseIsMap; diff --git a/.internal/baseIsRegExp.js b/.internal/baseIsRegExp.js deleted file mode 100644 index 43da1a187..000000000 --- a/.internal/baseIsRegExp.js +++ /dev/null @@ -1,18 +0,0 @@ -import baseGetTag from './.internal/baseGetTag.js'; -import isObjectLike from './isObjectLike.js'; - -/** `Object#toString` result references. */ -const regexpTag = '[object RegExp]'; - -/** - * The base implementation of `isRegExp` without Node.js optimizations. - * - * @private - * @param {*} value The value to check. - * @returns {boolean} Returns `true` if `value` is a regexp, else `false`. - */ -function baseIsRegExp(value) { - return isObjectLike(value) && baseGetTag(value) == regexpTag; -} - -export default baseIsRegExp; diff --git a/.internal/baseIsSet.js b/.internal/baseIsSet.js deleted file mode 100644 index a23612531..000000000 --- a/.internal/baseIsSet.js +++ /dev/null @@ -1,18 +0,0 @@ -import getTag from './.internal/getTag.js'; -import isObjectLike from './isObjectLike.js'; - -/** `Object#toString` result references. */ -const setTag = '[object Set]'; - -/** - * The base implementation of `isSet` without Node.js optimizations. - * - * @private - * @param {*} value The value to check. - * @returns {boolean} Returns `true` if `value` is a set, else `false`. - */ -function baseIsSet(value) { - return isObjectLike(value) && getTag(value) == setTag; -} - -export default baseIsSet; diff --git a/.internal/baseIsTypedArray.js b/.internal/baseIsTypedArray.js deleted file mode 100644 index 5c4935d04..000000000 --- a/.internal/baseIsTypedArray.js +++ /dev/null @@ -1,60 +0,0 @@ -import baseGetTag from './.internal/baseGetTag.js'; -import isLength from './isLength.js'; -import isObjectLike from './isObjectLike.js'; - -/** `Object#toString` result references. */ -const argsTag = '[object Arguments]'; -const arrayTag = '[object Array]'; -const boolTag = '[object Boolean]'; -const dateTag = '[object Date]'; -const errorTag = '[object Error]'; -const funcTag = '[object Function]'; -const mapTag = '[object Map]'; -const numberTag = '[object Number]'; -const objectTag = '[object Object]'; -const regexpTag = '[object RegExp]'; -const setTag = '[object Set]'; -const stringTag = '[object String]'; -const weakMapTag = '[object WeakMap]'; - -const arrayBufferTag = '[object ArrayBuffer]'; -const dataViewTag = '[object DataView]'; -const float32Tag = '[object Float32Array]'; -const float64Tag = '[object Float64Array]'; -const int8Tag = '[object Int8Array]'; -const int16Tag = '[object Int16Array]'; -const int32Tag = '[object Int32Array]'; -const uint8Tag = '[object Uint8Array]'; -const uint8ClampedTag = '[object Uint8ClampedArray]'; -const uint16Tag = '[object Uint16Array]'; -const uint32Tag = '[object Uint32Array]'; - -/** Used to identify `toStringTag` values of typed arrays. */ -const typedArrayTags = {}; -typedArrayTags[float32Tag] = typedArrayTags[float64Tag] = -typedArrayTags[int8Tag] = typedArrayTags[int16Tag] = -typedArrayTags[int32Tag] = typedArrayTags[uint8Tag] = -typedArrayTags[uint8ClampedTag] = typedArrayTags[uint16Tag] = -typedArrayTags[uint32Tag] = true; -typedArrayTags[argsTag] = typedArrayTags[arrayTag] = -typedArrayTags[arrayBufferTag] = typedArrayTags[boolTag] = -typedArrayTags[dataViewTag] = typedArrayTags[dateTag] = -typedArrayTags[errorTag] = typedArrayTags[funcTag] = -typedArrayTags[mapTag] = typedArrayTags[numberTag] = -typedArrayTags[objectTag] = typedArrayTags[regexpTag] = -typedArrayTags[setTag] = typedArrayTags[stringTag] = -typedArrayTags[weakMapTag] = false; - -/** - * The base implementation of `isTypedArray` without Node.js optimizations. - * - * @private - * @param {*} value The value to check. - * @returns {boolean} Returns `true` if `value` is a typed array, else `false`. - */ -function baseIsTypedArray(value) { - return isObjectLike(value) && - isLength(value.length) && !!typedArrayTags[baseGetTag(value)]; -} - -export default baseIsTypedArray; diff --git a/isArguments.js b/isArguments.js index 62a500f0b..8a9423ce9 100644 --- a/isArguments.js +++ b/isArguments.js @@ -1,9 +1,6 @@ import baseGetTag from './.internal/baseGetTag.js'; import isObjectLike from './isObjectLike.js'; -/** `Object#toString` result references. */ -const argsTag = '[object Arguments]'; - /** * Checks if `value` is likely an `arguments` object. * @@ -21,7 +18,7 @@ const argsTag = '[object Arguments]'; * // => false */ function isArguments(value) { - return isObjectLike(value) && baseGetTag(value) == argsTag; + return isObjectLike(value) && baseGetTag(value) == '[object Arguments]'; } export default isArguments; diff --git a/isArrayBuffer.js b/isArrayBuffer.js index 1a7778206..dabb7144b 100644 --- a/isArrayBuffer.js +++ b/isArrayBuffer.js @@ -1,4 +1,5 @@ -import baseIsArrayBuffer from './.internal/baseIsArrayBuffer.js'; +import baseGetTag from './.internal/baseGetTag.js'; +import isObjectLike from './isObjectLike.js'; import nodeUtil from './.internal/nodeUtil.js'; /* Node.js helper references. */ @@ -21,6 +22,6 @@ const nodeIsArrayBuffer = nodeUtil && nodeUtil.isArrayBuffer; */ const isArrayBuffer = nodeIsArrayBuffer ? value => nodeIsArrayBuffer(value) - : baseIsArrayBuffer; + : value => isObjectLike(value) && baseGetTag(value) == '[object ArrayBuffer]'; export default isArrayBuffer; diff --git a/isBoolean.js b/isBoolean.js index 527007cc2..4df8691ce 100644 --- a/isBoolean.js +++ b/isBoolean.js @@ -1,9 +1,6 @@ import baseGetTag from './.internal/baseGetTag.js'; import isObjectLike from './isObjectLike.js'; -/** `Object#toString` result references. */ -const boolTag = '[object Boolean]'; - /** * Checks if `value` is classified as a boolean primitive or object. * @@ -21,7 +18,7 @@ const boolTag = '[object Boolean]'; */ function isBoolean(value) { return value === true || value === false || - (isObjectLike(value) && baseGetTag(value) == boolTag); + (isObjectLike(value) && baseGetTag(value) == '[object Boolean]'); } export default isBoolean; diff --git a/isDate.js b/isDate.js index ef0e7302b..c9fa85124 100644 --- a/isDate.js +++ b/isDate.js @@ -1,4 +1,5 @@ -import baseIsDate from './.internal/baseIsDate.js'; +import baseGetTag from './.internal/baseGetTag.js'; +import isObjectLike from './isObjectLike.js'; import nodeUtil from './.internal/nodeUtil.js'; /* Node.js helper references. */ @@ -21,6 +22,6 @@ const nodeIsDate = nodeUtil && nodeUtil.isDate; */ const isDate = nodeIsDate ? value => nodeIsDate(value) - : baseIsDate; + : value => isObjectLike(value) && baseGetTag(value) == '[object Date]'; export default isDate; diff --git a/isEmpty.js b/isEmpty.js index 0efccc3be..bfa12e574 100644 --- a/isEmpty.js +++ b/isEmpty.js @@ -6,10 +6,6 @@ import isBuffer from './isBuffer.js'; import isPrototype from './.internal/isPrototype.js'; import isTypedArray from './isTypedArray.js'; -/** `Object#toString` result references. */ -const mapTag = '[object Map]'; -const setTag = '[object Set]'; - /** Used to check objects for own properties. */ const hasOwnProperty = Object.prototype.hasOwnProperty; @@ -54,7 +50,7 @@ function isEmpty(value) { return !value.length; } const tag = getTag(value); - if (tag == mapTag || tag == setTag) { + if (tag == '[object Map]' || tag == '[object Set]') { return !value.size; } if (isPrototype(value)) { diff --git a/isError.js b/isError.js index c25642114..3cf650bfc 100644 --- a/isError.js +++ b/isError.js @@ -2,10 +2,6 @@ import baseGetTag from './.internal/baseGetTag.js'; import isObjectLike from './isObjectLike.js'; import isPlainObject from './isPlainObject.js'; -/** `Object#toString` result references. */ -const domExcTag = '[object DOMException]'; -const errorTag = '[object Error]'; - /** * Checks if `value` is an `Error`, `EvalError`, `RangeError`, `ReferenceError`, * `SyntaxError`, `TypeError`, or `URIError` object. @@ -27,7 +23,7 @@ function isError(value) { return false; } const tag = baseGetTag(value); - return tag == errorTag || tag == domExcTag || + return tag == '[object Error]' || tag == '[object DOMException]' || (typeof value.message == 'string' && typeof value.name == 'string' && !isPlainObject(value)); } diff --git a/isFunction.js b/isFunction.js index 6d3df5eff..feaf661a2 100644 --- a/isFunction.js +++ b/isFunction.js @@ -1,12 +1,6 @@ import baseGetTag from './.internal/baseGetTag.js'; import isObject from './isObject.js'; -/** `Object#toString` result references. */ -const asyncTag = '[object AsyncFunction]'; -const funcTag = '[object Function]'; -const genTag = '[object GeneratorFunction]'; -const proxyTag = '[object Proxy]'; - /** * Checks if `value` is classified as a `Function` object. * @@ -29,7 +23,8 @@ function isFunction(value) { // The use of `Object#toString` avoids issues with the `typeof` operator // in Safari 9 which returns 'object' for typed arrays and other constructors. const tag = baseGetTag(value); - return tag == funcTag || tag == asyncTag || tag == genTag || tag == proxyTag; + return tag == '[object Function]' || tag == '[object AsyncFunction]' || + tag == '[object GeneratorFunction]' || tag == '[object Proxy]'; } export default isFunction; diff --git a/isMap.js b/isMap.js index 6790bb14b..10ef917d8 100644 --- a/isMap.js +++ b/isMap.js @@ -1,4 +1,5 @@ -import baseIsMap from './.internal/baseIsMap.js'; +import getTag from './.internal/getTag.js'; +import isObjectLike from './isObjectLike.js'; import nodeUtil from './.internal/nodeUtil.js'; /* Node.js helper references. */ @@ -21,6 +22,6 @@ const nodeIsMap = nodeUtil && nodeUtil.isMap; */ const isMap = nodeIsMap ? value => nodeIsMap(value) - : baseIsMap; + : value => isObjectLike(value) && getTag(value) == '[object Map]'; export default isMap; diff --git a/isNative.js b/isNative.js index de66934c0..d2862c67e 100644 --- a/isNative.js +++ b/isNative.js @@ -11,15 +11,9 @@ const reRegExpChar = /[\\^$.*+?()[\]{}|]/g; /** Used to detect host constructors (Safari). */ const reIsHostCtor = /^\[object .+?Constructor\]$/; -/** Used to resolve the decompiled source of functions. */ -const funcToString = Function.prototype.toString; - -/** Used to check objects for own properties. */ -const hasOwnProperty = Object.prototype.hasOwnProperty; - /** Used to detect if a method is native. */ const reIsNative = RegExp(`^${ - funcToString.call(hasOwnProperty) + Function.prototype.toString.call(Object.prototype.hasOwnProperty) .replace(reRegExpChar, '\\$&') .replace(/hasOwnProperty|(function).*?(?=\\\()| for .+?(?=\\\])/g, '$1.*?') }$`); diff --git a/isNumber.js b/isNumber.js index cdf20d1a5..b9b560a4c 100644 --- a/isNumber.js +++ b/isNumber.js @@ -1,9 +1,6 @@ import baseGetTag from './.internal/baseGetTag.js'; import isObjectLike from './isObjectLike.js'; -/** `Object#toString` result references. */ -const numberTag = '[object Number]'; - /** * Checks if `value` is classified as a `Number` primitive or object. * @@ -30,7 +27,7 @@ const numberTag = '[object Number]'; */ function isNumber(value) { return typeof value == 'number' || - (isObjectLike(value) && baseGetTag(value) == numberTag); + (isObjectLike(value) && baseGetTag(value) == '[object Number]'); } export default isNumber; diff --git a/isPlainObject.js b/isPlainObject.js index ac35ff1ee..df2629d20 100644 --- a/isPlainObject.js +++ b/isPlainObject.js @@ -2,9 +2,6 @@ import baseGetTag from './.internal/baseGetTag.js'; import getPrototype from './.internal/getPrototype.js'; import isObjectLike from './isObjectLike.js'; -/** `Object#toString` result references. */ -const objectTag = '[object Object]'; - /** Used to resolve the decompiled source of functions. */ const funcToString = Function.prototype.toString; @@ -41,7 +38,7 @@ const objectCtorString = funcToString.call(Object); * // => true */ function isPlainObject(value) { - if (!isObjectLike(value) || baseGetTag(value) != objectTag) { + if (!isObjectLike(value) || baseGetTag(value) != '[object Object]') { return false; } const proto = getPrototype(value); diff --git a/isRegExp.js b/isRegExp.js index 516d73506..b84f5f254 100644 --- a/isRegExp.js +++ b/isRegExp.js @@ -1,4 +1,5 @@ -import baseIsRegExp from './.internal/baseIsRegExp.js'; +import baseGetTag from './.internal/baseGetTag.js'; +import isObjectLike from './isObjectLike.js'; import nodeUtil from './.internal/nodeUtil.js'; /* Node.js helper references. */ @@ -21,6 +22,6 @@ const nodeIsRegExp = nodeUtil && nodeUtil.isRegExp; */ const isRegExp = nodeIsRegExp ? value => nodeIsRegExp(value) - : baseIsRegExp; + : value => isObjectLike(value) && baseGetTag(value) == '[object RegExp]'; export default isRegExp; diff --git a/isSet.js b/isSet.js index c8a4e860d..6dc28fb04 100644 --- a/isSet.js +++ b/isSet.js @@ -1,4 +1,5 @@ -import baseIsSet from './.internal/baseIsSet.js'; +import getTag from './.internal/getTag.js'; +import isObjectLike from './isObjectLike.js'; import nodeUtil from './.internal/nodeUtil.js'; /* Node.js helper references. */ @@ -21,6 +22,6 @@ const nodeIsSet = nodeUtil && nodeUtil.isSet; */ const isSet = nodeIsSet ? value => nodeIsSet(value) - : baseIsSet; + : value => isObjectLike(value) && getTag(value) == '[object Set]'; export default isSet; diff --git a/isString.js b/isString.js index 77140c1d9..7e2be4b2e 100644 --- a/isString.js +++ b/isString.js @@ -1,9 +1,6 @@ import baseGetTag from './.internal/baseGetTag.js'; import isObjectLike from './isObjectLike.js'; -/** `Object#toString` result references. */ -const stringTag = '[object String]'; - /** * Checks if `value` is classified as a `String` primitive or object. * @@ -21,7 +18,7 @@ const stringTag = '[object String]'; */ function isString(value) { return typeof value == 'string' || - (!Array.isArray(value) && isObjectLike(value) && baseGetTag(value) == stringTag); + (!Array.isArray(value) && isObjectLike(value) && baseGetTag(value) == '[object String]'); } export default isString; diff --git a/isSymbol.js b/isSymbol.js index 66c0e85a2..9d6f195d2 100644 --- a/isSymbol.js +++ b/isSymbol.js @@ -1,9 +1,6 @@ import baseGetTag from './.internal/baseGetTag.js'; import isObjectLike from './isObjectLike.js'; -/** `Object#toString` result references. */ -const symbolTag = '[object Symbol]'; - /** * Checks if `value` is classified as a `Symbol` primitive or object. * @@ -21,7 +18,7 @@ const symbolTag = '[object Symbol]'; */ function isSymbol(value) { return typeof value == 'symbol' || - (isObjectLike(value) && baseGetTag(value) == symbolTag); + (isObjectLike(value) && baseGetTag(value) == '[object Symbol]'); } export default isSymbol; diff --git a/isTypedArray.js b/isTypedArray.js index fa6ce1cda..b8dbaf9db 100644 --- a/isTypedArray.js +++ b/isTypedArray.js @@ -1,6 +1,26 @@ -import baseIsTypedArray from './.internal/baseIsTypedArray.js'; +import baseGetTag from './.internal/baseGetTag.js'; +import isLength from './isLength.js'; +import isObjectLike from './isObjectLike.js'; import nodeUtil from './.internal/nodeUtil.js'; +/** Used to identify `toStringTag` values of typed arrays. */ +const typedArrayTags = {}; +typedArrayTags['[object Float32Array]'] = typedArrayTags['[object Float64Array]'] = +typedArrayTags['[object Int8Array]'] = typedArrayTags['[object Int16Array]'] = +typedArrayTags['[object Int32Array]'] = typedArrayTags['[object Uint8Array]'] = +typedArrayTags['[object Uint8ClampedArray]'] = typedArrayTags['[object Uint16Array]'] = +typedArrayTags['[object Uint32Array]'] = true; + +typedArrayTags['[object AsyncFunction]'] = typedArrayTags['[object Arguments]'] = +typedArrayTags['[object Array]'] = typedArrayTags['[object ArrayBuffer]'] = +typedArrayTags['[object Boolean]'] = typedArrayTags['[object DataView]'] = +typedArrayTags['[object Date]'] = typedArrayTags['[object Error]'] = +typedArrayTags['[object Function]'] = typedArrayTags['[object GeneratorFunction]'] = +typedArrayTags['[object Map]'] = typedArrayTags['[object Number]'] = +typedArrayTags['[object Object]'] = typedArrayTags['[object Proxy]'] = +typedArrayTags['[object RegExp]'] = typedArrayTags['[object Set]'] = +typedArrayTags['[object String]'] = typedArrayTags['[object WeakMap]'] = false; + /* Node.js helper references. */ const nodeIsTypedArray = nodeUtil && nodeUtil.isTypedArray; @@ -21,6 +41,6 @@ const nodeIsTypedArray = nodeUtil && nodeUtil.isTypedArray; */ const isTypedArray = nodeIsTypedArray ? value => nodeIsTypedArray(value) - : baseIsTypedArray; + : value => isObjectLike(value) && isLength(value.length) && !!typedArrayTags[baseGetTag(value)]; export default isTypedArray; diff --git a/isWeakMap.js b/isWeakMap.js index 8b46bf862..f449403c6 100644 --- a/isWeakMap.js +++ b/isWeakMap.js @@ -1,9 +1,6 @@ import getTag from './.internal/getTag.js'; import isObjectLike from './isObjectLike.js'; -/** `Object#toString` result references. */ -const weakMapTag = '[object WeakMap]'; - /** * Checks if `value` is classified as a `WeakMap` object. * @@ -20,7 +17,7 @@ const weakMapTag = '[object WeakMap]'; * // => false */ function isWeakMap(value) { - return isObjectLike(value) && getTag(value) == weakMapTag; + return isObjectLike(value) && getTag(value) == '[object WeakMap]'; } export default isWeakMap; diff --git a/isWeakSet.js b/isWeakSet.js index 3b4700395..c7577f44c 100644 --- a/isWeakSet.js +++ b/isWeakSet.js @@ -1,9 +1,6 @@ import baseGetTag from './.internal/baseGetTag.js'; import isObjectLike from './isObjectLike.js'; -/** `Object#toString` result references. */ -const weakSetTag = '[object WeakSet]'; - /** * Checks if `value` is classified as a `WeakSet` object. * @@ -20,7 +17,7 @@ const weakSetTag = '[object WeakSet]'; * // => false */ function isWeakSet(value) { - return isObjectLike(value) && baseGetTag(value) == weakSetTag; + return isObjectLike(value) && baseGetTag(value) == '[object WeakSet]'; } export default isWeakSet;