has-values
Advanced tools
Comparing version 0.1.4 to 1.0.0
66
index.js
/*! | ||
* has-values <https://github.com/jonschlinkert/has-values> | ||
* | ||
* Copyright (c) 2014-2015, Jon Schlinkert. | ||
* Licensed under the MIT License. | ||
* Copyright (c) 2014-2015, 2017, Jon Schlinkert. | ||
* Released under the MIT License. | ||
*/ | ||
@@ -10,28 +10,52 @@ | ||
module.exports = function hasValue(o, noZero) { | ||
if (o === null || o === undefined) { | ||
return false; | ||
} | ||
var typeOf = require('kind-of'); | ||
var isNumber = require('is-number'); | ||
if (typeof o === 'boolean') { | ||
module.exports = function hasValue(val) { | ||
// is-number checks for NaN and other edge cases | ||
if (isNumber(val)) { | ||
return true; | ||
} | ||
if (typeof o === 'number') { | ||
if (o === 0 && noZero === true) { | ||
switch (typeOf(val)) { | ||
case 'null': | ||
case 'boolean': | ||
case 'function': | ||
return true; | ||
case 'string': | ||
case 'arguments': | ||
return val.length !== 0; | ||
case 'error': | ||
return val.message !== ''; | ||
case 'array': | ||
var len = val.length; | ||
if (len === 0) { | ||
return false; | ||
} | ||
for (var i = 0; i < len; i++) { | ||
if (hasValue(val[i])) { | ||
return true; | ||
} | ||
} | ||
return false; | ||
case 'file': | ||
case 'map': | ||
case 'set': | ||
return val.size !== 0; | ||
case 'object': | ||
var keys = Object.keys(val); | ||
if (keys.length === 0) { | ||
return false; | ||
} | ||
for (var i = 0; i < keys.length; i++) { | ||
var key = keys[i]; | ||
if (hasValue(val[key])) { | ||
return true; | ||
} | ||
} | ||
return false; | ||
default: { | ||
return false; | ||
} | ||
return true; | ||
} | ||
if (o.length !== undefined) { | ||
return o.length !== 0; | ||
} | ||
for (var key in o) { | ||
if (o.hasOwnProperty(key)) { | ||
return true; | ||
} | ||
} | ||
return false; | ||
}; |
{ | ||
"name": "has-values", | ||
"version": "0.1.4", | ||
"description": "Returns true if any values exist, false if empty. Works for booleans, functions, numbers, strings, nulls, objects and arrays. ", | ||
"version": "1.0.0", | ||
"homepage": "https://github.com/jonschlinkert/has-values", | ||
@@ -22,5 +22,9 @@ "author": "Jon Schlinkert (https://github.com/jonschlinkert)", | ||
}, | ||
"dependencies": { | ||
"is-number": "^3.0.0", | ||
"kind-of": "^4.0.0" | ||
}, | ||
"devDependencies": { | ||
"gulp-format-md": "^0.1.7", | ||
"mocha": "^2.4.5" | ||
"gulp-format-md": "^0.1.12", | ||
"mocha": "^3.4.1" | ||
}, | ||
@@ -50,3 +54,4 @@ "keywords": [ | ||
"utility", | ||
"value" | ||
"value", | ||
"values" | ||
], | ||
@@ -66,4 +71,6 @@ "verb": { | ||
"has-value", | ||
"isobject", | ||
"is-plain-object" | ||
"kind-of", | ||
"is-number", | ||
"is-plain-object", | ||
"isobject" | ||
] | ||
@@ -70,0 +77,0 @@ }, |
@@ -1,2 +0,2 @@ | ||
# has-values [![NPM version](https://img.shields.io/npm/v/has-values.svg?style=flat)](https://www.npmjs.com/package/has-values) [![NPM downloads](https://img.shields.io/npm/dm/has-values.svg?style=flat)](https://npmjs.org/package/has-values) [![Build Status](https://img.shields.io/travis/jonschlinkert/has-values.svg?style=flat)](https://travis-ci.org/jonschlinkert/has-values) | ||
# has-values [![NPM version](https://img.shields.io/npm/v/has-values.svg?style=flat)](https://www.npmjs.com/package/has-values) [![NPM monthly downloads](https://img.shields.io/npm/dm/has-values.svg?style=flat)](https://npmjs.org/package/has-values) [![NPM total downloads](https://img.shields.io/npm/dt/has-values.svg?style=flat)](https://npmjs.org/package/has-values) [![Linux Build Status](https://img.shields.io/travis/jonschlinkert/has-values.svg?style=flat&label=Travis)](https://travis-ci.org/jonschlinkert/has-values) | ||
@@ -10,3 +10,3 @@ > Returns true if any values exist, false if empty. Works for booleans, functions, numbers, strings, nulls, objects and arrays. | ||
```sh | ||
$ npm install has-values --save | ||
$ npm install --save has-values | ||
``` | ||
@@ -31,11 +31,12 @@ | ||
hasValue(0, true); // treat zero as a value | ||
//=> true | ||
hasValue({a: 'a'}}); | ||
//=> true | ||
hasValue({}}); | ||
hasValue({}); | ||
hasValue({foo: undefined}); | ||
//=> false | ||
hasValue({foo: null}); | ||
//=> true | ||
hasValue(['a']); | ||
@@ -45,11 +46,19 @@ //=> true | ||
hasValue([]); | ||
hasValue([[], []]); | ||
hasValue([[[]]]); | ||
//=> false | ||
hasValue(function(foo) {}); // function length/arity | ||
hasValue(['foo']); | ||
hasValue([0]); | ||
//=> true | ||
hasValue(function(foo) {}); | ||
//=> true | ||
hasValue(function() {}); | ||
//=> false | ||
//=> true | ||
hasValue(true); | ||
//=> true | ||
hasValue(false); | ||
@@ -69,37 +78,43 @@ //=> true | ||
## Related projects | ||
## Release history | ||
You might also be interested in these projects: | ||
### v1.0.0 | ||
* [has-value](https://www.npmjs.com/package/has-value): Returns true if a value exists, false if empty. Works with deeply nested values using… [more](https://www.npmjs.com/package/has-value) | [homepage](https://github.com/jonschlinkert/has-value) | ||
* [is-plain-object](https://www.npmjs.com/package/is-plain-object): Returns true if an object was created by the `Object` constructor. | [homepage](https://github.com/jonschlinkert/is-plain-object) | ||
* [isobject](https://www.npmjs.com/package/isobject): Returns true if the value is an object and not an array or null. | [homepage](https://github.com/jonschlinkert/isobject) | ||
* `zero` always returns true | ||
* `array` now recurses, so that an array of empty arrays will return `false` | ||
* `null` now returns true | ||
## Contributing | ||
## About | ||
Pull requests and stars are always welcome. For bugs and feature requests, [please create an issue](https://github.com/jonschlinkert/has-values/issues/new). | ||
### Related projects | ||
## Building docs | ||
* [has-value](https://www.npmjs.com/package/has-value): Returns true if a value exists, false if empty. Works with deeply nested values using… [more](https://github.com/jonschlinkert/has-value) | [homepage](https://github.com/jonschlinkert/has-value "Returns true if a value exists, false if empty. Works with deeply nested values using object paths.") | ||
* [is-number](https://www.npmjs.com/package/is-number): Returns true if the value is a number. comprehensive tests. | [homepage](https://github.com/jonschlinkert/is-number "Returns true if the value is a number. comprehensive tests.") | ||
* [is-plain-object](https://www.npmjs.com/package/is-plain-object): Returns true if an object was created by the `Object` constructor. | [homepage](https://github.com/jonschlinkert/is-plain-object "Returns true if an object was created by the `Object` constructor.") | ||
* [isobject](https://www.npmjs.com/package/isobject): Returns true if the value is an object and not an array or null. | [homepage](https://github.com/jonschlinkert/isobject "Returns true if the value is an object and not an array or null.") | ||
* [kind-of](https://www.npmjs.com/package/kind-of): Get the native type of a value. | [homepage](https://github.com/jonschlinkert/kind-of "Get the native type of a value.") | ||
Generate readme and API documentation with [verb](https://github.com/verbose/verb): | ||
### Contributing | ||
```sh | ||
$ npm install verb && npm run docs | ||
``` | ||
Pull requests and stars are always welcome. For bugs and feature requests, [please create an issue](../../issues/new). | ||
Or, if [verb](https://github.com/verbose/verb) is installed globally: | ||
### Building docs | ||
_(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.)_ | ||
To generate the readme, run the following command: | ||
```sh | ||
$ verb | ||
$ npm install -g verbose/verb#dev verb-generate-readme && verb | ||
``` | ||
## Running tests | ||
### Running tests | ||
Install dev dependencies: | ||
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: | ||
```sh | ||
$ npm install -d && npm test | ||
$ npm install && npm test | ||
``` | ||
## Author | ||
### Author | ||
@@ -109,11 +124,11 @@ **Jon Schlinkert** | ||
* [github/jonschlinkert](https://github.com/jonschlinkert) | ||
* [twitter/jonschlinkert](http://twitter.com/jonschlinkert) | ||
* [twitter/jonschlinkert](https://twitter.com/jonschlinkert) | ||
## License | ||
### License | ||
Copyright © 2016, [Jon Schlinkert](https://github.com/jonschlinkert). | ||
Released under the [MIT license](https://github.com/jonschlinkert/has-values/blob/master/LICENSE). | ||
Copyright © 2017, [Jon Schlinkert](https://github.com/jonschlinkert). | ||
Released under the [MIT License](LICENSE). | ||
*** | ||
_This file was generated by [verb](https://github.com/verbose/verb), v, on March 27, 2016._ | ||
_This file was generated by [verb-generate-readme](https://github.com/verbose/verb-generate-readme), v0.6.0, on May 19, 2017._ |
Sorry, the diff of this file is not supported yet
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
7880
56
0
129
2
+ Addedis-number@^3.0.0
+ Addedkind-of@^4.0.0
+ Addedis-buffer@1.1.6(transitive)
+ Addedis-number@3.0.0(transitive)
+ Addedkind-of@3.2.24.0.0(transitive)