45e5c996c552e82e3df18b322f8e8f2ae893ea5dc659f1dac8b40bef1db0e08c566e717f113745302d6fc1043cfbfe956756b2e3eb67889cf8ff386faedc60 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. # has-value [![NPM version](https://img.shields.io/npm/v/has-value.svg?style=flat)](https://www.npmjs.com/package/has-value) [![NPM downloads](https://img.shields.io/npm/dm/has-value.svg?style=flat)](https://npmjs.org/package/has-value) [![Build Status](https://img.shields.io/travis/jonschlinkert/has-value.svg?style=flat)](https://travis-ci.org/jonschlinkert/has-value)
  2. > Returns true if a value exists, false if empty. Works with deeply nested values using object paths.
  3. ## Install
  4. Install with [npm](https://www.npmjs.com/):
  5. ```sh
  6. $ npm install has-value --save
  7. ```
  8. **Works for:**
  9. * booleans
  10. * functions
  11. * numbers (pass `true` as the last arg to treat zero as a value instead of falsey)
  12. * strings
  13. * nulls
  14. * object
  15. * arrays
  16. ## Usage
  17. Works with nested object paths or a single value:
  18. ```js
  19. var hasValue = require('has-value');
  20. hasValue({a: {b: {c: 'foo'}}} 'a.b.c');
  21. //=> true
  22. hasValue('a');
  23. //=> true
  24. hasValue('');
  25. //=> false
  26. hasValue(1);
  27. //=> true
  28. hasValue(0);
  29. //=> false
  30. hasValue(0, true); // pass `true` as the last arg to treat zero as a value
  31. //=> true
  32. hasValue({a: 'a'}});
  33. //=> true
  34. hasValue({}});
  35. //=> false
  36. hasValue(['a']);
  37. //=> true
  38. hasValue([]);
  39. //=> false
  40. hasValue(function(foo) {}); // function length/arity
  41. //=> true
  42. hasValue(function() {});
  43. //=> false
  44. hasValue(true);
  45. hasValue(false);
  46. //=> true
  47. ```
  48. ## isEmpty
  49. To do the opposite and test for empty values, do:
  50. ```js
  51. function isEmpty(o, isZero) {
  52. return !hasValue.apply(hasValue, arguments);
  53. }
  54. ```
  55. ## Related projects
  56. You might also be interested in these projects:
  57. * [get-object](https://www.npmjs.com/package/get-object): Get a property from an object using dot (object path) notation. | [homepage](https://github.com/jonschlinkert/get-object)
  58. * [get-property](https://www.npmjs.com/package/get-property): Get a nested property or its value from an object using simple `a.b.c` paths. | [homepage](https://github.com/jonschlinkert/get-property)
  59. * [get-value](https://www.npmjs.com/package/get-value): Use property paths (`a.b.c`) to get a nested value from an object. | [homepage](https://github.com/jonschlinkert/get-value)
  60. * [set-value](https://www.npmjs.com/package/set-value): Create nested values and any intermediaries using dot notation (`'a.b.c'`) paths. | [homepage](https://github.com/jonschlinkert/set-value)
  61. ## Contributing
  62. Pull requests and stars are always welcome. For bugs and feature requests, [please create an issue](https://github.com/jonschlinkert/has-value/issues/new).
  63. ## Building docs
  64. Generate readme and API documentation with [verb](https://github.com/verbose/verb):
  65. ```sh
  66. $ npm install verb && npm run docs
  67. ```
  68. Or, if [verb](https://github.com/verbose/verb) is installed globally:
  69. ```sh
  70. $ verb
  71. ```
  72. ## Running tests
  73. Install dev dependencies:
  74. ```sh
  75. $ npm install -d && npm test
  76. ```
  77. ## Author
  78. **Jon Schlinkert**
  79. * [github/jonschlinkert](https://github.com/jonschlinkert)
  80. * [twitter/jonschlinkert](http://twitter.com/jonschlinkert)
  81. ## License
  82. Copyright © 2016, [Jon Schlinkert](https://github.com/jonschlinkert).
  83. Released under the [MIT license](https://github.com/jonschlinkert/has-value/blob/master/LICENSE).
  84. ***
  85. _This file was generated by [verb](https://github.com/verbose/verb), v, on March 27, 2016._