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 | Please consider following this project's author, [Jon Schlinkert](https://github.com/jonschlinkert), and consider starring the project to show your :heart: and support.
|
6 |
|
7 | ## Install
|
8 |
|
9 | Install with [npm](https://www.npmjs.com/):
|
10 |
|
11 | ```sh
|
12 | $ npm install --save has-value
|
13 | ```
|
14 |
|
15 | ## Heads up!
|
16 |
|
17 | Breaking changes in v2.0! See the [release history](#release-history) for details.
|
18 |
|
19 | ## Usage
|
20 |
|
21 | ```js
|
22 | const has = require('has-value');
|
23 |
|
24 | console.log(has()) //=> true
|
25 | console.log(has('foo')) //=> true
|
26 | ```
|
27 |
|
28 | **Works for:**
|
29 |
|
30 | * booleans
|
31 | * functions
|
32 | * numbers
|
33 | * strings
|
34 | * nulls
|
35 | * object
|
36 | * arrays
|
37 |
|
38 | **isEmpty**
|
39 |
|
40 | To do the opposite and test for empty values, do:
|
41 |
|
42 | ```js
|
43 | const isEmpty = (...args) => !has(...args);
|
44 | ```
|
45 |
|
46 | ## Supported types
|
47 |
|
48 | ### Arrays
|
49 |
|
50 | ```js
|
51 | console.log(has({ foo: { bar: ['a'] } }, 'foo.bar')); //=> true
|
52 | console.log(has({ foo: { bar: [0] } }, 'foo.bar')); //=> true
|
53 | console.log(has({ foo: { bar: [[[]]] } }, 'foo.bar')); //=> false
|
54 | console.log(has({ foo: { bar: [[], []] } }, 'foo.bar')); //=> false
|
55 | console.log(has({ foo: { bar: [] } }, 'foo.bar')); //=> false
|
56 | ```
|
57 |
|
58 | ### Booleans
|
59 |
|
60 | ```js
|
61 | console.log(has({ foo: { bar: true } }, 'foo.bar')); //=> true
|
62 | console.log(has({ foo: { bar: false } }, 'foo.bar')); //=> true
|
63 | ```
|
64 |
|
65 | ### Buffers
|
66 |
|
67 | ```js
|
68 | console.log(has({ foo: { bar: new Buffer() } }, 'foo.bar')); //=> false
|
69 | console.log(has({ foo: { bar: new Buffer('foo') } }, 'foo.bar')); //=> true
|
70 | ```
|
71 |
|
72 | ### Dates
|
73 |
|
74 | Dates are always true.
|
75 |
|
76 | ```js
|
77 | console.log(has({ foo: { bar: new Date() } }, 'foo.bar')); //=> true
|
78 | ```
|
79 |
|
80 | ### Errors
|
81 |
|
82 | Returns `false` if `err.message` is an empty string.
|
83 |
|
84 | ```js
|
85 | console.log(has({ foo: { bar: new Error() } }, 'foo.bar')); //=> false
|
86 | console.log(has({ foo: { bar: new Error('foo') } }, 'foo.bar')); //=> true
|
87 | ```
|
88 |
|
89 | ### Functions
|
90 |
|
91 | Functions are always true.
|
92 |
|
93 | ```js
|
94 | console.log(has({ foo: { bar: function(foo) {} } }, 'foo.bar')); //=> true
|
95 | console.log(has({ foo: { bar: function() {} } }, 'foo.bar')); //=> true
|
96 | ```
|
97 |
|
98 | ### Maps
|
99 |
|
100 | ```js
|
101 | console.log(has({ foo: { bar: new Map() } }, 'foo.bar')); //=> false
|
102 | console.log(has({ foo: { bar: new Map([['foo', 'bar']]) } }, 'foo.bar')); //=> true
|
103 | ```
|
104 |
|
105 | ### Null
|
106 |
|
107 | `null` is always true, as it's assumed that this is a user-defined value, versus `undefined` which is not.
|
108 |
|
109 | ```js
|
110 | console.log(has({ foo: { bar: null } }, 'foo.bar')); //=> true
|
111 | ```
|
112 |
|
113 | ### Objects
|
114 |
|
115 | ```js
|
116 | console.log(has({ foo: { bar: {} } }, 'foo.bar')); //=> false
|
117 | console.log(has({ foo: { bar: { a: 'a' }} } }, 'foo.bar')); //=> true
|
118 | console.log(has({ foo: { bar: { foo: undefined } } }, 'foo.bar')); //=> false
|
119 | console.log(has({ foo: { bar: { foo: null } } }, 'foo.bar')); //=> true
|
120 | ```
|
121 |
|
122 | ### Numbers
|
123 |
|
124 | ```js
|
125 | console.log(has({ foo: { bar: 1 } }, 'foo.bar')); //=> true
|
126 | console.log(has({ foo: { bar: 0 } }, 'foo.bar')); //=> true
|
127 | ```
|
128 |
|
129 | ### Regular expressions
|
130 |
|
131 | ```js
|
132 | console.log(has({ foo: { bar: new RegExp() } }, 'foo.bar')); //=> false
|
133 | console.log(has({ foo: { bar: new RegExp('foo') } }, 'foo.bar')); //=> true
|
134 | ```
|
135 |
|
136 | ### Sets
|
137 |
|
138 | ```js
|
139 | console.log(has({ foo: { bar: new Set() } }, 'foo.bar')); //=> false
|
140 | console.log(has({ foo: { bar: new Set(['foo', 'bar']) } }, 'foo.bar')); //=> true
|
141 | ```
|
142 |
|
143 | ### Strings
|
144 |
|
145 | ```js
|
146 | console.log(has({ foo: { bar: 'a' } }, 'foo.bar')); //=> true
|
147 | console.log(has({ foo: { bar: '' } }, 'foo.bar')); //=> false
|
148 | ```
|
149 |
|
150 | ## Undefined
|
151 |
|
152 | ```js
|
153 | console.log(has({ foo: { bar: } }, 'foo.bar')); //=> false
|
154 | console.log(has({ foo: { bar: void 0 } }, 'foo.bar')); //=> false
|
155 | console.log(has({ foo: { bar: undefined } }, 'foo.bar')); //=> false
|
156 | ```
|
157 |
|
158 | ## Release history
|
159 |
|
160 | ### v2.0.0
|
161 |
|
162 | **Breaking changes**
|
163 |
|
164 | * Now returns false if the first argument is not an object, function or array, and the second argument is not a string or array.
|
165 |
|
166 | ### v1.0.0
|
167 |
|
168 | * `zero` always returns true
|
169 | * `array` now recurses, so that an array of empty arrays will return `false`
|
170 | * `null` now returns true
|
171 |
|
172 | ## About
|
173 |
|
174 | <details>
|
175 | <summary><strong>Contributing</strong></summary>
|
176 |
|
177 | Pull requests and stars are always welcome. For bugs and feature requests, [please create an issue](../../issues/new).
|
178 |
|
179 | </details>
|
180 |
|
181 | <details>
|
182 | <summary><strong>Running Tests</strong></summary>
|
183 |
|
184 | 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:
|
185 |
|
186 | ```sh
|
187 | $ npm install && npm test
|
188 | ```
|
189 |
|
190 | </details>
|
191 |
|
192 | <details>
|
193 | <summary><strong>Building docs</strong></summary>
|
194 |
|
195 | _(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.)_
|
196 |
|
197 | To generate the readme, run the following command:
|
198 |
|
199 | ```sh
|
200 | $ npm install -g verbose/verb#dev verb-generate-readme && verb
|
201 | ```
|
202 |
|
203 | </details>
|
204 |
|
205 | ### Related projects
|
206 |
|
207 | You might also be interested in these projects:
|
208 |
|
209 | * [define-property](https://www.npmjs.com/package/define-property): Define a non-enumerable property on an object. Uses Reflect.defineProperty when available, otherwise Object.defineProperty. | [homepage](https://github.com/jonschlinkert/define-property "Define a non-enumerable property on an object. Uses Reflect.defineProperty when available, otherwise Object.defineProperty.")
|
210 | * [get-value](https://www.npmjs.com/package/get-value): Use property paths like 'a.b.c' to get a nested value from an object. Even works… [more](https://github.com/jonschlinkert/get-value) | [homepage](https://github.com/jonschlinkert/get-value "Use property paths like 'a.b.c' to get a nested value from an object. Even works when keys have dots in them (no other dot-prop library can do this!).")
|
211 | * [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.")
|
212 | * [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.")
|
213 |
|
214 | ### Contributors
|
215 |
|
216 | | **Commits** | **Contributor** |
|
217 | | --- | --- |
|
218 | | 32 | [jonschlinkert](https://github.com/jonschlinkert) |
|
219 | | 2 | [rmharrison](https://github.com/rmharrison) |
|
220 | | 1 | [wtgtybhertgeghgtwtg](https://github.com/wtgtybhertgeghgtwtg) |
|
221 |
|
222 | ### Author
|
223 |
|
224 | **Jon Schlinkert**
|
225 |
|
226 | * [LinkedIn Profile](https://linkedin.com/in/jonschlinkert)
|
227 | * [GitHub Profile](https://github.com/jonschlinkert)
|
228 | * [Twitter Profile](https://twitter.com/jonschlinkert)
|
229 |
|
230 | ### License
|
231 |
|
232 | Copyright © 2018, [Jon Schlinkert](https://github.com/jonschlinkert).
|
233 | Released under the [MIT License](LICENSE).
|
234 |
|
235 | ***
|
236 |
|
237 | _This file was generated by [verb-generate-readme](https://github.com/verbose/verb-generate-readme), v0.6.0, on March 03, 2018._ |
\ | No newline at end of file |