{"name":"is-what","version":"3.11.0","keywords":["javascript","typescript","typechecker","check-type","javascript-type","primitive-types","plain-object","plain-objects","class-instance","class-identifier","type-checking","type-checker","type-check","define-type","get-type","what-type","is-object","is-plain-obj","is-plain-object"],"author":{"name":"Luca Ban - Mesqueeb"},"license":"MIT","_id":"is-what@3.11.0","maintainers":[{"name":"mesqueeb","email":"mesqueeb@gmail.com"}],"homepage":"https://github.com/mesqueeb/is-what#readme","bugs":{"url":"https://github.com/mesqueeb/is-what/issues"},"ava":{"require":["tsconfig-paths/register","ts-node/register"],"extensions":["ts"]},"dist":{"shasum":"87c51e480f215e3261010c216b9c89c922ec3366","tarball":"http://registry-npm.ywwl.com/is-what/download/is-what-3.11.0.tgz","fileCount":16,"integrity":"sha512-70wGVRNNT4DHVK9ZEcQmA3dMUW04Rdnlr5TDrcq/qN/7nMGY/2KIi5wwP7RhaNiIoPNeFyuvL8gaP+SRRP72OA==","signatures":[{"sig":"MEUCICD4evuHrn81N810ra8hOvb0jZU/pXaoXBm3QGbh5EWhAiEA7fGaV1mU6agIpwsyhc8JAZ1xDf6Q0cYvjC9wKS5awrk=","keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA"}],"unpackedSize":58986,"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v3.0.4\r\nComment: https://openpgpjs.org\r\n\r\nwsFcBAEBCAAQBQJfI9VGCRA9TVsSAnZWagAAXZAP/1W2guSEQaJX0YTHAbgA\n4MV+AkaV9dkWPa8suaQH+rpHl8GOSoN38VhV4zskTAdlW8RFnutDWTltiL0+\nAX4yKoI+OyFXTOSuf7i7oXdQihyj9MJ2M19SCkvLI760xVXGrrxBX+XHZKPN\nmnkBrVv8zch1N5HfNHyFt91l8q85hmhftuVtcWifZ3X5F4O5OFet0iJ4Iv6v\nO+0E7tgplYd5Pff6Ca9zLR/BAiliD0JS4c5syQ5jD/JmpUZm3hsnQVefZsq8\n/aAGqcPpCClWMlGUm0dunLh3sfzrczTnGmU1M4pM/uxRvWIHJ4sEApYkdzOK\n/jREd/u+H7JJsUMIaJDqe+DABSVZMTIijhGWsxrzoSEK6n7x5r2+w5gW0eDD\n3Bznn96dpqdR4YHyi3zI6YgCAyOjd2RAst7uB5RdeYiiIwOse11v5Yz5Q9pa\nWUX8VX1Iuiqk7WNvQ4FsGcLbKh6jOSXKkAwp2p31zikWEweUZmlAsgq6VWI5\nxyxdmt3f0AkoMJQKwBT5mVvMW0s/ffv/u3fgs9mXbqOnbzV13YlE33z5BNlI\nlRIq+E0syFO6CKJxUCPFh6wpiwRg+04AoRRqTZSuBSi4zobRa+CVjrFNP7sa\nbU0tXqhC2E2HQBoGPkf0uPg2VL163yd4wf7G9KJgSMh5IoTI5QzN6SUrxE6L\nMGun\r\n=Crk8\r\n-----END PGP SIGNATURE-----\r\n","size":11183,"noattachment":false,"key":"/is-what/-/is-what-3.11.0.tgz"},"main":"dist/index.cjs.js","module":"dist/index.esm.js","gitHead":"906aeb8d8404e2235b02d5d83fe844970735a7d5","scripts":{"ava":"ava","lint":"eslint . --ext .js,.jsx,.ts,.tsx","test":"jest","build":"npm run lint && npm run rollup && npm run test","rollup":"rollup -c ./build/rollup.js","test-w":"jest --watchAll"},"typings":"types/index.d.ts","_npmUser":{"name":"mesqueeb","email":"mesqueeb@gmail.com"},"repository":{"url":"git+https://github.com/mesqueeb/is-what.git","type":"git"},"_npmVersion":"6.14.5","description":"JS type check (TypeScript supported) functions like `isPlainObject() isArray()` etc. A simple & small integration.","directories":{},"sideEffects":false,"_nodeVersion":"14.5.0","dependencies":{},"_hasShrinkwrap":false,"devDependencies":{"ava":"^3.11.0","jest":"^25.5.4","eslint":"^6.8.0","rollup":"^1.32.1","ts-node":"^8.10.2","babel-core":"^7.0.0-bridge.0","babel-jest":"^25.5.1","typescript":"^3.9.7","@babel/core":"^7.11.0","@types/jest":"^25.2.3","tsconfig-paths":"^3.9.0","babel-preset-env":"^1.7.0","@types/babel-core":"^6.25.6","regenerator-runtime":"^0.13.7","eslint-config-prettier":"^6.11.0","@typescript-eslint/parser":"^2.34.0","rollup-plugin-typescript2":"^0.27.1","eslint-plugin-tree-shaking":"^1.8.0","@typescript-eslint/eslint-plugin":"^2.34.0"},"_npmOperationalInternal":{"tmp":"tmp/is-what_3.11.0_1596183876957_0.257392067991409","host":"s3://npm-registry-packages"},"readme":"# is What? 🙉\n\n<a href=\"https://www.npmjs.com/package/is-what\"><img src=\"https://img.shields.io/npm/v/is-what.svg\" alt=\"Total Downloads\"></a>\n<a href=\"https://www.npmjs.com/package/is-what\"><img src=\"https://img.shields.io/npm/dw/is-what.svg\" alt=\"Latest Stable Version\"></a>\n\nVery simple & small JS type check functions. It's fully TypeScript supported!\n\n```\nnpm i is-what\n```\n\nOr for deno available at: `\"deno.land/x/is_what\"`\n\n> Also check out [is-where 🙈](https://github.com/mesqueeb/is-where)\n\n## Motivation\n\nI built is-what because the existing solutions were all too complex or too poorly built.\n\nI was looking for:\n\n- A simple way to check any kind of type (including non-primitives)\n- Be able to check if an object is a plain object `{}` or a special object (like a class instance) ‼️\n- Let TypeScript automatically know what type a value is when checking\n\nAnd that's exactly what `is-what` is! (what a great wordplay 😃)\n\n## Usage\n\nis-what is really easy to use, and most functions work just like you'd expect.\n\n```js\n// import functions you want to use like so:\nimport { isString, isDate, isPlainObject } from 'is-what'\n```\n\n1. First I'll go over the simple functions available. Only `isNumber` and `isDate` have special treatment.\n2. After that I'll talk about working with Objects (plain objects vs class instances etc.).\n3. Lastly I'll talk about TypeScript implementation\n\n### Simple type check functions\n\n```js\n// basics\nisBoolean(true) // true\nisBoolean(false) // true\nisUndefined(undefined) // true\nisNull(null) // true\n\n// strings\nisString('') // true\nisEmptyString('') // true\nisFullString('') // false\n\n// numbers\nisNumber(0) // true\nisNumber('0') // false\nisNumber(NaN) // false *\nisPositiveNumber(1) // true\nisNegativeNumber(-1) // true\n// * see below for special NaN use cases!\n\n// arrays\nisArray([]) // true\nisEmptyArray([]) // true\nisFullArray([1]) // true\n\n// objects\nisPlainObject({}) // true *\nisEmptyObject({}) // true\nisFullObject({ a: 1 }) // true\n// * see below for special object (& class instance) use cases!\n\n// functions\nisFunction(function () {}) // true\nisFunction(() => {}) // true\n\n// dates\nisDate(new Date()) // true\nisDate(new Date('invalid date')) // false\n\n// maps & sets\nisMap(new Map()) // true\nisSet(new Set()) // true\nisWeakMap(new WeakMap()) // true\nisWeakSet(new WeakSet()) // true\n\n// others\nisRegExp(/\\s/gi) // true\nisSymbol(Symbol()) // true\nisBlob(new Blob()) // true\nisFile(new File([''], '', { type: 'text/html' })) // true\nisError(new Error('')) // true\nisPromise(new Promise((resolve) => {})) // true\n\n// primitives\nisPrimitive('') // true\n// true for any of: boolean, null, undefined, number, string, symbol\n```\n\n### Let's talk about NaN\n\n`isNaN` is a built-in JS Function but it really makes no sense:\n\n```js\n// 1)\ntypeof NaN === 'number' // true\n// 🤔 (\"not a number\" is a \"number\"...)\n\n// 2)\nisNaN('1') // false\n// 🤔 the string '1' is not-\"not a number\"... so it's a number??\n\n// 3)\nisNaN('one') // true\n// 🤔 'one' is NaN but `NaN === 'one'` is false...\n```\n\nWith is-what the way we treat NaN makes a little bit more sense:\n\n```js\nimport { isNumber, isNaNValue } from 'is-what'\n\n// 1)\nisNumber(NaN) // false!\n// let's not treat NaN as a number\n\n// 2)\nisNaNValue('1') // false\n// if it's not NaN, it's not NaN!!\n\n// 3)\nisNaNValue('one') // false\n// if it's not NaN, it's not NaN!!\n\nisNaNValue(NaN) // true\n```\n\n### isPlainObject vs isAnyObject\n\nChecking for a JavaScript object can be really difficult. In JavaScript you can create classes that will behave just like JavaScript objects but might have completely different prototypes. With is-what I went for this classification:\n\n- `isPlainObject` will only return `true` on plain JavaScript objects and not on classes or others\n- `isAnyObject` will be more loose and return `true` on regular objects, classes, etc.\n\n```js\n// define a plain object\nconst plainObject = { hello: 'I am a good old object.' }\n\n// define a special object\nclass SpecialObject {\n  constructor(somethingSpecial) {\n    this.speciality = somethingSpecial\n  }\n}\nconst specialObject = new SpecialObject('I am a special object! I am a class instance!!!')\n\n// check the plain object\nisPlainObject(plainObject) // returns true\nisAnyObject(plainObject) // returns true\ngetType(plainObject) // returns 'Object'\n\n// check the special object\nisPlainObject(specialObject) // returns false !!!!!!!!!\nisAnyObject(specialObject) // returns true\ngetType(specialObject) // returns 'Object'\n```\n\n> Please note that `isPlainObject` will only return `true` for normal plain JavaScript objects.\n\n### Getting and checking for specific types\n\nYou can check for specific types with `getType` and `isType`:\n\n```js\nimport { getType, isType } from 'is-what'\n\ngetType('') // returns 'String'\n// pass a Type as second param:\nisType('', String) // returns true\n```\n\nIf you just want to make sure your object _inherits_ from a particular class or\n`toStringTag` value, you can use `isInstanceOf()` like this:\n\n```js\nimport { isInstanceOf } from 'is-what'\n\nisInstanceOf(new XMLHttpRequest(), 'EventTarget')\n// returns true\nisInstanceOf(globalThis, ReadableStream)\n// returns false\n```\n\n## TypeScript\n\nis-what makes TypeScript know the type during if statements. This means that a check returns the type of the payload for TypeScript users.\n\n```ts\nfunction isNumber(payload: unknown): payload is number {\n  // return boolean\n}\n// As you can see above, all functions return a boolean for JavaScript, but pass the payload type to TypeScript.\n\n// usage example:\nfunction fn(payload: string | number): number {\n  if (isNumber(payload)) {\n    // ↑ TypeScript already knows payload is a number here!\n    return payload\n  }\n  return 0\n}\n```\n\n`isPlainObject` and `isAnyObject` with TypeScript will declare the payload to be an object type with any props:\n\n```ts\nfunction isPlainObject(payload: unknown): payload is { [key: string]: unknown }\nfunction isAnyObject(payload: unknown): payload is { [key: string]: unknown }\n// The reason to return `{[key: string]: unknown}` is to be able to do\nif (isPlainObject(payload) && payload.id) return payload.id\n// if isPlainObject() would return `payload is object` then it would give an error at `payload.id`\n```\n\n### isObjectLike\n\nIf you want more control over what kind of interface/type is casted when checking for objects.\n\nTo cast to a specific type while checking for `isAnyObject`, can use `isObjectLike<T>`:\n\n```ts\nimport { isObjectLike } from 'is-what'\n\nconst payload = { name: 'Mesqueeb' } // current type: `{ name: string }`\n\n// Without casting:\nif (isAnyObject(payload)) {\n  // in here `payload` is casted to: `Record<string | number | symbol, unknown>`\n  // WE LOOSE THE TYPE!\n}\n\n// With casting:\n// you can pass a specific type for TS that will be casted when the function returns\nif (isObjectLike<{ name: string }>(payload)) {\n  // in here `payload` is casted to: `{ name: string }`\n}\n```\n\nPlease note: this library will not actually check the shape of the object, you need to do that yourself.\n\n`isObjectLike<T>` works like this under the hood:\n\n```ts\nfunction isObjectLike<T extends object>(payload: unknown): payload is T {\n  return isAnyObject(payload)\n}\n```\n\n## Meet the family (more tiny utils with TS support)\n\n- [is-what 🙉](https://github.com/mesqueeb/is-what)\n- [is-where 🙈](https://github.com/mesqueeb/is-where)\n- [merge-anything 🥡](https://github.com/mesqueeb/merge-anything)\n- [check-anything 👁](https://github.com/mesqueeb/check-anything)\n- [remove-anything ✂️](https://github.com/mesqueeb/remove-anything)\n- [getorset-anything 🐊](https://github.com/mesqueeb/getorset-anything)\n- [map-anything 🗺](https://github.com/mesqueeb/map-anything)\n- [filter-anything ⚔️](https://github.com/mesqueeb/filter-anything)\n- [copy-anything 🎭](https://github.com/mesqueeb/copy-anything)\n- [case-anything 🐫](https://github.com/mesqueeb/case-anything)\n- [flatten-anything 🏏](https://github.com/mesqueeb/flatten-anything)\n- [nestify-anything 🧅](https://github.com/mesqueeb/nestify-anything)\n\n## Source code\n\nIt's litterally just these functions:\n\n```js\nfunction getType(payload) {\n  return Object.prototype.toString.call(payload).slice(8, -1)\n}\nfunction isUndefined(payload) {\n  return getType(payload) === 'Undefined'\n}\nfunction isString(payload) {\n  return getType(payload) === 'String'\n}\nfunction isAnyObject(payload) {\n  return getType(payload) === 'Object'\n}\n// etc...\n```\n\nSee the full source code [here](https://github.com/mesqueeb/is-what/blob/main/src/index.ts).\n","publish_time":1596183877092,"_cnpm_publish_time":1596183877092,"dist-tags":{"latest":"5.5.7","next":"4.1.15-3"}}