Simplify isType modules.

This commit is contained in:
John-David Dalton
2017-01-10 13:44:02 -08:00
parent b2f69ea36a
commit 4ecd69e4fa
24 changed files with 50 additions and 217 deletions

View File

@@ -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;

View File

@@ -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;

View File

@@ -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;

View File

@@ -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;

View File

@@ -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;

View File

@@ -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;

View File

@@ -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;

View File

@@ -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;

View File

@@ -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;

View File

@@ -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;

View File

@@ -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)) {

View File

@@ -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));
}

View File

@@ -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;

View File

@@ -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;

View File

@@ -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.*?')
}$`);

View File

@@ -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;

View File

@@ -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);

View File

@@ -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;

View File

@@ -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;

View File

@@ -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;

View File

@@ -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;

View File

@@ -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;

View File

@@ -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;

View File

@@ -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;