{"name":"zwitch","version":"2.0.4","description":"Handle values based on a property","license":"MIT","keywords":["handle","switch","property"],"repository":{"type":"git","url":"git+https://github.com/wooorm/zwitch.git"},"bugs":{"url":"https://github.com/wooorm/zwitch/issues"},"funding":{"type":"github","url":"https://github.com/sponsors/wooorm"},"author":{"name":"Titus Wormer","email":"tituswormer@gmail.com","url":"https://wooorm.com"},"contributors":[{"name":"Titus Wormer","email":"tituswormer@gmail.com","url":"https://wooorm.com"}],"sideEffects":false,"type":"module","main":"index.js","types":"index.d.ts","devDependencies":{"@types/node":"^18.0.0","c8":"^7.0.0","prettier":"^2.0.0","remark-cli":"^11.0.0","remark-preset-wooorm":"^9.0.0","tsd":"^0.24.0","type-coverage":"^2.0.0","typescript":"^4.0.0","xo":"^0.52.0"},"scripts":{"prepack":"npm run build && npm run format","build":"tsc --build --clean && tsc --build && tsd && type-coverage","format":"remark . -qfo && prettier . -w --loglevel warn && xo --fix","test-api":"node --conditions development test.js","test-coverage":"c8 --check-coverage --100 --reporter lcov npm run test-api","test":"npm run build && npm run format && npm run test-coverage"},"prettier":{"tabWidth":2,"useTabs":false,"singleQuote":true,"bracketSpacing":false,"semi":false,"trailingComma":"none"},"xo":{"prettier":true},"remarkConfig":{"plugins":["preset-wooorm"]},"typeCoverage":{"atLeast":100,"detail":true,"strict":true,"ignoreFiles":["index.d.ts"]},"gitHead":"7a4992d2fd34772bf7caf3f755ba9a767cf79771","homepage":"https://github.com/wooorm/zwitch#readme","_id":"zwitch@2.0.4","_nodeVersion":"19.0.0","_npmVersion":"8.19.2","dist":{"integrity":"sha512-bXE4cR/kVZhKZX/RjPEflHaKVhUVl85noU3v6b8apfQEc1x4A+zBxjZ4lN8LqGd6WZ3dl98pY4o717VFmoPp+A==","shasum":"c827d4b0acb76fc3e685a4c6ec2902d51070e9d7","tarball":"http://registry-npm.ywwl.com/zwitch/download/zwitch-2.0.4.tgz","fileCount":5,"unpackedSize":13752,"signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEYCIQDVjIIedduzzuY8l+MPKOA7NpUsQGcTsg4Lg3EQgHXd0wIhAKBeuxXx7ZGAte4eZkfYnfzpFKSwIpr1+Sqkm89JtiOD"}],"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v4.10.10\r\nComment: https://openpgpjs.org\r\n\r\nwsFzBAEBCAAGBQJjdKi6ACEJED1NWxICdlZqFiEECWMYAoorWMhJKdjhPU1b\r\nEgJ2VmrgyQ/+I1Sq+0bMK4O9czlVsetS6XRAx3cun4zLTKKCqBh4t3g9+Wtq\r\n1U1+eje8+cwPEKGg1xgyIklku6bD4bKShaJ9dnblXHD8dZ49KgGvHKaGgGYh\r\ndFxTtgzADv2koAZ04D88IhXG533eaLfoRvB0Dgy4bmVO3GrczkQw1qtwIeyk\r\nd4V2MxsUDPBZTtqNIyA7k1vJSXQXKO4JECPlUzdDYfGOExKRI/30UnroJo7N\r\ngIGYKWfDAPXK52463586xvgVA2l/MwC6CAfKDIkPkhksQqy4ZmKtzNhkGrOn\r\nsSNuQQvlfcaq1JBAs+74G1duX0gV/qsfrjPf6PnBbvcCIJ4hroalLRRxKIVm\r\njC2CaSxzyWeMrcbt0mn+eojWHDzajf5Rq+fjw9x31KoJRQH5WrnPfvvBULDK\r\nE4yPiZTnMFXp5iPxZAB9PkbpICQG+33CRKnNfwcyqWTnnXQ2S+JFWOcING9b\r\nMjx2CQExdpCTGVwkoCB3m+Or2LVwe/OSry6g0HNfcUapbh45JApVPY+FBG2p\r\nrpCTwwc59m1rPeZx+YcyxNcqJtfHVvO/ZvXVgNI+U2Vf50MVdAiXNx9mvZLe\r\n0Xe2zARXF1FUaij8+/THRS71I0PrY8EumazcH/2iY93TKnla5C9W06KzmhVe\r\nMrBdeGcwj6KrqrBENdLIKnHm8QakXnvv35M=\r\n=RPgT\r\n-----END PGP SIGNATURE-----\r\n","size":4529,"noattachment":false,"key":"/zwitch/-/zwitch-2.0.4.tgz"},"_npmUser":{"name":"wooorm","email":"tituswormer@gmail.com"},"directories":{},"maintainers":[{"name":"wooorm","email":"tituswormer@gmail.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/zwitch_2.0.4_1668589753881_0.3148052657085556"},"_hasShrinkwrap":false,"readme":"# zwitch\n\n[![Build][build-badge]][build]\n[![Coverage][coverage-badge]][coverage]\n[![Downloads][downloads-badge]][downloads]\n[![Size][size-badge]][size]\n\nHandle values based on a field.\n\n## Contents\n\n*   [What is this?](#what-is-this)\n*   [When should I use this?](#when-should-i-use-this)\n*   [Install](#install)\n*   [Use](#use)\n*   [API](#api)\n    *   [`zwitch(key[, options])`](#zwitchkey-options)\n    *   [`one(value[, rest…])`](#onevalue-rest)\n    *   [`function handler(value[, rest…])`](#function-handlervalue-rest)\n*   [Types](#types)\n*   [Compatibility](#compatibility)\n*   [Related](#related)\n*   [Contribute](#contribute)\n*   [Security](#security)\n*   [License](#license)\n\n## What is this?\n\nThis is a tiny package that lets you `switch` between some field on objects.\n\n## When should I use this?\n\nThis package is very useful when mapping one AST to another.\nIt’s a lot like a `switch` statement on one field, but it’s extensible.\n\n## Install\n\nThis package is [ESM only][esm].\nIn Node.js (version 14.14+, 16.0+), install with [npm][]:\n\n```sh\nnpm install zwitch\n```\n\nIn Deno with [`esm.sh`][esmsh]:\n\n```js\nimport {zwitch} from 'https://esm.sh/zwitch@2'\n```\n\nIn browsers with [`esm.sh`][esmsh]:\n\n```html\n<script type=\"module\">\n  import {zwitch} from 'https://esm.sh/zwitch@2?bundle'\n</script>\n```\n\n## Use\n\n```js\nimport {zwitch} from 'zwitch'\n\nconst handle = zwitch('type', {invalid, unknown, handlers: {alpha: handleAlpha}})\n\nhandle({type: 'alpha'})\n\nfunction handleAlpha() { /* … */ }\n```\n\nOr, with a `switch` statement:\n\n```js\nconst field = 'type'\n\nfunction handle(value) {\n  let fn = invalid\n\n  if (value && typeof value === 'object' && field in value) {\n    switch (value[field]) {\n      case 'alpha':\n        fn = handleAlpha\n        break\n      default:\n        fn = unknown\n        break\n    }\n  }\n\n  return fn.apply(this, arguments)\n}\n\nhandle({type: 'alpha'})\n\nfunction handleAlpha() { /* … */ }\nfunction unknown() { /* … */ }\nfunction invalid() { /* … */ }\n```\n\n## API\n\nThis package exports the identifier `zwitch`.\nThere is no default export.\n\n### `zwitch(key[, options])`\n\nCreate a switch, based on a `key` (`string`).\n\n##### `options`\n\nOptions can be omitted and added later to `one`.\n\n###### `options.handlers`\n\nHandlers to use, stored on `one.handlers` (`Record<string, Function>`,\noptional).\n\n###### `options.unknown`\n\nHandler to use for unknown values, stored on `one.unknown` (`Function`,\noptional).\n\n###### `options.invalid`\n\nHandler to use for invalid values, stored on `one.invalid` (`Function`,\noptional).\n\n###### Returns\n\nSee [`one`][one] (`Function`).\n\n### `one(value[, rest…])`\n\nHandle one value.\nBased on the bound `key`, a respective handler will be called.\nIf `value` is not an object, or doesn’t have a `key` property, the special\n“invalid” handler will be called.\nIf `value` has an unknown `key`, the special “unknown” handler will be called.\n\nAll arguments, and the context object (`this`), are passed through to the\n[handler][], and it’s result is returned.\n\n###### `one.handlers`\n\nMap of [handler][]s (`Record<string, Function>`).\n\n###### `one.invalid`\n\nSpecial [`handler`][handler] called if a value doesn’t have a `key` property.\nIf not set, `undefined` is returned for invalid values.\n\n###### `one.unknown`\n\nSpecial [`handler`][handler] called if a value does not have a matching\nhandler.\nIf not set, `undefined` is returned for unknown values.\n\n### `function handler(value[, rest…])`\n\nHandle one value.\n\n## Types\n\nThis package is fully typed with [TypeScript][].\nIt exports the types `Handler`, `UnknownHandler`, `InvalidHandler`, and\n`Options`.\n\n## Compatibility\n\nThis package is at least compatible with all maintained versions of Node.js.\nAs of now, that is Node.js 14.14+ and 16.0+.\nIt also works in Deno and modern browsers.\n\n## Related\n\n*   [`mapz`](https://github.com/wooorm/mapz)\n    — functional map\n\n## Contribute\n\nYes please!\nSee [How to Contribute to Open Source][contribute].\n\n## Security\n\nThis package is safe.\n\n## License\n\n[MIT][license] © [Titus Wormer][author]\n\n<!-- Definitions -->\n\n[build-badge]: https://github.com/wooorm/zwitch/workflows/main/badge.svg\n\n[build]: https://github.com/wooorm/zwitch/actions\n\n[coverage-badge]: https://img.shields.io/codecov/c/github/wooorm/zwitch.svg\n\n[coverage]: https://codecov.io/github/wooorm/zwitch\n\n[downloads-badge]: https://img.shields.io/npm/dm/zwitch.svg\n\n[downloads]: https://www.npmjs.com/package/zwitch\n\n[size-badge]: https://img.shields.io/bundlephobia/minzip/zwitch.svg\n\n[size]: https://bundlephobia.com/result?p=zwitch\n\n[npm]: https://docs.npmjs.com/cli/install\n\n[esm]: https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c\n\n[esmsh]: https://esm.sh\n\n[typescript]: https://www.typescriptlang.org\n\n[contribute]: https://opensource.guide/how-to-contribute/\n\n[license]: license\n\n[author]: https://wooorm.com\n\n[one]: #onevalue-rest\n\n[handler]: #function-handlervalue-rest\n","publish_time":1668589754038,"_cnpm_publish_time":1668589754038,"dist-tags":{"latest":"2.0.4"}}