UNPKG

is-it-type

Version:
144 lines (123 loc) 3.9 kB
import getGlobalThis from 'globalthis'; /* -------------------- * 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 = getGlobalThis(); 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[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; } export { isArray, isBoolean, isDate, isEmptyObject, isEmptyString, isError, isFullString, isFunction, isInteger, isNegativeInteger, isNegativeIntegerOrZero, isNull, isNullOrUndefined, isNumber, isObject, isPositiveInteger, isPositiveIntegerOrZero, isPrimitive, isRegExp, isString, isSymbol, isType, isUndefined }; //# sourceMappingURL=is-it-type.js.map