{"name":"zwitch","version":"1.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"},"author":{"name":"Titus Wormer","email":"tituswormer@gmail.com","url":"http://wooorm.com"},"contributors":[{"name":"Titus Wormer","email":"tituswormer@gmail.com","url":"http://wooorm.com"}],"files":["index.js"],"dependencies":{},"devDependencies":{"browserify":"^14.1.0","esmangle":"^1.0.1","nyc":"^11.0.0","remark-cli":"^3.0.0","remark-preset-wooorm":"^3.0.0","tape":"^4.0.0","xo":"^0.18.0"},"scripts":{"build-md":"remark . -qfo","build-bundle":"browserify index.js --bare -s zwitch > zwitch.js","build-mangle":"esmangle < zwitch.js > zwitch.min.js","build":"npm run build-md && npm run build-bundle && npm run build-mangle","lint":"xo","test-api":"node test","test-coverage":"nyc --reporter lcov tape test.js","test":"npm run build && npm run lint && npm run test-coverage"},"xo":{"space":true,"esnext":false,"ignores":["zwitch.js"]},"nyc":{"check-coverage":true,"lines":100,"functions":100,"branches":100},"remarkConfig":{"plugins":["preset-wooorm"]},"gitHead":"9ffbaaee751f761dd8449e221c916e8ea3d2061d","homepage":"https://github.com/wooorm/zwitch#readme","_id":"zwitch@1.0.2","_shasum":"9b059541bfa844799fe2d903bde609de2503a041","_from":".","_npmVersion":"2.14.2","_nodeVersion":"4.0.0","_npmUser":{"name":"wooorm","email":"tituswormer@gmail.com"},"dist":{"shasum":"9b059541bfa844799fe2d903bde609de2503a041","tarball":"http://registry-npm.ywwl.com/zwitch/download/zwitch-1.0.2.tgz","integrity":"sha512-2tH4r1rg3EHnnbwWyS+vMN4tkTKZIb5eFEwBIjzAvIacFlvCfNtssfZ4hijJyEwNdLcOLpAWdMEIY5rJxn/vdA==","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEYCIQD/gRCQnNlSZOT2pFf0BHp9JqO2OhRB8NQTMPD+/5S74wIhAMrFuyDp8iQTvGYWAOn4aTPGy/y7bdfhu3UVtz/zBFHK"}],"size":2686,"noattachment":false,"key":"/zwitch/-/zwitch-1.0.2.tgz"},"maintainers":[{"name":"wooorm","email":"tituswormer@gmail.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/zwitch-1.0.2.tgz_1500392239794_0.8526203446090221"},"directories":{},"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":1500392240847,"_cnpm_publish_time":1500392240847,"dist-tags":{"latest":"2.0.4"}}