Returns true if a value exists, false if empty. Works with deeply nested values using object paths.
Installations
npm install has-value
Developer Guide
Typescript
No
Module System
CommonJS
Min. Node Version
>=6
Node Version
9.7.1
NPM Version
5.6.0
Score
99.2
Supply Chain
97
Quality
74.9
Maintenance
100
Vulnerability
100
License
Contributors
Unable to fetch Contributors
Languages
JavaScript (100%)
Developer
Download Statistics
Total Downloads
8,703,990,919
Last Day
3,772,031
Last Week
19,697,522
Last Month
71,441,679
Last Year
1,192,135,632
GitHub Statistics
26 Stars
38 Commits
6 Forks
4 Watching
1 Branches
4 Contributors
Bundle Size
4.10 kB
Minified
1.40 kB
Minified + Gzipped
Package Meta Information
Latest Version
2.0.2
Package Id
has-value@2.0.2
Size
3.57 kB
NPM Version
5.6.0
Node Version
9.7.1
Publised On
03 Mar 2018
Total Downloads
Cumulative downloads
Total Downloads
8,703,990,919
Last day
-2.9%
3,772,031
Compared to previous day
Last week
0.4%
19,697,522
Compared to previous week
Last month
-24.9%
71,441,679
Compared to previous month
Last year
-23.1%
1,192,135,632
Compared to previous year
Daily Downloads
Weekly Downloads
Monthly Downloads
Yearly Downloads
Dependencies
2
Dev Dependencies
2
has-value
Returns true if a value exists, false if empty. Works with deeply nested values using object paths.
Please consider following this project's author, Jon Schlinkert, and consider starring the project to show your :heart: and support.
Install
Install with npm:
1$ npm install --save has-value
Heads up!
Breaking changes in v2.0! See the release history for details.
Usage
1const has = require('has-value'); 2 3console.log(has()) //=> true 4console.log(has('foo')) //=> true
Works for:
- booleans
- functions
- numbers
- strings
- nulls
- object
- arrays
isEmpty
To do the opposite and test for empty values, do:
1const isEmpty = (...args) => !has(...args);
Supported types
Arrays
1console.log(has({ foo: { bar: ['a'] } }, 'foo.bar')); //=> true 2console.log(has({ foo: { bar: [0] } }, 'foo.bar')); //=> true 3console.log(has({ foo: { bar: [[[]]] } }, 'foo.bar')); //=> false 4console.log(has({ foo: { bar: [[], []] } }, 'foo.bar')); //=> false 5console.log(has({ foo: { bar: [] } }, 'foo.bar')); //=> false
Booleans
1console.log(has({ foo: { bar: true } }, 'foo.bar')); //=> true 2console.log(has({ foo: { bar: false } }, 'foo.bar')); //=> true
Buffers
1console.log(has({ foo: { bar: new Buffer() } }, 'foo.bar')); //=> false 2console.log(has({ foo: { bar: new Buffer('foo') } }, 'foo.bar')); //=> true
Dates
Dates are always true.
1console.log(has({ foo: { bar: new Date() } }, 'foo.bar')); //=> true
Errors
Returns false
if err.message
is an empty string.
1console.log(has({ foo: { bar: new Error() } }, 'foo.bar')); //=> false 2console.log(has({ foo: { bar: new Error('foo') } }, 'foo.bar')); //=> true
Functions
Functions are always true.
1console.log(has({ foo: { bar: function(foo) {} } }, 'foo.bar')); //=> true 2console.log(has({ foo: { bar: function() {} } }, 'foo.bar')); //=> true
Maps
1console.log(has({ foo: { bar: new Map() } }, 'foo.bar')); //=> false 2console.log(has({ foo: { bar: new Map([['foo', 'bar']]) } }, 'foo.bar')); //=> true
Null
null
is always true, as it's assumed that this is a user-defined value, versus undefined
which is not.
1console.log(has({ foo: { bar: null } }, 'foo.bar')); //=> true
Objects
1console.log(has({ foo: { bar: {} } }, 'foo.bar')); //=> false 2console.log(has({ foo: { bar: { a: 'a' }} } }, 'foo.bar')); //=> true 3console.log(has({ foo: { bar: { foo: undefined } } }, 'foo.bar')); //=> false 4console.log(has({ foo: { bar: { foo: null } } }, 'foo.bar')); //=> true
Numbers
1console.log(has({ foo: { bar: 1 } }, 'foo.bar')); //=> true 2console.log(has({ foo: { bar: 0 } }, 'foo.bar')); //=> true
Regular expressions
1console.log(has({ foo: { bar: new RegExp() } }, 'foo.bar')); //=> false 2console.log(has({ foo: { bar: new RegExp('foo') } }, 'foo.bar')); //=> true
Sets
1console.log(has({ foo: { bar: new Set() } }, 'foo.bar')); //=> false 2console.log(has({ foo: { bar: new Set(['foo', 'bar']) } }, 'foo.bar')); //=> true
Strings
1console.log(has({ foo: { bar: 'a' } }, 'foo.bar')); //=> true 2console.log(has({ foo: { bar: '' } }, 'foo.bar')); //=> false
Undefined
1console.log(has({ foo: { bar: } }, 'foo.bar')); //=> false 2console.log(has({ foo: { bar: void 0 } }, 'foo.bar')); //=> false 3console.log(has({ foo: { bar: undefined } }, 'foo.bar')); //=> false
Release history
v2.0.0
Breaking changes
- Now returns false if the first argument is not an object, function or array, and the second argument is not a string or array.
v1.0.0
zero
always returns truearray
now recurses, so that an array of empty arrays will returnfalse
null
now returns true
About
Contributing
Pull requests and stars are always welcome. For bugs and feature requests, please create an issue.
Running Tests
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:
1$ npm install && npm test
Building docs
(This project's readme.md is generated by verb, please don't edit the readme directly. Any changes to the readme must be made in the .verb.md readme template.)
To generate the readme, run the following command:
1$ npm install -g verbose/verb#dev verb-generate-readme && verb
Related projects
You might also be interested in these projects:
- define-property: Define a non-enumerable property on an object. Uses Reflect.defineProperty when available, otherwise Object.defineProperty. | homepage
- get-value: Use property paths like 'a.b.c' to get a nested value from an object. Even works… more | homepage
- set-value: Create nested values and any intermediaries using dot notation (
'a.b.c'
) paths. | homepage - unset-value: Delete nested properties from an object using dot notation. | homepage
Contributors
Commits | Contributor |
---|---|
32 | jonschlinkert |
2 | rmharrison |
1 | wtgtybhertgeghgtwtg |
Author
Jon Schlinkert
License
Copyright © 2018, Jon Schlinkert. Released under the MIT License.
This file was generated by verb-generate-readme, v0.6.0, on March 03, 2018.
No vulnerabilities found.
Reason
no binaries found in the repo
Reason
license file detected
Details
- Info: project has a license file: LICENSE:0
- Info: FSF or OSI recognized license: MIT License: LICENSE:0
Reason
0 existing vulnerabilities detected
Reason
Found 3/27 approved changesets -- score normalized to 1
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
project is not fuzzed
Details
- Warn: no fuzzer integrations found
Reason
branch protection not enabled on development/release branches
Details
- Warn: branch protection not enabled for branch 'master'
Reason
security policy file not detected
Details
- Warn: no security policy file detected
- Warn: no security file to analyze
- Warn: no security file to analyze
- Warn: no security file to analyze
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
- Warn: 0 commits out of 6 are checked with a SAST tool
Score
3.2
/10
Last Scanned on 2025-01-13
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn MoreOther packages similar to has-value
has-values
Returns true if any values exist, false if empty. Works for booleans, functions, numbers, strings, nulls, objects and arrays.
union-value
Set an array of unique values as the property of an object. Supports setting deeply nested properties using using object-paths/dot notation.
set-value
Set nested properties on an object using dot notation.
is-accessor-descriptor
Returns true if a value has the characteristics of a valid JavaScript accessor descriptor.