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 4.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. <p align="center">
  2. <a href="http://gulpjs.com">
  3. <img height="257" width="114" src="https://raw.githubusercontent.com/gulpjs/artwork/master/gulp-2x.png">
  4. </a>
  5. </p>
  6. # value-or-function
  7. [![NPM version][npm-image]][npm-url] [![Downloads][downloads-image]][npm-url] [![Build Status][travis-image]][travis-url] [![AppVeyor Build Status][appveyor-image]][appveyor-url] [![Coveralls Status][coveralls-image]][coveralls-url] [![Gitter chat][gitter-image]][gitter-url]
  8. Normalize a value or function, applying extra args to the function
  9. ## Example
  10. ```js
  11. var normalize = require('value-or-function');
  12. // Values matching type are returned
  13. var isEnabled = normalize('boolean', true);
  14. // isEnabled === true
  15. // Values not matching type return undefined
  16. var isEnabled = normalize('boolean', 1);
  17. // isEnabled === undefined
  18. // Functions are called
  19. var isEnabled = normalize('boolean', function() {
  20. return false;
  21. });
  22. // isEnabled === false
  23. // Extra arguments are applied to function
  24. var count = normalize('number', function(a, b) {
  25. return a + b;
  26. }, 1, 2);
  27. // count === 3
  28. // Supply the function with context
  29. var context = { c: 3 };
  30. var count = normalize.call(context, 'number', function(a, b) {
  31. return a + b + this.c;
  32. }, 1, 2);
  33. // count === 6
  34. // Values one of multiple types are returned
  35. var isEnabled = normalize(['string', 'boolean'], true);
  36. // isEnabled === true
  37. // Provide a function as first argument to do custom coercion
  38. var now = new Date();
  39. var enabledSince = normalize(function(value) {
  40. if (value.constructor === Date) {
  41. return value;
  42. }
  43. }, now);
  44. // enabledSince === now
  45. // Convenience methods are available for the built-in types
  46. var result = normalize.object({});
  47. var result = normalize.number(1);
  48. var result = normalize.string('');
  49. var result = normalize.symbol(Symbol());
  50. var result = normalize.boolean(true);
  51. var result = normalize.function(function() {});
  52. var result = normalize.date(new Date());
  53. ```
  54. ## API
  55. ### `normalize(coercer, value[, ...appliedArguments])`
  56. Takes a coercer function `coercer` to transform `value` to the desired type.
  57. Also optionally takes any extra arguments to apply to `value` if `value` is a function.
  58. If the return value of `coercer(value)` is not `null` or `undefined`, that value is returned.
  59. Otherwise, if `value` is a function, that function is called with any extra arguments
  60. supplied to `normalize`, and its return value is passed through the coercer.
  61. If `coercer` is a string, it must be one of the built-in types (see below)
  62. and the appropriate default coercer is invoked, optionally first reducing `value`
  63. to a primitive type with `.valueOf()` if it is an Object.
  64. If `coercer` is an array, each element is tried until one returns something other
  65. than `null` or `undefined`, or it results in `undefined` if all of the elements yield `null` or `undefined`.
  66. #### `normalize.object(value[, ...appliedArguments])`
  67. Convenience method for `normalize('object', ...)`.
  68. #### `normalize.number(value[, ...appliedArguments])`
  69. Convenience method for `normalize('number', ...)`.
  70. #### `normalize.string(value[, ...appliedArguments])`
  71. Convenience method for `normalize('string', ...)`.
  72. #### `normalize.symbol(value[, ...appliedArguments])`
  73. Convenience method for `normalize('symbol', ...)`.
  74. #### `normalize.boolean(value[, ...appliedArguments])`
  75. Convenience method for `normalize('boolean', ...)`.
  76. #### `normalize.function(value[, ...appliedArguments])`
  77. Convenience method for `normalize('function', ...)`.
  78. #### `normalize.date(value[, ...appliedArguments])`
  79. Convenience method for `normalize('date', ...)`.
  80. ## License
  81. MIT
  82. [downloads-image]: http://img.shields.io/npm/dm/value-or-function.svg
  83. [npm-url]: https://npmjs.org/package/value-or-function
  84. [npm-image]: http://img.shields.io/npm/v/value-or-function.svg
  85. [travis-url]: https://travis-ci.org/gulpjs/value-or-function
  86. [travis-image]: http://img.shields.io/travis/gulpjs/value-or-function.svg?label=travis-ci
  87. [appveyor-url]: https://ci.appveyor.com/project/gulpjs/value-or-function
  88. [appveyor-image]: https://img.shields.io/appveyor/ci/gulpjs/value-or-function.svg?label=appveyor
  89. [coveralls-url]: https://coveralls.io/r/gulpjs/value-or-function
  90. [coveralls-image]: http://img.shields.io/coveralls/gulpjs/value-or-function/master.svg
  91. [gitter-url]: https://gitter.im/gulpjs/gulp
  92. [gitter-image]: https://badges.gitter.im/gulpjs/gulp.png