Software zum Installieren eines Smart-Mirror Frameworks , zum Nutzen von hochschulrelevanten Informationen, auf einem Raspberry-Pi.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

readme.md 2.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. # zwitch
  2. [![Build][build-badge]][build]
  3. [![Coverage][coverage-badge]][coverage]
  4. [![Downloads][downloads-badge]][downloads]
  5. [![Size][size-badge]][size]
  6. Handle values based on a property.
  7. ## Install
  8. [npm][]:
  9. ```sh
  10. npm install zwitch
  11. ```
  12. ## Use
  13. ```js
  14. var zwitch = require('zwitch')
  15. var handle = zwitch('type')
  16. handle.invalid = invalid
  17. handle.unknown = unknown
  18. handle.handlers.alpha = handle
  19. handle({type: 'alpha'})
  20. ```
  21. Or, with a `switch` statement:
  22. ```javascript
  23. function handle(value) {
  24. var fn
  25. if (!value || typeof value !== 'object' || !('type' in value)) {
  26. fn = invalid
  27. } else {
  28. switch (value.type) {
  29. case 'alpha':
  30. fn = handle
  31. break
  32. default:
  33. fn = unknown
  34. break
  35. }
  36. }
  37. return fn.apply(this, arguments)
  38. }
  39. handle({type: 'alpha'})
  40. ```
  41. ## API
  42. ### `zwitch(key[, options])`
  43. Create a functional switch, based on a `key` (`string`).
  44. ###### `options`
  45. Options can be omitted and added later to `one`.
  46. * `handlers` (`Object.<Function>`, optional)
  47. — Object mapping values to handle, stored on `one.handlers`
  48. * `invalid` (`Function`, optional)
  49. — Handle values without `key`, stored on `one.invalid`
  50. * `unknown` (`Function`, optional)
  51. — Handle values with an unhandled `key`, stored on `one.unknown`
  52. ###### Returns
  53. `Function` — See [`one`][one].
  54. #### `one(value[, rest...])`
  55. Handle one value. Based on the bound `key`, a respective handler will be
  56. invoked.
  57. If `value` is not an object, or doesn’t have a `key` property, the special
  58. “invalid” handler will be invoked.
  59. If `value` has an unknown `key`, the special “unknown” handler will be invoked.
  60. All arguments, and the context object, are passed through to the [handler][],
  61. and it’s result is returned.
  62. #### `one.handlers`
  63. Map of [handler][]s (`Object.<string, Function>`).
  64. #### `one.invalid`
  65. Special [`handler`][handler] invoked if a value doesn’t have a `key` property.
  66. If not set, `undefined` is returned for invalid values.
  67. #### `one.unknown`
  68. Special [`handler`][handler] invoked if a value does not have a matching
  69. handler.
  70. If not set, `undefined` is returned for unknown values.
  71. ### `function handler(value[, rest...])`
  72. Handle one value.
  73. ## Related
  74. * [`mapz`](https://github.com/wooorm/mapz)
  75. — Functional map
  76. ## License
  77. [MIT][license] © [Titus Wormer][author]
  78. <!-- Definitions -->
  79. [build-badge]: https://img.shields.io/travis/wooorm/zwitch.svg
  80. [build]: https://travis-ci.org/wooorm/zwitch
  81. [coverage-badge]: https://img.shields.io/codecov/c/github/wooorm/zwitch.svg
  82. [coverage]: https://codecov.io/github/wooorm/zwitch
  83. [downloads-badge]: https://img.shields.io/npm/dm/zwitch.svg
  84. [downloads]: https://www.npmjs.com/package/zwitch
  85. [size-badge]: https://img.shields.io/bundlephobia/minzip/zwitch.svg
  86. [size]: https://bundlephobia.com/result?p=zwitch
  87. [npm]: https://docs.npmjs.com/cli/install
  88. [license]: license
  89. [author]: https://wooorm.com
  90. [one]: #onevalue-rest
  91. [handler]: #function-handlervalue-rest