UNPKG

7.49 kBMarkdownView Raw
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
5Please 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
9Install with [npm](https://www.npmjs.com/):
10
11```sh
12$ npm install --save has-value
13```
14
15## Heads up!
16
17Breaking changes in v2.0! See the [release history](#release-history) for details.
18
19## Usage
20
21```js
22const has = require('has-value');
23
24console.log(has()) //=> true
25console.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
40To do the opposite and test for empty values, do:
41
42```js
43const isEmpty = (...args) => !has(...args);
44```
45
46## Supported types
47
48### Arrays
49
50```js
51console.log(has({ foo: { bar: ['a'] } }, 'foo.bar')); //=> true
52console.log(has({ foo: { bar: [0] } }, 'foo.bar')); //=> true
53console.log(has({ foo: { bar: [[[]]] } }, 'foo.bar')); //=> false
54console.log(has({ foo: { bar: [[], []] } }, 'foo.bar')); //=> false
55console.log(has({ foo: { bar: [] } }, 'foo.bar')); //=> false
56```
57
58### Booleans
59
60```js
61console.log(has({ foo: { bar: true } }, 'foo.bar')); //=> true
62console.log(has({ foo: { bar: false } }, 'foo.bar')); //=> true
63```
64
65### Buffers
66
67```js
68console.log(has({ foo: { bar: new Buffer() } }, 'foo.bar')); //=> false
69console.log(has({ foo: { bar: new Buffer('foo') } }, 'foo.bar')); //=> true
70```
71
72### Dates
73
74Dates are always true.
75
76```js
77console.log(has({ foo: { bar: new Date() } }, 'foo.bar')); //=> true
78```
79
80### Errors
81
82Returns `false` if `err.message` is an empty string.
83
84```js
85console.log(has({ foo: { bar: new Error() } }, 'foo.bar')); //=> false
86console.log(has({ foo: { bar: new Error('foo') } }, 'foo.bar')); //=> true
87```
88
89### Functions
90
91Functions are always true.
92
93```js
94console.log(has({ foo: { bar: function(foo) {} } }, 'foo.bar')); //=> true
95console.log(has({ foo: { bar: function() {} } }, 'foo.bar')); //=> true
96```
97
98### Maps
99
100```js
101console.log(has({ foo: { bar: new Map() } }, 'foo.bar')); //=> false
102console.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
110console.log(has({ foo: { bar: null } }, 'foo.bar')); //=> true
111```
112
113### Objects
114
115```js
116console.log(has({ foo: { bar: {} } }, 'foo.bar')); //=> false
117console.log(has({ foo: { bar: { a: 'a' }} } }, 'foo.bar')); //=> true
118console.log(has({ foo: { bar: { foo: undefined } } }, 'foo.bar')); //=> false
119console.log(has({ foo: { bar: { foo: null } } }, 'foo.bar')); //=> true
120```
121
122### Numbers
123
124```js
125console.log(has({ foo: { bar: 1 } }, 'foo.bar')); //=> true
126console.log(has({ foo: { bar: 0 } }, 'foo.bar')); //=> true
127```
128
129### Regular expressions
130
131```js
132console.log(has({ foo: { bar: new RegExp() } }, 'foo.bar')); //=> false
133console.log(has({ foo: { bar: new RegExp('foo') } }, 'foo.bar')); //=> true
134```
135
136### Sets
137
138```js
139console.log(has({ foo: { bar: new Set() } }, 'foo.bar')); //=> false
140console.log(has({ foo: { bar: new Set(['foo', 'bar']) } }, 'foo.bar')); //=> true
141```
142
143### Strings
144
145```js
146console.log(has({ foo: { bar: 'a' } }, 'foo.bar')); //=> true
147console.log(has({ foo: { bar: '' } }, 'foo.bar')); //=> false
148```
149
150## Undefined
151
152```js
153console.log(has({ foo: { bar: } }, 'foo.bar')); //=> false
154console.log(has({ foo: { bar: void 0 } }, 'foo.bar')); //=> false
155console.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
177Pull 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
184Running 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
197To 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
207You 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
232Copyright © 2018, [Jon Schlinkert](https://github.com/jonschlinkert).
233Released 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