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.

148 lines
4.4 KiB

4 years ago
  1. # has-value [![NPM version](https://img.shields.io/npm/v/has-value.svg?style=flat)](https://www.npmjs.com/package/has-value) [![NPM monthly downloads](https://img.shields.io/npm/dm/has-value.svg?style=flat)](https://npmjs.org/package/has-value) [![NPM total downloads](https://img.shields.io/npm/dt/has-value.svg?style=flat)](https://npmjs.org/package/has-value) [![Linux Build Status](https://img.shields.io/travis/jonschlinkert/has-value.svg?style=flat&label=Travis)](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 --save has-value
  7. ```
  8. **Works for:**
  9. * booleans
  10. * functions
  11. * numbers
  12. * strings
  13. * nulls
  14. * object
  15. * arrays
  16. ## Usage
  17. Works with property values (supports object-path notation, like `foo.bar`) or a single value:
  18. ```js
  19. var hasValue = require('has-value');
  20. hasValue('foo');
  21. hasValue({foo: 'bar'}, 'foo');
  22. hasValue({a: {b: {c: 'foo'}}}, 'a.b.c');
  23. //=> true
  24. hasValue('');
  25. hasValue({foo: ''}, 'foo');
  26. //=> false
  27. hasValue(0);
  28. hasValue(1);
  29. hasValue({foo: 0}, 'foo');
  30. hasValue({foo: 1}, 'foo');
  31. hasValue({foo: null}, 'foo');
  32. hasValue({foo: {bar: 'a'}}}, 'foo');
  33. hasValue({foo: {bar: 'a'}}}, 'foo.bar');
  34. //=> true
  35. hasValue({foo: {}}}, 'foo');
  36. hasValue({foo: {bar: {}}}}, 'foo.bar');
  37. hasValue({foo: undefined}, 'foo');
  38. //=> false
  39. hasValue([]);
  40. hasValue([[]]);
  41. hasValue([[], []]);
  42. hasValue([undefined]);
  43. hasValue({foo: []}, 'foo');
  44. //=> false
  45. hasValue([0]);
  46. hasValue([null]);
  47. hasValue(['foo']);
  48. hasValue({foo: ['a']}, 'foo');
  49. //=> true
  50. hasValue(function() {})
  51. hasValue(function(foo) {})
  52. hasValue({foo: function(foo) {}}, 'foo');
  53. hasValue({foo: function() {}}, 'foo');
  54. //=> true
  55. hasValue(true);
  56. hasValue(false);
  57. hasValue({foo: true}, 'foo');
  58. hasValue({foo: false}, 'foo');
  59. //=> true
  60. ```
  61. ## isEmpty
  62. To do the opposite and test for empty values, do:
  63. ```js
  64. function isEmpty(o) {
  65. return !hasValue.apply(hasValue, arguments);
  66. }
  67. ```
  68. ## Release history
  69. ### v1.0.0
  70. * `zero` always returns true
  71. * `array` now recurses, so that an array of empty arrays will return `false`
  72. * `null` now returns true
  73. ## About
  74. ### Related projects
  75. * [define-property](https://www.npmjs.com/package/define-property): Define a non-enumerable property on an object. | [homepage](https://github.com/jonschlinkert/define-property "Define a non-enumerable property on an object.")
  76. * [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 "Use property paths (`a.b.c`) to get a nested value from an object.")
  77. * [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 "Create nested values and any intermediaries using dot notation (`'a.b.c'`) paths.")
  78. * [unset-value](https://www.npmjs.com/package/unset-value): Delete nested properties from an object using dot notation. | [homepage](https://github.com/jonschlinkert/unset-value "Delete nested properties from an object using dot notation.")
  79. ### Contributing
  80. Pull requests and stars are always welcome. For bugs and feature requests, [please create an issue](../../issues/new).
  81. ### Contributors
  82. | **Commits** | **Contributor** |
  83. | --- | --- |
  84. | 17 | [jonschlinkert](https://github.com/jonschlinkert) |
  85. | 2 | [rmharrison](https://github.com/rmharrison) |
  86. ### Building docs
  87. _(This project's readme.md is generated by [verb](https://github.com/verbose/verb-generate-readme), please don't edit the readme directly. Any changes to the readme must be made in the [.verb.md](.verb.md) readme template.)_
  88. To generate the readme, run the following command:
  89. ```sh
  90. $ npm install -g verbose/verb#dev verb-generate-readme && verb
  91. ```
  92. ### Running tests
  93. Running and reviewing unit tests is a great way to get familiarized with a library and its API. You can install dependencies and run tests with the following command:
  94. ```sh
  95. $ npm install && npm test
  96. ```
  97. ### Author
  98. **Jon Schlinkert**
  99. * [github/jonschlinkert](https://github.com/jonschlinkert)
  100. * [twitter/jonschlinkert](https://twitter.com/jonschlinkert)
  101. ### License
  102. Copyright © 2017, [Jon Schlinkert](https://github.com/jonschlinkert).
  103. Released under the [MIT License](LICENSE).
  104. ***
  105. _This file was generated by [verb-generate-readme](https://github.com/verbose/verb-generate-readme), v0.6.0, on May 19, 2017._