has-values
Advanced tools
Comparing version 1.0.0 to 2.0.0
51
index.js
/*! | ||
* has-values <https://github.com/jonschlinkert/has-values> | ||
* | ||
* Copyright (c) 2014-2015, 2017, Jon Schlinkert. | ||
* Copyright (c) 2014-2018, Jon Schlinkert. | ||
* Released under the MIT License. | ||
@@ -10,32 +10,23 @@ */ | ||
var typeOf = require('kind-of'); | ||
var isNumber = require('is-number'); | ||
const typeOf = require('kind-of'); | ||
module.exports = function hasValue(val) { | ||
// is-number checks for NaN and other edge cases | ||
if (isNumber(val)) { | ||
return true; | ||
} | ||
module.exports = function has(val) { | ||
switch (typeOf(val)) { | ||
case 'null': | ||
case 'boolean': | ||
case 'date': | ||
case 'function': | ||
case 'null': | ||
case 'number': | ||
return true; | ||
case 'undefined': | ||
return false; | ||
case 'regexp': | ||
return val.source !== '(?:)' && val.source !== ''; | ||
case 'buffer': | ||
return val.toString() !== ''; | ||
case 'error': | ||
return val.message !== ''; | ||
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': | ||
@@ -45,10 +36,6 @@ case 'map': | ||
return val.size !== 0; | ||
case 'array': | ||
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])) { | ||
for (const key of Object.keys(val)) { | ||
if (has(val[key])) { | ||
return true; | ||
@@ -58,6 +45,8 @@ } | ||
return false; | ||
// everything else | ||
default: { | ||
return false; | ||
return true; | ||
} | ||
} | ||
}; |
{ | ||
"name": "has-values", | ||
"description": "Returns true if any values exist, false if empty. Works for booleans, functions, numbers, strings, nulls, objects and arrays. ", | ||
"version": "1.0.0", | ||
"version": "2.0.0", | ||
"homepage": "https://github.com/jonschlinkert/has-values", | ||
@@ -17,3 +17,3 @@ "author": "Jon Schlinkert (https://github.com/jonschlinkert)", | ||
"engines": { | ||
"node": ">=0.10.0" | ||
"node": ">=6" | ||
}, | ||
@@ -24,8 +24,7 @@ "scripts": { | ||
"dependencies": { | ||
"is-number": "^3.0.0", | ||
"kind-of": "^4.0.0" | ||
"kind-of": "^6.0.2" | ||
}, | ||
"devDependencies": { | ||
"gulp-format-md": "^0.1.12", | ||
"mocha": "^3.4.1" | ||
"gulp-format-md": "^1.0.0", | ||
"mocha": "^3.5.3" | ||
}, | ||
@@ -71,6 +70,6 @@ "keywords": [ | ||
"has-value", | ||
"kind-of", | ||
"is-number", | ||
"is-plain-object", | ||
"isobject" | ||
"isobject", | ||
"kind-of" | ||
] | ||
@@ -77,0 +76,0 @@ }, |
192
README.md
@@ -5,2 +5,4 @@ # 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) | ||
Please consider following this project's author, [Jon Schlinkert](https://github.com/jonschlinkert), and consider starring the project to show your :heart: and support. | ||
## Install | ||
@@ -17,65 +19,135 @@ | ||
```js | ||
var hasValue = require('has-values'); | ||
const has = require('has-values'); | ||
``` | ||
hasValue('a'); | ||
//=> true | ||
Create an `isEmpty` function by returning the inverse of the result from has-values: | ||
hasValue(''); | ||
//=> false | ||
```js | ||
const isEmpty = (val, allowZero) => !has(val, allowZero); | ||
``` | ||
hasValue(1); | ||
//=> true | ||
## Supported types | ||
hasValue(0); | ||
//=> false | ||
### Arrays | ||
hasValue({a: 'a'}}); | ||
//=> true | ||
```js | ||
console.log(has(['a'])); //=> true | ||
console.log(has([0])); //=> true | ||
console.log(has([0], false)); //=> false | ||
console.log(has([[[]]])); //=> false | ||
console.log(has([[], []])); //=> false | ||
console.log(has([])); //=> false | ||
``` | ||
hasValue({}); | ||
hasValue({foo: undefined}); | ||
//=> false | ||
### Booleans | ||
hasValue({foo: null}); | ||
//=> true | ||
```js | ||
console.log(has(true)); //=> true | ||
console.log(has(false)); //=> true | ||
``` | ||
hasValue(['a']); | ||
//=> true | ||
### Buffers | ||
hasValue([]); | ||
hasValue([[], []]); | ||
hasValue([[[]]]); | ||
//=> false | ||
```js | ||
console.log(has(new Buffer())); //=> false | ||
console.log(has(new Buffer('foo'))); //=> true | ||
``` | ||
hasValue(['foo']); | ||
hasValue([0]); | ||
//=> true | ||
### Dates | ||
hasValue(function(foo) {}); | ||
//=> true | ||
Dates are always true. | ||
hasValue(function() {}); | ||
//=> true | ||
```js | ||
console.log(has(new Date())); //=> true | ||
``` | ||
hasValue(true); | ||
//=> true | ||
### Errors | ||
hasValue(false); | ||
//=> true | ||
Returns `false` if `err.message` is an empty string. | ||
```js | ||
console.log(has(new Error())); //=> false | ||
console.log(has(new Error('foo'))); //=> true | ||
``` | ||
## isEmpty | ||
### Functions | ||
To test for empty values, do: | ||
Functions are always true. | ||
```js | ||
function isEmpty(o, isZero) { | ||
return !hasValue(o, isZero); | ||
} | ||
console.log(has(function(foo) {})); //=> true | ||
console.log(has(function() {})); //=> true | ||
``` | ||
### Maps | ||
```js | ||
console.log(has(new Map())); //=> false | ||
console.log(has(new Map([['foo', 'bar']]))); //=> true | ||
``` | ||
### Null | ||
`null` is always true, as it's assumed that this is a user-defined value, versus `undefined` which is not. | ||
```js | ||
console.log(has(null)); //=> true | ||
``` | ||
### Objects | ||
```js | ||
console.log(has({})); //=> false | ||
console.log(has({ a: 'a' }})); //=> true | ||
console.log(has({ foo: undefined })); //=> false | ||
console.log(has({ foo: null })); //=> true | ||
``` | ||
### Numbers | ||
```js | ||
console.log(has(1)); //=> true | ||
console.log(has(0)); //=> true | ||
``` | ||
### Regular expressions | ||
```js | ||
console.log(has(new RegExp())); //=> false | ||
console.log(has(new RegExp('foo'))); //=> true | ||
``` | ||
### Sets | ||
```js | ||
console.log(has(new Set())); //=> false | ||
console.log(has(new Set(['foo', 'bar']))); //=> true | ||
``` | ||
### Strings | ||
```js | ||
console.log(has('a')); //=> true | ||
console.log(has('')); //=> false | ||
``` | ||
## Undefined | ||
```js | ||
console.log(has()); //=> false | ||
console.log(has(void 0)); //=> false | ||
console.log(has(undefined)); //=> false | ||
``` | ||
## Release history | ||
### v2.0.0 | ||
* no longer supports numbers as a string | ||
* optimizations | ||
* adds support for `regex` and `buffer` | ||
### v1.0.0 | ||
* adds support for `Map` and `Set` | ||
* `zero` always returns true | ||
@@ -87,16 +159,23 @@ * `array` now recurses, so that an array of empty arrays will return `false` | ||
### Related projects | ||
<details> | ||
<summary><strong>Contributing</strong></summary> | ||
* [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.") | ||
Pull requests and stars are always welcome. For bugs and feature requests, [please create an issue](../../issues/new). | ||
### Contributing | ||
</details> | ||
Pull requests and stars are always welcome. For bugs and feature requests, [please create an issue](../../issues/new). | ||
<details> | ||
<summary><strong>Running Tests</strong></summary> | ||
### Building docs | ||
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 && npm test | ||
``` | ||
</details> | ||
<details> | ||
<summary><strong>Building docs</strong></summary> | ||
_(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.)_ | ||
@@ -110,10 +189,14 @@ | ||
### Running tests | ||
</details> | ||
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: | ||
### Related projects | ||
```sh | ||
$ npm install && npm test | ||
``` | ||
You might also be interested in these projects: | ||
* [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.") | ||
### Author | ||
@@ -123,2 +206,3 @@ | ||
* [linkedin/in/jonschlinkert](https://linkedin.com/in/jonschlinkert) | ||
* [github/jonschlinkert](https://github.com/jonschlinkert) | ||
@@ -129,3 +213,3 @@ * [twitter/jonschlinkert](https://twitter.com/jonschlinkert) | ||
Copyright © 2017, [Jon Schlinkert](https://github.com/jonschlinkert). | ||
Copyright © 2018, [Jon Schlinkert](https://github.com/jonschlinkert). | ||
Released under the [MIT License](LICENSE). | ||
@@ -135,2 +219,2 @@ | ||
_This file was generated by [verb-generate-readme](https://github.com/verbose/verb-generate-readme), v0.6.0, on May 19, 2017._ | ||
_This file was generated by [verb-generate-readme](https://github.com/verbose/verb-generate-readme), v0.6.0, on January 30, 2018._ |
Sorry, the diff of this file is not supported yet
9722
1
213
45
+ Addedkind-of@6.0.3(transitive)
- Removedis-number@^3.0.0
- Removedis-buffer@1.1.6(transitive)
- Removedis-number@3.0.0(transitive)
- Removedkind-of@3.2.24.0.0(transitive)
Updatedkind-of@^6.0.2