{"name":"zwitch","version":"2.0.0","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","typescript":"^4.0.0","xo":"^0.38.0"},"scripts":{"prepublishOnly":"npm run build && npm run format","prebuild":"rimraf \"*.d.ts\"","build":"tsc","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"]},"gitHead":"50e40e42ff65689abbfdc96e1b791b7764afa09e","homepage":"https://github.com/wooorm/zwitch#readme","_id":"zwitch@2.0.0","_nodeVersion":"15.11.0","_npmVersion":"7.6.1","dist":{"integrity":"sha512-RZOigZnU/+mHcJT7Wr1aenc2pOk+uDui8cquBce+v2NVZcmQpqYXuskqTzJ3c2jny7EQJ5HCsXGphl/FDXmy0w==","shasum":"0404abe6597a1927a87e1ea9bdd90ed71b5681e2","tarball":"http://registry-npm.ywwl.com/zwitch/download/zwitch-2.0.0.tgz","fileCount":5,"unpackedSize":8037,"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v3.0.13\r\nComment: https://openpgpjs.org\r\n\r\nwsFcBAEBCAAQBQJgR3gJCRA9TVsSAnZWagAAQjcQAIsY+ob0PFAViBOcM+/m\nNuURoqYrr5dbDYuoNGckZw5NQA90CTOcOHFxXtPkaPPusQT2jS0ijMxT2WxX\nlNpg+RZxXt91lZOyKnVykLjHpZrsBE89ZBN0bHfvgilYGmbVT1RsOQ7s+CzQ\nvaivY6eCc9Mx9LXGn7VRNM/Iwfb1AbG6A65g30TA96eCvKp/5hlBipHwTuqf\nbDH0c9BZ3ELsnOECQdgf+CZKCj5XxJ/GpBvsmwtC3TVaybFk9xtMnKWosZQY\njISLm8tBIqXyvPAxAd8B+SMWv4iB0TqFJxHxLmNuqvMZIVvpTni+XBR4nMwA\nwE4uuRKG7vMdPFHk0ryTkyDQ8wRoZgNnZ9RT2DADIYdGMW26cs/CyXc90aBy\nM9C3t2VSSUo0PuoLrGyxbkfCJ2DGadHTtYZ+t/6egVPABIQe2yoeYCXuFU61\nLKkSNaXQGsXpr9sDprhBaLcpOFC8jtQGw9824i0JN+aT8Ko0p6aVk5QXSccr\nsmXSrvINGINS5Ki+hGCI33oOicVnP/ymVeOWd2uJSMSYjJ1/tisIrFgalMj4\n3dgk7zdNwjfHNGXdHf7mqTATMlRGsU7N/vWYDfYfoZdYIU3YMpCzqQdQoKCY\nC+zqEJPrfZ8Bq8/JvKjd0BX8A5nRnI9BCK5c9HRg5HdodNasFaRvaKOIsB/K\n79cO\r\n=xKNo\r\n-----END PGP SIGNATURE-----\r\n","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEYCIQDcgBoJdaYBMGw/FuB6dgBE0WiPVMLHTBMFCf5ajBrGfgIhALuaWeMdZYoovCe1TOhciIXrGNgejdDf3k/rFAj1l7aJ"}],"size":3266,"noattachment":false,"key":"/zwitch/-/zwitch-2.0.0.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.0_1615296521226_0.8560484650519882"},"_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":1615296521389,"_cnpm_publish_time":1615296521389,"dist-tags":{"latest":"2.0.4"}}