index.js.map 11 KB

1
  1. {"version":3,"sources":["../src/index.ts","../src/helpers.ts"],"sourcesContent":["/* eslint-disable @typescript-eslint/ban-types */\nimport { getObjectType, isObjectOfType, isOfType, isPrimitiveType } from './helpers';\nimport type { Class, PlainObject, Primitive, TypeName } from './types';\n\nconst DOM_PROPERTIES_TO_CHECK: Array<keyof HTMLElement> = [\n 'innerHTML',\n 'ownerDocument',\n 'style',\n 'attributes',\n 'nodeValue',\n];\n\nfunction is(value: unknown): TypeName {\n if (value === null) {\n return 'null';\n }\n\n switch (typeof value) {\n case 'bigint':\n return 'bigint';\n case 'boolean':\n return 'boolean';\n case 'number':\n return 'number';\n case 'string':\n return 'string';\n case 'symbol':\n return 'symbol';\n case 'undefined':\n return 'undefined';\n default:\n }\n\n if (is.array(value)) {\n return 'Array';\n }\n\n if (is.plainFunction(value)) {\n return 'Function';\n }\n\n const tagType = getObjectType(value);\n\n if (tagType) {\n return tagType;\n }\n /* c8 ignore next 3 */\n\n return 'Object';\n}\n\nis.array = Array.isArray;\n\nis.arrayOf = (target: unknown[], predicate: (v: unknown) => boolean): boolean => {\n if (!is.array(target) && !is.function(predicate)) {\n return false;\n }\n\n return target.every(d => predicate(d));\n};\n\nis.asyncGeneratorFunction = (value: unknown): value is (...arguments_: any[]) => Promise<unknown> =>\n getObjectType(value) === 'AsyncGeneratorFunction';\n\nis.asyncFunction = isObjectOfType<Function>('AsyncFunction');\n\nis.bigint = isOfType<bigint>('bigint');\n\nis.boolean = (value: unknown): value is boolean => {\n return value === true || value === false;\n};\n\nis.date = isObjectOfType<Date>('Date');\n\nis.defined = (value: unknown): boolean => !is.undefined(value);\n\nis.domElement = (value: unknown): value is HTMLElement => {\n return (\n is.object(value) &&\n !is.plainObject(value) &&\n (value as HTMLElement).nodeType === 1 &&\n is.string((value as HTMLElement).nodeName) &&\n DOM_PROPERTIES_TO_CHECK.every(property => property in (value as HTMLElement))\n );\n};\n\nis.empty = (value: unknown): boolean => {\n return (\n (is.string(value) && value.length === 0) ||\n (is.array(value) && value.length === 0) ||\n (is.object(value) && !is.map(value) && !is.set(value) && Object.keys(value).length === 0) ||\n (is.set(value) && value.size === 0) ||\n (is.map(value) && value.size === 0)\n );\n};\n\nis.error = isObjectOfType<Error>('Error');\n\nis.function = isOfType<Function>('function');\n\nis.generator = (value: unknown): value is Generator => {\n return (\n is.iterable(value) &&\n is.function((value as IterableIterator<unknown>).next) &&\n is.function((value as IterableIterator<unknown>).throw)\n );\n};\n\nis.generatorFunction = isObjectOfType<GeneratorFunction>('GeneratorFunction');\n\nis.instanceOf = <T>(instance: unknown, class_: Class<T>): instance is T => {\n if (!instance || !(class_ as Class<T>)) {\n return false;\n }\n\n return Object.getPrototypeOf(instance) === class_.prototype;\n};\n\nis.iterable = (value: unknown): value is IterableIterator<unknown> => {\n return (\n !is.nullOrUndefined(value) && is.function((value as IterableIterator<unknown>)[Symbol.iterator])\n );\n};\n\nis.map = isObjectOfType<Map<unknown, unknown>>('Map');\n\nis.nan = (value: unknown): boolean => {\n return Number.isNaN(value as number);\n};\n\nis.null = (value: unknown): value is null => {\n return value === null;\n};\n\nis.nullOrUndefined = (value: unknown): value is null | undefined => {\n return is.null(value) || is.undefined(value);\n};\n\nis.number = (value: unknown): value is number => {\n return isOfType<number>('number')(value) && !is.nan(value);\n};\n\nis.numericString = (value: unknown): value is string => {\n return is.string(value) && (value as string).length > 0 && !Number.isNaN(Number(value));\n};\n\nis.object = (value: unknown): value is object => {\n return !is.nullOrUndefined(value) && (is.function(value) || typeof value === 'object');\n};\n\nis.oneOf = (target: unknown[], value: any): boolean => {\n if (!is.array(target)) {\n return false;\n }\n\n // eslint-disable-next-line unicorn/prefer-includes\n return target.indexOf(value) > -1;\n};\n\nis.plainFunction = isObjectOfType<Function>('Function');\n\nis.plainObject = (value: unknown): value is PlainObject => {\n if (getObjectType(value) !== 'Object') {\n return false;\n }\n\n const prototype = Object.getPrototypeOf(value);\n\n return prototype === null || prototype === Object.getPrototypeOf({});\n};\n\nis.primitive = (value: unknown): value is Primitive =>\n is.null(value) || isPrimitiveType(typeof value);\n\nis.promise = isObjectOfType<Promise<unknown>>('Promise');\n\nis.propertyOf = (\n target: PlainObject,\n key: string,\n predicate?: (v: unknown) => boolean,\n): boolean => {\n if (!is.object(target) || !key) {\n return false;\n }\n\n const value = target[key];\n\n if (is.function(predicate)) {\n return predicate(value);\n }\n\n return is.defined(value);\n};\n\nis.regexp = isObjectOfType<RegExp>('RegExp');\n\nis.set = isObjectOfType<Set<PlainObject>>('Set');\n\nis.string = isOfType<string>('string');\n\nis.symbol = isOfType<symbol>('symbol');\n\nis.undefined = isOfType<undefined>('undefined');\n\nis.weakMap = isObjectOfType<WeakMap<PlainObject, unknown>>('WeakMap');\n\nis.weakSet = isObjectOfType<WeakSet<PlainObject>>('WeakSet');\n\nexport default is;\n","/* eslint-disable @typescript-eslint/ban-types */\nimport type { ObjectTypes, Primitive, PrimitiveTypes } from './types';\n\nexport const objectTypes = [\n 'Array',\n 'ArrayBuffer',\n 'AsyncFunction',\n 'AsyncGenerator',\n 'AsyncGeneratorFunction',\n 'Date',\n 'Error',\n 'Function',\n 'Generator',\n 'GeneratorFunction',\n 'HTMLElement',\n 'Map',\n 'Object',\n 'Promise',\n 'RegExp',\n 'Set',\n 'WeakMap',\n 'WeakSet',\n] as const;\n\nexport const primitiveTypes = [\n 'bigint',\n 'boolean',\n 'null',\n 'number',\n 'string',\n 'symbol',\n 'undefined',\n] as const;\n\nexport function getObjectType(value: unknown): ObjectTypes | undefined {\n const objectTypeName = Object.prototype.toString.call(value).slice(8, -1);\n\n if (/HTML\\w+Element/.test(objectTypeName)) {\n return 'HTMLElement';\n }\n\n if (isObjectType(objectTypeName)) {\n return objectTypeName;\n }\n\n return undefined;\n}\n\nexport function isObjectOfType<T>(type: string) {\n return (value: unknown): value is T => getObjectType(value) === type;\n}\n\nexport function isObjectType(name: unknown): name is ObjectTypes {\n return objectTypes.includes(name as ObjectTypes);\n}\n\nexport function isOfType<T extends Primitive | Function>(type: string) {\n // eslint-disable-next-line valid-typeof\n return (value: unknown): value is T => typeof value === type;\n}\n\nexport function isPrimitiveType(name: unknown): name is PrimitiveTypes {\n return primitiveTypes.includes(name as PrimitiveTypes);\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACGO,IAAM,cAAc;AAAA,EACzB;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AACF;AAEO,IAAM,iBAAiB;AAAA,EAC5B;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AACF;AAEO,SAAS,cAAc,OAAyC;AACrE,QAAM,iBAAiB,OAAO,UAAU,SAAS,KAAK,KAAK,EAAE,MAAM,GAAG,EAAE;AAExE,MAAI,iBAAiB,KAAK,cAAc,GAAG;AACzC,WAAO;AAAA,EACT;AAEA,MAAI,aAAa,cAAc,GAAG;AAChC,WAAO;AAAA,EACT;AAEA,SAAO;AACT;AAEO,SAAS,eAAkB,MAAc;AAC9C,SAAO,CAAC,UAA+B,cAAc,KAAK,MAAM;AAClE;AAEO,SAAS,aAAa,MAAoC;AAC/D,SAAO,YAAY,SAAS,IAAmB;AACjD;AAEO,SAAS,SAAyC,MAAc;AAErE,SAAO,CAAC,UAA+B,OAAO,UAAU;AAC1D;AAEO,SAAS,gBAAgB,MAAuC;AACrE,SAAO,eAAe,SAAS,IAAsB;AACvD;;;AD3DA,IAAM,0BAAoD;AAAA,EACxD;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AACF;AAEA,SAAS,GAAG,OAA0B;AACpC,MAAI,UAAU,MAAM;AAClB,WAAO;AAAA,EACT;AAEA,UAAQ,OAAO,OAAO;AAAA,IACpB,KAAK;A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