has-value
Advanced tools
Comparing version 1.0.0 to 2.0.0
20
index.js
/*! | ||
* has-value <https://github.com/jonschlinkert/has-value> | ||
* | ||
* Copyright (c) 2014-2017, Jon Schlinkert. | ||
* Licensed under the MIT License. | ||
* Copyright (c) 2014-2018, Jon Schlinkert. | ||
* Released under the MIT License. | ||
*/ | ||
@@ -10,8 +10,14 @@ | ||
var isObject = require('isobject'); | ||
var hasValues = require('has-values'); | ||
var get = require('get-value'); | ||
const get = require('get-value'); | ||
const has = require('has-values'); | ||
module.exports = function(val, prop) { | ||
return hasValues(isObject(val) && prop ? get(val, prop) : val); | ||
module.exports = function(obj, path, options) { | ||
if (isObject(obj) && typeof path === 'string' || Array.isArray(path)) { | ||
return has(get(obj, path, options)); | ||
} | ||
return false; | ||
}; | ||
function isObject(val) { | ||
return val != null && typeof val === 'object' || typeof val === 'function' || Array.isArray(val); | ||
} |
{ | ||
"name": "has-value", | ||
"description": "Returns true if a value exists, false if empty. Works with deeply nested values using object paths.", | ||
"version": "1.0.0", | ||
"version": "2.0.0", | ||
"homepage": "https://github.com/jonschlinkert/has-value", | ||
@@ -9,3 +9,4 @@ "author": "Jon Schlinkert (https://github.com/jonschlinkert)", | ||
"Jon Schlinkert (http://twitter.com/jonschlinkert)", | ||
"Ryan M Harrison (https://linkedin.com/in/harrisonrm)" | ||
"Ryan M Harrison (https://linkedin.com/in/harrisonrm)", | ||
"(https://github.com/wtgtybhertgeghgtwtg)" | ||
], | ||
@@ -22,3 +23,3 @@ "repository": "jonschlinkert/has-value", | ||
"engines": { | ||
"node": ">=0.10.0" | ||
"node": ">=6" | ||
}, | ||
@@ -29,9 +30,8 @@ "scripts": { | ||
"dependencies": { | ||
"get-value": "^2.0.6", | ||
"has-values": "^1.0.0", | ||
"isobject": "^3.0.0" | ||
"get-value": "^3.0.0", | ||
"has-values": "^2.0.0" | ||
}, | ||
"devDependencies": { | ||
"gulp-format-md": "^0.1.12", | ||
"mocha": "^3.4.1" | ||
"gulp-format-md": "^1.0.0", | ||
"mocha": "^3.5.3" | ||
}, | ||
@@ -41,15 +41,14 @@ "keywords": [ | ||
"boolean", | ||
"check", | ||
"deep", | ||
"empty", | ||
"find", | ||
"function", | ||
"has", | ||
"hasOwn", | ||
"javascript", | ||
"js", | ||
"key", | ||
"keys", | ||
"node.js", | ||
"is-empty", | ||
"nested", | ||
"null", | ||
"number", | ||
"object", | ||
"object path", | ||
"properties", | ||
@@ -59,5 +58,2 @@ "property", | ||
"type", | ||
"util", | ||
"utilities", | ||
"utility", | ||
"value" | ||
@@ -83,3 +79,2 @@ ], | ||
}, | ||
"reflinks": [], | ||
"lint": { | ||
@@ -86,0 +81,0 @@ "reflinks": true |
224
README.md
@@ -5,2 +5,4 @@ # 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) | ||
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 | ||
@@ -14,2 +16,15 @@ | ||
## Heads up! | ||
Breaking changes in v2.0! See the [release history](#release-history) for details. | ||
## Usage | ||
```js | ||
const has = require('has-value'); | ||
console.log(has()) //=> true | ||
console.log(has('foo')) //=> true | ||
``` | ||
**Works for:** | ||
@@ -25,70 +40,130 @@ | ||
## Usage | ||
**isEmpty** | ||
Works with property values (supports object-path notation, like `foo.bar`) or a single value: | ||
To do the opposite and test for empty values, do: | ||
```js | ||
var hasValue = require('has-value'); | ||
const isEmpty = (...args) => !has(...args); | ||
``` | ||
hasValue('foo'); | ||
hasValue({foo: 'bar'}, 'foo'); | ||
hasValue({a: {b: {c: 'foo'}}}, 'a.b.c'); | ||
//=> true | ||
## Supported types | ||
hasValue(''); | ||
hasValue({foo: ''}, 'foo'); | ||
//=> false | ||
### Arrays | ||
hasValue(0); | ||
hasValue(1); | ||
hasValue({foo: 0}, 'foo'); | ||
hasValue({foo: 1}, 'foo'); | ||
hasValue({foo: null}, 'foo'); | ||
hasValue({foo: {bar: 'a'}}}, 'foo'); | ||
hasValue({foo: {bar: 'a'}}}, 'foo.bar'); | ||
//=> true | ||
```js | ||
console.log(has({ foo: { bar: ['a'] } }, 'foo.bar')); //=> true | ||
console.log(has({ foo: { bar: [0] } }, 'foo.bar')); //=> true | ||
console.log(has({ foo: { bar: [[[]]] } }, 'foo.bar')); //=> false | ||
console.log(has({ foo: { bar: [[], []] } }, 'foo.bar')); //=> false | ||
console.log(has({ foo: { bar: [] } }, 'foo.bar')); //=> false | ||
``` | ||
hasValue({foo: {}}}, 'foo'); | ||
hasValue({foo: {bar: {}}}}, 'foo.bar'); | ||
hasValue({foo: undefined}, 'foo'); | ||
//=> false | ||
### Booleans | ||
hasValue([]); | ||
hasValue([[]]); | ||
hasValue([[], []]); | ||
hasValue([undefined]); | ||
hasValue({foo: []}, 'foo'); | ||
//=> false | ||
```js | ||
console.log(has({ foo: { bar: true } }, 'foo.bar')); //=> true | ||
console.log(has({ foo: { bar: false } }, 'foo.bar')); //=> true | ||
``` | ||
hasValue([0]); | ||
hasValue([null]); | ||
hasValue(['foo']); | ||
hasValue({foo: ['a']}, 'foo'); | ||
//=> true | ||
### Buffers | ||
hasValue(function() {}) | ||
hasValue(function(foo) {}) | ||
hasValue({foo: function(foo) {}}, 'foo'); | ||
hasValue({foo: function() {}}, 'foo'); | ||
//=> true | ||
```js | ||
console.log(has({ foo: { bar: new Buffer() } }, 'foo.bar')); //=> false | ||
console.log(has({ foo: { bar: new Buffer('foo') } }, 'foo.bar')); //=> true | ||
``` | ||
hasValue(true); | ||
hasValue(false); | ||
hasValue({foo: true}, 'foo'); | ||
hasValue({foo: false}, 'foo'); | ||
//=> true | ||
### Dates | ||
Dates are always true. | ||
```js | ||
console.log(has({ foo: { bar: new Date() } }, 'foo.bar')); //=> true | ||
``` | ||
## isEmpty | ||
### Errors | ||
To do the opposite and test for empty values, do: | ||
Returns `false` if `err.message` is an empty string. | ||
```js | ||
function isEmpty(o) { | ||
return !hasValue.apply(hasValue, arguments); | ||
} | ||
console.log(has({ foo: { bar: new Error() } }, 'foo.bar')); //=> false | ||
console.log(has({ foo: { bar: new Error('foo') } }, 'foo.bar')); //=> true | ||
``` | ||
### Functions | ||
Functions are always true. | ||
```js | ||
console.log(has({ foo: { bar: function(foo) {} } }, 'foo.bar')); //=> true | ||
console.log(has({ foo: { bar: function() {} } }, 'foo.bar')); //=> true | ||
``` | ||
### Maps | ||
```js | ||
console.log(has({ foo: { bar: new Map() } }, 'foo.bar')); //=> false | ||
console.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. | ||
```js | ||
console.log(has({ foo: { bar: null } }, 'foo.bar')); //=> true | ||
``` | ||
### Objects | ||
```js | ||
console.log(has({ foo: { bar: {} } }, 'foo.bar')); //=> false | ||
console.log(has({ foo: { bar: { a: 'a' }} } }, 'foo.bar')); //=> true | ||
console.log(has({ foo: { bar: { foo: undefined } } }, 'foo.bar')); //=> false | ||
console.log(has({ foo: { bar: { foo: null } } }, 'foo.bar')); //=> true | ||
``` | ||
### Numbers | ||
```js | ||
console.log(has({ foo: { bar: 1 } }, 'foo.bar')); //=> true | ||
console.log(has({ foo: { bar: 0 } }, 'foo.bar')); //=> true | ||
``` | ||
### Regular expressions | ||
```js | ||
console.log(has({ foo: { bar: new RegExp() } }, 'foo.bar')); //=> false | ||
console.log(has({ foo: { bar: new RegExp('foo') } }, 'foo.bar')); //=> true | ||
``` | ||
### Sets | ||
```js | ||
console.log(has({ foo: { bar: new Set() } }, 'foo.bar')); //=> false | ||
console.log(has({ foo: { bar: new Set(['foo', 'bar']) } }, 'foo.bar')); //=> true | ||
``` | ||
### Strings | ||
```js | ||
console.log(has({ foo: { bar: 'a' } }, 'foo.bar')); //=> true | ||
console.log(has({ foo: { bar: '' } }, 'foo.bar')); //=> false | ||
``` | ||
## Undefined | ||
```js | ||
console.log(has({ foo: { bar: } }, 'foo.bar')); //=> false | ||
console.log(has({ foo: { bar: void 0 } }, 'foo.bar')); //=> false | ||
console.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 | ||
@@ -102,22 +177,23 @@ | ||
### Related projects | ||
<details> | ||
<summary><strong>Contributing</strong></summary> | ||
* [define-property](https://www.npmjs.com/package/define-property): Define a non-enumerable property on an object. | [homepage](https://github.com/jonschlinkert/define-property "Define a non-enumerable property on an object.") | ||
* [get-value](https://www.npmjs.com/package/get-value): Use property paths (`a.b.c`) to get a nested value from an object. | [homepage](https://github.com/jonschlinkert/get-value "Use property paths (`a.b.c`) to get a nested value from an object.") | ||
* [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.") | ||
* [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.") | ||
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> | ||
### Contributors | ||
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: | ||
| **Commits** | **Contributor** | | ||
| --- | --- | | ||
| 17 | [jonschlinkert](https://github.com/jonschlinkert) | | ||
| 2 | [rmharrison](https://github.com/rmharrison) | | ||
```sh | ||
$ npm install && npm test | ||
``` | ||
### Building docs | ||
</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.)_ | ||
@@ -131,10 +207,21 @@ | ||
### 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: | ||
* [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.") | ||
* [get-value](https://www.npmjs.com/package/get-value): Use property paths (`a.b.c`) to get a nested value from an object. | [homepage](https://github.com/jonschlinkert/get-value "Use property paths (`a.b.c`) to get a nested value from an object.") | ||
* [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.") | ||
* [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.") | ||
### Contributors | ||
| **Commits** | **Contributor** | | ||
| --- | --- | | ||
| 24 | [jonschlinkert](https://github.com/jonschlinkert) | | ||
| 2 | [rmharrison](https://github.com/rmharrison) | | ||
| 1 | [wtgtybhertgeghgtwtg](https://github.com/wtgtybhertgeghgtwtg) | | ||
### Author | ||
@@ -144,2 +231,3 @@ | ||
* [linkedin/in/jonschlinkert](https://linkedin.com/in/jonschlinkert) | ||
* [github/jonschlinkert](https://github.com/jonschlinkert) | ||
@@ -150,3 +238,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). | ||
@@ -156,2 +244,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
10547
2
18
237
+ Addedget-value@3.0.1(transitive)
+ Addedhas-values@2.0.1(transitive)
+ Addedkind-of@6.0.3(transitive)
- Removedisobject@^3.0.0
- Removedget-value@2.0.6(transitive)
- Removedhas-values@1.0.0(transitive)
- Removedis-buffer@1.1.6(transitive)
- Removedis-number@3.0.0(transitive)
- Removedkind-of@3.2.24.0.0(transitive)
Updatedget-value@^3.0.0
Updatedhas-values@^2.0.0