is-it-type
Version:
Determine type of a variable
451 lines (394 loc) • 12.7 kB
JavaScript
(function (global, factory) {
typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) :
typeof define === 'function' && define.amd ? define(['exports'], factory) :
(global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global.isItType = {}));
})(this, (function (exports) { 'use strict';
var commonjsGlobal = typeof globalThis !== 'undefined' ? globalThis : typeof window !== 'undefined' ? window : typeof global !== 'undefined' ? global : typeof self !== 'undefined' ? self : {};
var toStr$2 = Object.prototype.toString;
var isArguments = function isArguments(value) {
var str = toStr$2.call(value);
var isArgs = str === '[object Arguments]';
if (!isArgs) {
isArgs = str !== '[object Array]' &&
value !== null &&
typeof value === 'object' &&
typeof value.length === 'number' &&
value.length >= 0 &&
toStr$2.call(value.callee) === '[object Function]';
}
return isArgs;
};
var keysShim$1;
if (!Object.keys) {
// modified from https://github.com/es-shims/es5-shim
var has = Object.prototype.hasOwnProperty;
var toStr$1 = Object.prototype.toString;
var isArgs$1 = isArguments; // eslint-disable-line global-require
var isEnumerable = Object.prototype.propertyIsEnumerable;
var hasDontEnumBug = !isEnumerable.call({ toString: null }, 'toString');
var hasProtoEnumBug = isEnumerable.call(function () {}, 'prototype');
var dontEnums = [
'toString',
'toLocaleString',
'valueOf',
'hasOwnProperty',
'isPrototypeOf',
'propertyIsEnumerable',
'constructor'
];
var equalsConstructorPrototype = function (o) {
var ctor = o.constructor;
return ctor && ctor.prototype === o;
};
var excludedKeys = {
$applicationCache: true,
$console: true,
$external: true,
$frame: true,
$frameElement: true,
$frames: true,
$innerHeight: true,
$innerWidth: true,
$onmozfullscreenchange: true,
$onmozfullscreenerror: true,
$outerHeight: true,
$outerWidth: true,
$pageXOffset: true,
$pageYOffset: true,
$parent: true,
$scrollLeft: true,
$scrollTop: true,
$scrollX: true,
$scrollY: true,
$self: true,
$webkitIndexedDB: true,
$webkitStorageInfo: true,
$window: true
};
var hasAutomationEqualityBug = (function () {
/* global window */
if (typeof window === 'undefined') { return false; }
for (var k in window) {
try {
if (!excludedKeys['$' + k] && has.call(window, k) && window[k] !== null && typeof window[k] === 'object') {
try {
equalsConstructorPrototype(window[k]);
} catch (e) {
return true;
}
}
} catch (e) {
return true;
}
}
return false;
}());
var equalsConstructorPrototypeIfNotBuggy = function (o) {
/* global window */
if (typeof window === 'undefined' || !hasAutomationEqualityBug) {
return equalsConstructorPrototype(o);
}
try {
return equalsConstructorPrototype(o);
} catch (e) {
return false;
}
};
keysShim$1 = function keys(object) {
var isObject = object !== null && typeof object === 'object';
var isFunction = toStr$1.call(object) === '[object Function]';
var isArguments = isArgs$1(object);
var isString = isObject && toStr$1.call(object) === '[object String]';
var theKeys = [];
if (!isObject && !isFunction && !isArguments) {
throw new TypeError('Object.keys called on a non-object');
}
var skipProto = hasProtoEnumBug && isFunction;
if (isString && object.length > 0 && !has.call(object, 0)) {
for (var i = 0; i < object.length; ++i) {
theKeys.push(String(i));
}
}
if (isArguments && object.length > 0) {
for (var j = 0; j < object.length; ++j) {
theKeys.push(String(j));
}
} else {
for (var name in object) {
if (!(skipProto && name === 'prototype') && has.call(object, name)) {
theKeys.push(String(name));
}
}
}
if (hasDontEnumBug) {
var skipConstructor = equalsConstructorPrototypeIfNotBuggy(object);
for (var k = 0; k < dontEnums.length; ++k) {
if (!(skipConstructor && dontEnums[k] === 'constructor') && has.call(object, dontEnums[k])) {
theKeys.push(dontEnums[k]);
}
}
}
return theKeys;
};
}
var implementation$3 = keysShim$1;
var slice = Array.prototype.slice;
var isArgs = isArguments;
var origKeys = Object.keys;
var keysShim = origKeys ? function keys(o) { return origKeys(o); } : implementation$3;
var originalKeys = Object.keys;
keysShim.shim = function shimObjectKeys() {
if (Object.keys) {
var keysWorksWithArguments = (function () {
// Safari 5.0 bug
var args = Object.keys(arguments);
return args && args.length === arguments.length;
}(1, 2));
if (!keysWorksWithArguments) {
Object.keys = function keys(object) { // eslint-disable-line func-name-matching
if (isArgs(object)) {
return originalKeys(slice.call(object));
}
return originalKeys(object);
};
}
} else {
Object.keys = keysShim;
}
return Object.keys || keysShim;
};
var objectKeys = keysShim;
var keys = objectKeys;
var hasSymbols = typeof Symbol === 'function' && typeof Symbol('foo') === 'symbol';
var toStr = Object.prototype.toString;
var concat = Array.prototype.concat;
var origDefineProperty = Object.defineProperty;
var isFunction$1 = function (fn) {
return typeof fn === 'function' && toStr.call(fn) === '[object Function]';
};
var arePropertyDescriptorsSupported = function () {
var obj = {};
try {
origDefineProperty(obj, 'x', { enumerable: false, value: obj });
// eslint-disable-next-line no-unused-vars, no-restricted-syntax
for (var _ in obj) { // jscs:ignore disallowUnusedVariables
return false;
}
return obj.x === obj;
} catch (e) { /* this is IE 8. */
return false;
}
};
var supportsDescriptors = origDefineProperty && arePropertyDescriptorsSupported();
var defineProperty = function (object, name, value, predicate) {
if (name in object && (!isFunction$1(predicate) || !predicate())) {
return;
}
if (supportsDescriptors) {
origDefineProperty(object, name, {
configurable: true,
enumerable: false,
value: value,
writable: true
});
} else {
object[name] = value;
}
};
var defineProperties$1 = function (object, map) {
var predicates = arguments.length > 2 ? arguments[2] : {};
var props = keys(map);
if (hasSymbols) {
props = concat.call(props, Object.getOwnPropertySymbols(map));
}
for (var i = 0; i < props.length; i += 1) {
defineProperty(object, props[i], map[props[i]], predicates[props[i]]);
}
};
defineProperties$1.supportsDescriptors = !!supportsDescriptors;
var defineProperties_1 = defineProperties$1;
var implementation$2 = commonjsGlobal;
var implementation$1 = implementation$2;
var polyfill$1 = function getPolyfill() {
if (typeof commonjsGlobal !== 'object' || !commonjsGlobal || commonjsGlobal.Math !== Math || commonjsGlobal.Array !== Array) {
return implementation$1;
}
return commonjsGlobal;
};
var define = defineProperties_1;
var getPolyfill$1 = polyfill$1;
var shim$1 = function shimGlobal() {
var polyfill = getPolyfill$1();
if (define.supportsDescriptors) {
var descriptor = Object.getOwnPropertyDescriptor(polyfill, 'globalThis');
if (!descriptor || (descriptor.configurable && (descriptor.enumerable || descriptor.writable || globalThis !== polyfill))) { // eslint-disable-line max-len
Object.defineProperty(polyfill, 'globalThis', {
configurable: true,
enumerable: false,
value: polyfill,
writable: false
});
}
} else if (typeof globalThis !== 'object' || globalThis !== polyfill) {
polyfill.globalThis = polyfill;
}
return polyfill;
};
var defineProperties = defineProperties_1;
var implementation = implementation$2;
var getPolyfill = polyfill$1;
var shim = shim$1;
var polyfill = getPolyfill();
var getGlobal = function () { return polyfill; };
defineProperties(getGlobal, {
getPolyfill: getPolyfill,
implementation: implementation,
shim: shim
});
var globalthis = getGlobal;
/* --------------------
* is-it-type module
* Entry point
* ------------------*/
/*
* Replication of core-util-is methods.
* https://www.npmjs.com/package/core-util-is
* NB `isBuffer()` is omitted and `isObject()` is different from `core-util-is`'s implementation
*/
var isArray = Array.isArray;
function isBoolean(arg) {
return isType('boolean', arg);
}
function isNull(arg) {
return arg === null;
}
function isUndefined(arg) {
return arg === void 0; // eslint-disable-line no-void
}
function isNullOrUndefined(arg) {
return arg == null;
}
function isNumber(arg) {
return isType('number', arg);
}
function isString(arg) {
return isType('string', arg);
}
function isSymbol(arg) {
return isType('symbol', arg);
}
function isRegExp(arg) {
return arg instanceof RegExp;
}
function isDate(arg) {
return arg instanceof Date;
}
function isError(arg) {
return arg instanceof Error;
}
function isFunction(arg) {
return isType('function', arg);
}
function isPrimitive(arg) {
var type = getType(arg);
return arg == null || type === 'boolean' || type === 'number' || type === 'string' || type === 'symbol';
}
/*
* Additional methods
*/
// Strings
function isEmptyString(arg) {
return arg === '';
}
function isFullString(arg) {
return isString(arg) && !isEmptyString(arg);
} // Objects
var getPrototypeOf = Object.getPrototypeOf,
ObjectPrototype = Object.prototype,
globalThis$1 = globalthis();
function isObject(arg) {
if (!isType('object', arg) || isNull(arg)) return false;
var proto = getPrototypeOf(arg);
if (proto === null || proto === ObjectPrototype) return true;
while (true) {
// eslint-disable-line no-constant-condition
var nextProto = getPrototypeOf(proto);
if (nextProto === null) return true;
if (nextProto === ObjectPrototype) break;
proto = nextProto;
}
return isNotNativeProto(proto);
}
function isNotNativeProto(proto) {
var nativeProtos = [];
for (var _i = 0, _arr = ['Function', 'Array', 'Number', 'Boolean', 'String', 'Symbol', 'Date', 'Promise', 'RegExp', 'Error', 'ArrayBuffer', 'DataView', 'Map', 'BigInt', 'Set', 'WeakMap', 'WeakSet', 'SharedArrayBuffer', 'FinalizationRegistry', 'WeakRef', 'URL', 'URLSearchParams', 'TextEncoder', 'TextDecoder']; _i < _arr.length; _i++) {
var ctorName = _arr[_i];
var ctor = globalThis$1[ctorName];
if (ctor) nativeProtos.push(ctor.prototype);
}
if (typeof Uint8Array === 'function') nativeProtos.push(getPrototypeOf(Uint8Array.prototype));
if (typeof Set === 'function') {
nativeProtos = new Set(nativeProtos);
isNotNativeProto = function isNotNativeProto(p) {
return !nativeProtos.has(p);
}; // eslint-disable-line no-func-assign
} else {
isNotNativeProto = function isNotNativeProto(p) {
return !nativeProtos.includes(p);
}; // eslint-disable-line no-func-assign
}
return isNotNativeProto(proto);
}
function isEmptyObject(arg) {
return isObject(arg) && Object.keys(arg).length === 0;
} // Numbers
function isInteger(arg) {
return Number.isInteger(arg);
}
function isPositiveInteger(arg) {
return isInteger(arg) && arg > 0;
}
function isPositiveIntegerOrZero(arg) {
return isInteger(arg) && arg >= 0;
}
function isNegativeInteger(arg) {
return isInteger(arg) && arg < 0;
}
function isNegativeIntegerOrZero(arg) {
return isInteger(arg) && arg <= 0;
} // Other
function isType(type, arg) {
return getType(arg) === type;
}
/*
* Helpers
*/
function getType(arg) {
return typeof arg;
}
exports.isArray = isArray;
exports.isBoolean = isBoolean;
exports.isDate = isDate;
exports.isEmptyObject = isEmptyObject;
exports.isEmptyString = isEmptyString;
exports.isError = isError;
exports.isFullString = isFullString;
exports.isFunction = isFunction;
exports.isInteger = isInteger;
exports.isNegativeInteger = isNegativeInteger;
exports.isNegativeIntegerOrZero = isNegativeIntegerOrZero;
exports.isNull = isNull;
exports.isNullOrUndefined = isNullOrUndefined;
exports.isNumber = isNumber;
exports.isObject = isObject;
exports.isPositiveInteger = isPositiveInteger;
exports.isPositiveIntegerOrZero = isPositiveIntegerOrZero;
exports.isPrimitive = isPrimitive;
exports.isRegExp = isRegExp;
exports.isString = isString;
exports.isSymbol = isSymbol;
exports.isType = isType;
exports.isUndefined = isUndefined;
Object.defineProperty(exports, '__esModule', { value: true });
}));
//# sourceMappingURL=is-it-type.js.map