{"name":"zwitch","version":"2.0.2","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/tape":"^4.0.0","c8":"^7.0.0","prettier":"^2.0.0","remark-cli":"^9.0.0","remark-preset-wooorm":"^8.0.0","rimraf":"^3.0.0","tape":"^5.0.0","type-coverage":"^2.0.0","typescript":"^4.0.0","xo":"^0.38.0"},"scripts":{"prepack":"npm run build && npm run format","build":"tsc && type-coverage","format":"remark . -qfo && prettier . -w --loglevel warn && xo --fix","test-api":"node test.js","test-coverage":"c8 --check-coverage --branches 100 --functions 100 --lines 100 --statements 100 --reporter lcov node test.js","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,"rules":{"no-var":"off","prefer-arrow-callback":"off","unicorn/prefer-reflect-apply":"off"}},"remarkConfig":{"plugins":["preset-wooorm"]},"typeCoverage":{"atLeast":100,"detail":true,"strict":true},"gitHead":"a4bbebd4832c9f15c41fdb41d55852b94771ef7d","homepage":"https://github.com/wooorm/zwitch#readme","_id":"zwitch@2.0.2","_nodeVersion":"15.11.0","_npmVersion":"7.6.2","dist":{"integrity":"sha512-JZxotl7SxAJH0j7dN4pxsTV6ZLXoLdGME+PsjkL/DaBrVryK9kTGq06GfKrwcSOqypP+fdXGoCHE36b99fWVoA==","shasum":"91f8d0e901ffa3d66599756dde7f57b17c95dce1","tarball":"http://registry-npm.ywwl.com/zwitch/download/zwitch-2.0.2.tgz","fileCount":5,"unpackedSize":8621,"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v3.0.13\r\nComment: https://openpgpjs.org\r\n\r\nwsFcBAEBCAAQBQJgZumMCRA9TVsSAnZWagAAChEQAKPHXVw1dp9H/RPnrmA2\nuu+Qgu2CdRaDpUxjV9x22YNQujdcTqFajgjfXQ3lNtmsmdf9x4WY928PZFqA\nzklgVkfRPamI9zwFUa34fgDtauiHk1Sc/cjb1K4H+5W5zrWHt8fTD2SBrL+k\nkMP3X7HiWqiz62aDKVveT2FW4BJnumKGpWWtB9Bv+4XvWCFXmqJcTX8SM77l\nQkvAs6jfDhfcPdRsfBO3Qj6jHvIUb5YiG8JpeBVbfyNKvwUwSOk/PYDXQ1lE\nrtjIMq50+ostC8bWMQ5UKNXS59cwnjkXggyFh6xFAU2O6/tl54W7qExBohoK\nLmLVeCSIM0UTkB1B9au1QtGq4fSqMwKYTqyREwAqYpE5v3jXO8+StiMHfrEM\nj67resQVKuy7iRO9xeTIVu0BScu2BejgXBMMY65nzgW1uIoH58ehKZ+YRCPK\nwRTyf1FLIIBb5YZj6Ip0wecwn4zxrGkoRriD5NO+yHucLx8gtJFOZvGuC+oM\nFDbf/qw99XDoSnKvOq7UpUCtLW5MPSGEA0But672Oo4IUGH0KJAqnYqJwYYQ\nQuuEbXpkJMI+ERI0i8uPaUv2eXALozwc33UONAiVVec/OMVkStM160UeTKPN\n9Vd4+YWQZ+Oct4T4yZs0KzEP0A64EnmOwcRiGcMprIZRAnmf+iGf5wBik+Vk\nhfN3\r\n=DYua\r\n-----END PGP SIGNATURE-----\r\n","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEYCIQDgct/Za6Xh4dy18GrO+GHgKvhmpUKQTH5qrSEgi/brFQIhAJklt7lr7fbyiiFH7DtPtlI/pHf+2BaGEspgc1cHio/U"}],"size":3440,"noattachment":false,"key":"/zwitch/-/zwitch-2.0.2.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.2_1617357195628_0.2388781746729176"},"_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":1617357195797,"_cnpm_publish_time":1617357195797,"dist-tags":{"latest":"2.0.4"}}