@oxog/kindof
Version:
Zero-dependency advanced type detection library with TypeScript support, plugin system, and 100% test coverage
689 lines (679 loc) • 21.6 kB
JavaScript
'use strict';
function isUndefined(value) {
return value === void 0;
}
function isNull(value) {
return value === null;
}
function isBoolean(value) {
return typeof value === "boolean";
}
function isNumber(value) {
return typeof value === "number";
}
function isString(value) {
return typeof value === "string";
}
function isSymbol(value) {
return typeof value === "symbol";
}
function isBigInt(value) {
return typeof value === "bigint";
}
function isPrimitive(value) {
return value === null || typeof value !== "object" && typeof value !== "function";
}
function isNullish(value) {
return value === null || value === void 0;
}
function isFalsy(value) {
return !value;
}
function isTruthy(value) {
return !!value;
}
function isInteger(value) {
return typeof value === "number" && Number.isInteger(value);
}
function isSafeInteger(value) {
return typeof value === "number" && Number.isSafeInteger(value);
}
function isFinite(value) {
return typeof value === "number" && Number.isFinite(value);
}
function isNaN(value) {
return typeof value === "number" && Number.isNaN(value);
}
function isInfinity(value) {
return value === Infinity || value === -Infinity;
}
function isEmptyString(value) {
return value === "";
}
function isNumericString(value) {
if (typeof value !== "string") return false;
if (value === "") return false;
return !isNaN(Number(value)) && !isNaN(parseFloat(value));
}
function isJsonString(value) {
if (typeof value !== "string") return false;
try {
JSON.parse(value);
return true;
} catch {
return false;
}
}
function isObject(value) {
return value !== null && typeof value === "object";
}
function isArray(value) {
return Array.isArray(value);
}
function isFunction(value) {
return typeof value === "function";
}
function isDate(value) {
return value instanceof Date;
}
function isRegExp(value) {
return value instanceof RegExp;
}
function isError(value) {
return value instanceof Error || value !== null && typeof value === "object" && "name" in value && "message" in value && "stack" in value;
}
function isPromise(value) {
return value instanceof Promise || value !== null && typeof value === "object" && "then" in value && typeof value.then === "function";
}
function isArguments(value) {
return Object.prototype.toString.call(value) === "[object Arguments]";
}
function isBuffer(value) {
return typeof Buffer !== "undefined" && value !== null && typeof value === "object" && "isBuffer" in Buffer && Buffer.isBuffer(value);
}
function isPlainObject(value) {
if (!isObject(value)) return false;
const proto = Object.getPrototypeOf(value);
if (proto === null) return true;
const Ctor = Object.prototype.hasOwnProperty.call(proto, "constructor") && proto.constructor;
return typeof Ctor === "function" && Ctor instanceof Ctor && Function.prototype.toString.call(Ctor) === Function.prototype.toString.call(Object);
}
function isEmpty(value) {
if (value === null || value === void 0) return true;
if (typeof value === "string" || Array.isArray(value)) return value.length === 0;
if (value instanceof Map || value instanceof Set) return value.size === 0;
if (isPlainObject(value)) return Object.keys(value).length === 0;
return false;
}
function isEmptyArray(value) {
return Array.isArray(value) && value.length === 0;
}
function isEmptyObject(value) {
return isPlainObject(value) && Object.keys(value).length === 0;
}
function hasLength(value) {
return value !== null && value !== void 0 && (typeof value === "object" || typeof value === "string") && (typeof value === "string" || "length" in value) && typeof value.length === "number";
}
function hasSize(value) {
return value !== null && value !== void 0 && typeof value === "object" && "size" in value && typeof value.size === "number";
}
function isArrayLike(value) {
return value !== null && value !== void 0 && typeof value !== "function" && hasLength(value) && value.length >= 0 && value.length <= Number.MAX_SAFE_INTEGER;
}
function isIterable(value) {
return value !== null && value !== void 0 && typeof value[Symbol.iterator] === "function";
}
function isAsyncIterable(value) {
return value !== null && value !== void 0 && typeof value[Symbol.asyncIterator] === "function";
}
function isConstructor(value) {
if (typeof value !== "function") return false;
try {
const testObj = {};
const BoundTest = value.bind(testObj);
new BoundTest();
return true;
} catch {
return false;
}
}
function isThenable$1(value) {
return value !== null && (typeof value === "object" || typeof value === "function") && "then" in value && typeof value.then === "function";
}
function isObservable(value) {
return value !== null && typeof value === "object" && "subscribe" in value && typeof value.subscribe === "function";
}
function isGenerator(value) {
return value !== null && typeof value === "object" && typeof value.next === "function" && typeof value.throw === "function" && typeof value.return === "function";
}
function isAsyncGenerator(value) {
return value !== null && typeof value === "object" && typeof value.next === "function" && typeof value.throw === "function" && typeof value.return === "function" && isAsyncIterable(value);
}
function isGeneratorFunction(value) {
if (typeof value !== "function") return false;
const name = value.constructor?.name;
return name === "GeneratorFunction" || /^function\s*\*/.test(value.toString());
}
function isAsyncFunction(value) {
if (typeof value !== "function") return false;
const name = value.constructor?.name;
return name === "AsyncFunction" || /^async\s/.test(value.toString());
}
function isAsyncGeneratorFunction(value) {
if (typeof value !== "function") return false;
const name = value.constructor?.name;
return name === "AsyncGeneratorFunction" || /^async\s*function\s*\*/.test(value.toString());
}
function isProxy$1(value) {
try {
if (typeof value !== "object" && typeof value !== "function") return false;
if (value === null) return false;
const testHandler = {
get() {
throw new Error("proxy trap");
}
};
const proxy = new Proxy({}, testHandler);
const isProxyLike = Object.prototype.toString.call(value) === Object.prototype.toString.call(proxy);
return isProxyLike;
} catch {
return false;
}
}
function isTypeError(value) {
return value instanceof TypeError;
}
function isRangeError(value) {
return value instanceof RangeError;
}
function isSyntaxError(value) {
return value instanceof SyntaxError;
}
function isReferenceError(value) {
return value instanceof ReferenceError;
}
function isEvalError(value) {
return value instanceof EvalError;
}
function isURIError(value) {
return value instanceof URIError;
}
function isStream$1(value) {
return value !== null && typeof value === "object" && typeof value.pipe === "function";
}
function isEventEmitter$1(value) {
return value !== null && typeof value === "object" && typeof value.on === "function" && typeof value.emit === "function" && typeof value.removeListener === "function";
}
function isElement(value) {
return typeof Element !== "undefined" && value instanceof Element;
}
function isNode(value) {
return typeof Node !== "undefined" && value instanceof Node;
}
function isWindow(value) {
return typeof Window !== "undefined" && value instanceof Window;
}
function isDocument(value) {
return typeof Document !== "undefined" && value instanceof Document;
}
function isGlobal$1(value) {
if (typeof globalThis !== "undefined" && value === globalThis) return true;
if (typeof global !== "undefined" && value === global) return true;
if (typeof window !== "undefined" && value === window) return true;
if (typeof self !== "undefined" && value === self) return true;
return false;
}
function isMap(value) {
return value instanceof Map;
}
function isSet(value) {
return value instanceof Set;
}
function isWeakMap(value) {
return value instanceof WeakMap;
}
function isWeakSet(value) {
return value instanceof WeakSet;
}
function isDataView(value) {
return value instanceof DataView;
}
function isArrayBuffer(value) {
return value instanceof ArrayBuffer;
}
function isSharedArrayBuffer(value) {
return typeof SharedArrayBuffer !== "undefined" && value instanceof SharedArrayBuffer;
}
function isTypedArray(value) {
return ArrayBuffer.isView(value) && !(value instanceof DataView);
}
function isInt8Array(value) {
return value instanceof Int8Array;
}
function isUint8Array(value) {
return value instanceof Uint8Array;
}
function isUint8ClampedArray(value) {
return value instanceof Uint8ClampedArray;
}
function isInt16Array(value) {
return value instanceof Int16Array;
}
function isUint16Array(value) {
return value instanceof Uint16Array;
}
function isInt32Array(value) {
return value instanceof Int32Array;
}
function isUint32Array(value) {
return value instanceof Uint32Array;
}
function isFloat32Array(value) {
return value instanceof Float32Array;
}
function isFloat64Array(value) {
return value instanceof Float64Array;
}
function isBigInt64Array(value) {
return typeof BigInt64Array !== "undefined" && value instanceof BigInt64Array;
}
function isBigUint64Array(value) {
return typeof BigUint64Array !== "undefined" && value instanceof BigUint64Array;
}
const TYPE_TAG_MAP = {
"[object Arguments]": "arguments",
"[object Array]": "array",
"[object ArrayBuffer]": "arraybuffer",
"[object AsyncFunction]": "asyncfunction",
"[object AsyncGeneratorFunction]": "asyncgeneratorfunction",
"[object BigInt]": "bigint",
"[object BigInt64Array]": "bigint64array",
"[object BigUint64Array]": "biguint64array",
"[object Boolean]": "boolean",
"[object DataView]": "dataview",
"[object Date]": "date",
"[object Error]": "error",
"[object Float32Array]": "float32array",
"[object Float64Array]": "float64array",
"[object Function]": "function",
"[object GeneratorFunction]": "generatorfunction",
"[object Int8Array]": "int8array",
"[object Int16Array]": "int16array",
"[object Int32Array]": "int32array",
"[object Map]": "map",
"[object Number]": "number",
"[object Object]": "object",
"[object Promise]": "promise",
"[object Proxy]": "proxy",
"[object RegExp]": "regexp",
"[object Set]": "set",
"[object SharedArrayBuffer]": "sharedarraybuffer",
"[object String]": "string",
"[object Symbol]": "symbol",
"[object Uint8Array]": "uint8array",
"[object Uint8ClampedArray]": "uint8clampedarray",
"[object Uint16Array]": "uint16array",
"[object Uint32Array]": "uint32array",
"[object WeakMap]": "weakmap",
"[object WeakSet]": "weakset",
"[object Window]": "window",
"[object HTMLDocument]": "document",
"[object Document]": "document",
"[object Null]": "null",
"[object Undefined]": "undefined"
};
const toString = Object.prototype.toString;
const toStringTag = Symbol.toStringTag;
function getNativeType(value) {
if (value === void 0) return "undefined";
if (value === null) return "null";
const primitiveType = typeof value;
switch (primitiveType) {
case "boolean":
return "boolean";
case "number":
return "number";
case "string":
return "string";
case "symbol":
return "symbol";
case "bigint":
return "bigint";
case "function":
return getFunctionType(value);
case "object":
return getObjectType(value);
default:
return null;
}
}
function getFunctionType(fn) {
const fnString = fn.toString();
if (/^class\s/.test(fnString)) {
return "function";
}
if (/^async\s*function\s*\*/.test(fnString) || /^async\s*\*/.test(fnString)) {
return "asyncgeneratorfunction";
}
if (/^async\s/.test(fnString)) {
return "asyncfunction";
}
if (/^function\s*\*/.test(fnString) || /^\*/.test(fnString)) {
return "generatorfunction";
}
const ctorName = fn.constructor?.name;
if (ctorName === "AsyncFunction") return "asyncfunction";
if (ctorName === "GeneratorFunction") return "generatorfunction";
if (ctorName === "AsyncGeneratorFunction") return "asyncgeneratorfunction";
return "function";
}
function getObjectType(obj) {
const tag = toString.call(obj);
const mappedType = TYPE_TAG_MAP[tag];
if (mappedType) {
return mappedType;
}
if (typeof Buffer !== "undefined" && Buffer.isBuffer && Buffer.isBuffer(obj)) {
return "buffer";
}
if (typeof obj === "object" && "callee" in obj && typeof obj.callee === "function") {
return "arguments";
}
if (isStream(obj)) {
return "stream";
}
if (isEventEmitter(obj)) {
return "eventemitter";
}
if (tag === "[object Object]") {
const customTag = obj[toStringTag];
if (typeof customTag === "string") {
return customTag.toLowerCase();
}
const ctor = obj.constructor;
if (ctor && ctor !== Object) {
const ctorName = ctor.name;
if (ctorName && ctorName !== "Object") {
return ctorName.toLowerCase();
}
}
}
if (isDOMElement(obj)) {
return "element";
}
if (isDOMNode(obj)) {
return "node";
}
if (isGlobal(obj)) {
return "global";
}
return "object";
}
function isStream(obj) {
return obj !== null && typeof obj === "object" && typeof obj.pipe === "function";
}
function isEventEmitter(obj) {
return obj !== null && typeof obj === "object" && typeof obj.on === "function" && typeof obj.emit === "function" && typeof obj.removeListener === "function";
}
function isDOMElement(obj) {
if (typeof HTMLElement === "undefined") return false;
return obj instanceof HTMLElement;
}
function isDOMNode(obj) {
if (typeof Node === "undefined") return false;
return obj instanceof Node;
}
function isGlobal(obj) {
if (typeof globalThis !== "undefined" && obj === globalThis) return true;
if (typeof global !== "undefined" && obj === global) return true;
if (typeof window !== "undefined" && obj === window) return true;
if (typeof self !== "undefined" && obj === self) return true;
return false;
}
const WeakMapPrototype = WeakMap.prototype;
const WeakSetPrototype = WeakSet.prototype;
const MapPrototype = Map.prototype;
const SetPrototype = Set.prototype;
const ArrayBufferPrototype = ArrayBuffer.prototype;
const DataViewPrototype = DataView.prototype;
const SharedArrayBufferPrototype = typeof SharedArrayBuffer !== "undefined" ? SharedArrayBuffer.prototype : null;
function checkModernType(value) {
if (value === null || value === void 0) {
return null;
}
try {
if (value instanceof Promise || isThenable(value)) {
return "promise";
}
if (value instanceof Map || value.__proto__ === MapPrototype) {
return "map";
}
if (value instanceof Set || value.__proto__ === SetPrototype) {
return "set";
}
if (value instanceof WeakMap || value.__proto__ === WeakMapPrototype) {
return "weakmap";
}
if (value instanceof WeakSet || value.__proto__ === WeakSetPrototype) {
return "weakset";
}
if (value instanceof ArrayBuffer || value.__proto__ === ArrayBufferPrototype) {
return "arraybuffer";
}
if (SharedArrayBufferPrototype && (value instanceof SharedArrayBuffer || value.__proto__ === SharedArrayBufferPrototype)) {
return "sharedarraybuffer";
}
if (value instanceof DataView || value.__proto__ === DataViewPrototype) {
return "dataview";
}
if (isProxy(value)) {
return "proxy";
}
} catch {
}
return null;
}
function isThenable(value) {
return value !== null && (typeof value === "object" || typeof value === "function") && typeof value.then === "function";
}
function isProxy(value) {
try {
if (typeof value !== "object" && typeof value !== "function") {
return false;
}
new Proxy({}, value);
const descriptor = Object.getOwnPropertyDescriptor(value, Symbol.toStringTag);
if (descriptor && !descriptor.configurable && descriptor.value === "Proxy") {
return true;
}
return false;
} catch {
return false;
}
}
function getTypedArrayType(value) {
if (!ArrayBuffer.isView(value)) {
return null;
}
const proto = Object.getPrototypeOf(value);
const ctorName = proto?.constructor?.name;
switch (ctorName) {
case "Int8Array":
return "int8array";
case "Uint8Array":
return "uint8array";
case "Uint8ClampedArray":
return "uint8clampedarray";
case "Int16Array":
return "int16array";
case "Uint16Array":
return "uint16array";
case "Int32Array":
return "int32array";
case "Uint32Array":
return "uint32array";
case "Float32Array":
return "float32array";
case "Float64Array":
return "float64array";
case "BigInt64Array":
return "bigint64array";
case "BigUint64Array":
return "biguint64array";
default:
return null;
}
}
let typeCache = null;
let cacheEnabled = true;
function kindOfCore(value) {
if (value === void 0) return "undefined";
if (value === null) return "null";
const primitiveType = getPrimitiveType(value);
if (primitiveType) return primitiveType;
if (typeof value === "object" && cacheEnabled && typeCache) {
const cached = typeCache.get(value);
if (cached) return cached;
}
let type = getNativeType(value);
if (!type || type === "object") {
const modernType = checkModernType(value);
if (modernType) {
type = modernType;
} else {
const arrayType = getTypedArrayType(value);
if (arrayType) {
type = arrayType;
} else {
type = getCustomType(value) || type || "object";
}
}
}
if (typeof value === "object" && cacheEnabled && type) {
if (!typeCache) typeCache = /* @__PURE__ */ new WeakMap();
typeCache.set(value, type);
}
return type || "object";
}
function getPrimitiveType(value) {
const type = typeof value;
switch (type) {
case "boolean":
case "number":
case "string":
case "symbol":
case "bigint":
return type;
default:
return null;
}
}
function getCustomType(value) {
try {
const toStringTag = value[Symbol.toStringTag];
if (typeof toStringTag === "string") {
return toStringTag.toLowerCase();
}
if (value.constructor && value.constructor !== Object) {
const ctorName = value.constructor.name;
if (ctorName && ctorName !== "Object") {
return ctorName.toLowerCase();
}
}
} catch {
}
return null;
}
function isType(value, type) {
return kindOfCore(value) === type;
}
function assertType(value, type) {
if (kindOfCore(value) !== type) {
throw new TypeError(`Expected type "${type}" but got "${kindOfCore(value)}"`);
}
}
function ensureType(value, type, defaultValue) {
return isType(value, type) ? value : defaultValue;
}
exports.assertType = assertType;
exports.ensureType = ensureType;
exports.hasLength = hasLength;
exports.hasSize = hasSize;
exports.isArguments = isArguments;
exports.isArray = isArray;
exports.isArrayBuffer = isArrayBuffer;
exports.isArrayLike = isArrayLike;
exports.isAsyncFunction = isAsyncFunction;
exports.isAsyncGenerator = isAsyncGenerator;
exports.isAsyncGeneratorFunction = isAsyncGeneratorFunction;
exports.isAsyncIterable = isAsyncIterable;
exports.isBigInt = isBigInt;
exports.isBigInt64Array = isBigInt64Array;
exports.isBigUint64Array = isBigUint64Array;
exports.isBoolean = isBoolean;
exports.isBuffer = isBuffer;
exports.isConstructor = isConstructor;
exports.isDataView = isDataView;
exports.isDate = isDate;
exports.isDocument = isDocument;
exports.isElement = isElement;
exports.isEmpty = isEmpty;
exports.isEmptyArray = isEmptyArray;
exports.isEmptyObject = isEmptyObject;
exports.isEmptyString = isEmptyString;
exports.isError = isError;
exports.isEvalError = isEvalError;
exports.isEventEmitter = isEventEmitter$1;
exports.isFalsy = isFalsy;
exports.isFinite = isFinite;
exports.isFloat32Array = isFloat32Array;
exports.isFloat64Array = isFloat64Array;
exports.isFunction = isFunction;
exports.isGenerator = isGenerator;
exports.isGeneratorFunction = isGeneratorFunction;
exports.isGlobal = isGlobal$1;
exports.isInfinity = isInfinity;
exports.isInt16Array = isInt16Array;
exports.isInt32Array = isInt32Array;
exports.isInt8Array = isInt8Array;
exports.isInteger = isInteger;
exports.isIterable = isIterable;
exports.isJsonString = isJsonString;
exports.isMap = isMap;
exports.isNaN = isNaN;
exports.isNode = isNode;
exports.isNull = isNull;
exports.isNullish = isNullish;
exports.isNumber = isNumber;
exports.isNumericString = isNumericString;
exports.isObject = isObject;
exports.isObservable = isObservable;
exports.isPlainObject = isPlainObject;
exports.isPrimitive = isPrimitive;
exports.isPromise = isPromise;
exports.isProxy = isProxy$1;
exports.isRangeError = isRangeError;
exports.isReferenceError = isReferenceError;
exports.isRegExp = isRegExp;
exports.isSafeInteger = isSafeInteger;
exports.isSet = isSet;
exports.isSharedArrayBuffer = isSharedArrayBuffer;
exports.isStream = isStream$1;
exports.isString = isString;
exports.isSymbol = isSymbol;
exports.isSyntaxError = isSyntaxError;
exports.isThenable = isThenable$1;
exports.isTruthy = isTruthy;
exports.isType = isType;
exports.isTypeError = isTypeError;
exports.isTypedArray = isTypedArray;
exports.isURIError = isURIError;
exports.isUint16Array = isUint16Array;
exports.isUint32Array = isUint32Array;
exports.isUint8Array = isUint8Array;
exports.isUint8ClampedArray = isUint8ClampedArray;
exports.isUndefined = isUndefined;
exports.isWeakMap = isWeakMap;
exports.isWeakSet = isWeakSet;
exports.isWindow = isWindow;
//# sourceMappingURL=index.cjs.map