{"name":"is-what","version":"3.1.2","keywords":["javascript","typescript","typechecker","check-type"],"author":{"name":"Luca Ban - Mesqueeb"},"license":"MIT","_id":"is-what@3.1.2","maintainers":[{"name":"mesqueeb","email":"mesqueeb@gmail.com"}],"homepage":"https://github.com/mesqueeb/is-what#readme","bugs":{"url":"https://github.com/mesqueeb/is-what/issues"},"dist":{"shasum":"988de1c66e43a6f26a2ee5b8a9c99fa8b91dce7d","tarball":"http://registry-npm.ywwl.com/is-what/download/is-what-3.1.2.tgz","fileCount":12,"integrity":"sha512-Vq0dNCSDGxl7KnquuKqkVaOHOCllLn9zm7WObKckKI8F1I5ATH6erjeqhSVag0rTv4n+J8+hSGtuDaCIM2q/qA==","signatures":[{"sig":"MEUCIQD1j6oayT1iJ00p55EZze5gIu7MzEN/FS+TzywrHWe35QIgLK31kFulgEIV99XRondG8DSUd9rw2pefvc01Z4wenxQ=","keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA"}],"unpackedSize":35116,"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v3.0.4\r\nComment: https://openpgpjs.org\r\n\r\nwsFcBAEBCAAQBQJcNhUuCRA9TVsSAnZWagAA3b0QAIkxQF7Mjev42nurQoG6\nNG1bt+RK2MoSl1cAhRGeIxMG5Z6EafrPRiaC0jrRSSIsYJfzOckNvDOhquC5\nlssSKKRHxW/JT0JzCOAVE6Umij3g++gWYmlMfqnEGn7yC8xL/MnLQ69Qdoac\n1q3t53OtV08UUTiiUU0AaWvEJhkKppfnl7Jg6Oj0w6b2yAN1CTr9cF468r+F\ntWJt9ylWFpug8tYrgS5OvCGMfLRa7jUPAtVujkaX8gzq90fUdIVMXhrXkzai\nFo2D6lzUjgLCRrMeNELW/K4bwReUAzjn+b7+gRc6wvxexMpjJcUC4s4QUfVf\n9qYRVUIRWKOihNy6VWksVMF2Be0fIaFCl4mA+URhnSufjmhkvG4ktghC0MgZ\ntsJEigTp+v2OdO87ytRzO4dPXi+L+F0cVKs8niGlZnAY8as5ss6ogL8nb/oT\n1Hr1cl4RJ6veyOiT3zB6zn3vhpfs9ROAQzr4XPh3UwD8Uy1lRFlV3yLnOdWn\nll/5J0BC2HJJoAmpEfbkAqqFE5I43Eqsn9bQA2lQtNRFa+4XFkI5PUNMbECc\nLt+pwy727+6IAFigPCvrKPzjRm6DLuoB6fYHa1bRKdsYG2HNgKpeZiVVmub1\n0G0Z5IkDi4TPQCWAYss6kcN7c95uAMe+KaDAj3pV6yBge6cS5+qPH121X1DC\nIpjL\r\n=ckb7\r\n-----END PGP SIGNATURE-----\r\n","size":7357,"noattachment":false,"key":"/is-what/-/is-what-3.1.2.tgz"},"main":"dist/index.cjs.js","module":"dist/index.esm.js","gitHead":"c15d7dc3b5192e9b66c0950b22ce1d98adad4836","scripts":{"test":"jest","build":"rollup -c ./build/rollup.js && npm run test","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.4.1","description":"JS type check (TypeScript supported) functions like `isPlainObject() isArray()` etc. A simple & small integration.","directories":{},"_nodeVersion":"10.10.0","dependencies":{},"_hasShrinkwrap":false,"devDependencies":{"jest":"^23.6.0","rollup":"^0.65.2","babel-core":"^7.0.0-bridge.0","babel-jest":"^23.6.0","typescript":"^3.1.6","@babel/core":"^7.1.6","@types/jest":"^23.3.10","babel-preset-env":"^1.7.0","@types/babel-core":"^6.25.5","regenerator-runtime":"^0.12.1","rollup-plugin-typescript2":"^0.17.2"},"_npmOperationalInternal":{"tmp":"tmp/is-what_3.1.2_1547048238032_0.8772549179885865","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":1547048238157,"_cnpm_publish_time":1547048238157,"dist-tags":{"latest":"5.5.7","next":"4.1.15-3"}}