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
|
|
3
|
> Returns true if a value exists, false if empty. Works with deeply nested values using object paths.
|
4
|
|
5
|
## Install
|
6
|
|
7
|
Install with [npm](https://www.npmjs.com/):
|
8
|
|
9
|
```sh
|
10
|
$ npm install --save has-value
|
11
|
```
|
12
|
|
13
|
**Works for:**
|
14
|
|
15
|
* booleans
|
16
|
* functions
|
17
|
* numbers
|
18
|
* strings
|
19
|
* nulls
|
20
|
* object
|
21
|
* arrays
|
22
|
|
23
|
## Usage
|
24
|
|
25
|
Works with property values (supports object-path notation, like `foo.bar`) or a single value:
|
26
|
|
27
|
```js
|
28
|
var hasValue = require('has-value');
|
29
|
|
30
|
hasValue('foo');
|
31
|
hasValue({foo: 'bar'}, 'foo');
|
32
|
hasValue({a: {b: {c: 'foo'}}}, 'a.b.c');
|
33
|
//=> true
|
34
|
|
35
|
hasValue('');
|
36
|
hasValue({foo: ''}, 'foo');
|
37
|
//=> false
|
38
|
|
39
|
hasValue(0);
|
40
|
hasValue(1);
|
41
|
hasValue({foo: 0}, 'foo');
|
42
|
hasValue({foo: 1}, 'foo');
|
43
|
hasValue({foo: null}, 'foo');
|
44
|
hasValue({foo: {bar: 'a'}}}, 'foo');
|
45
|
hasValue({foo: {bar: 'a'}}}, 'foo.bar');
|
46
|
//=> true
|
47
|
|
48
|
hasValue({foo: {}}}, 'foo');
|
49
|
hasValue({foo: {bar: {}}}}, 'foo.bar');
|
50
|
hasValue({foo: undefined}, 'foo');
|
51
|
//=> false
|
52
|
|
53
|
hasValue([]);
|
54
|
hasValue([[]]);
|
55
|
hasValue([[], []]);
|
56
|
hasValue([undefined]);
|
57
|
hasValue({foo: []}, 'foo');
|
58
|
//=> false
|
59
|
|
60
|
hasValue([0]);
|
61
|
hasValue([null]);
|
62
|
hasValue(['foo']);
|
63
|
hasValue({foo: ['a']}, 'foo');
|
64
|
//=> true
|
65
|
|
66
|
hasValue(function() {})
|
67
|
hasValue(function(foo) {})
|
68
|
hasValue({foo: function(foo) {}}, 'foo');
|
69
|
hasValue({foo: function() {}}, 'foo');
|
70
|
//=> true
|
71
|
|
72
|
hasValue(true);
|
73
|
hasValue(false);
|
74
|
hasValue({foo: true}, 'foo');
|
75
|
hasValue({foo: false}, 'foo');
|
76
|
//=> true
|
77
|
```
|
78
|
|
79
|
## isEmpty
|
80
|
|
81
|
To do the opposite and test for empty values, do:
|
82
|
|
83
|
```js
|
84
|
function isEmpty(o) {
|
85
|
return !hasValue.apply(hasValue, arguments);
|
86
|
}
|
87
|
```
|
88
|
|
89
|
## Release history
|
90
|
|
91
|
### v1.0.0
|
92
|
|
93
|
* `zero` always returns true
|
94
|
* `array` now recurses, so that an array of empty arrays will return `false`
|
95
|
* `null` now returns true
|
96
|
|
97
|
## About
|
98
|
|
99
|
### Related projects
|
100
|
|
101
|
* [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.")
|
102
|
* [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.")
|
103
|
* [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.")
|
104
|
* [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.")
|
105
|
|
106
|
### Contributing
|
107
|
|
108
|
Pull requests and stars are always welcome. For bugs and feature requests, [please create an issue](../../issues/new).
|
109
|
|
110
|
### Contributors
|
111
|
|
112
|
| **Commits** | **Contributor** |
|
113
|
| --- | --- |
|
114
|
| 17 | [jonschlinkert](https://github.com/jonschlinkert) |
|
115
|
| 2 | [rmharrison](https://github.com/rmharrison) |
|
116
|
|
117
|
### Building docs
|
118
|
|
119
|
_(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.)_
|
120
|
|
121
|
To generate the readme, run the following command:
|
122
|
|
123
|
```sh
|
124
|
$ npm install -g verbose/verb#dev verb-generate-readme && verb
|
125
|
```
|
126
|
|
127
|
### Running tests
|
128
|
|
129
|
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:
|
130
|
|
131
|
```sh
|
132
|
$ npm install && npm test
|
133
|
```
|
134
|
|
135
|
### Author
|
136
|
|
137
|
**Jon Schlinkert**
|
138
|
|
139
|
* [github/jonschlinkert](https://github.com/jonschlinkert)
|
140
|
* [twitter/jonschlinkert](https://twitter.com/jonschlinkert)
|
141
|
|
142
|
### License
|
143
|
|
144
|
Copyright © 2017, [Jon Schlinkert](https://github.com/jonschlinkert).
|
145
|
Released under the [MIT License](LICENSE).
|
146
|
|
147
|
***
|
148
|
|
149
|
_This file was generated by [verb-generate-readme](https://github.com/verbose/verb-generate-readme), v0.6.0, on May 19, 2017._
|