You're Invited:Meet the Socket Team at BlackHat and DEF CON in Las Vegas, Aug 7-8.RSVP
Socket
Socket
Sign inDemoInstall

has-values

Package Overview
Dependencies
1
Maintainers
1
Versions
7
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

has-values

Returns true if any values exist, false if empty. Works for booleans, functions, numbers, strings, nulls, objects and arrays.


Version published
Maintainers
1
Created

Package description

What is has-values?

The has-values npm package is a utility that checks if a value exists and is not empty. It can be used to validate objects, arrays, strings, and other data types to ensure they contain meaningful data.

What are has-values's main functionalities?

Check if a string has a value

This feature allows you to check if a string is non-empty. It returns true if the string has a value and false if it is empty.

const hasValues = require('has-values');
console.log(hasValues('hello')); // true
console.log(hasValues('')); // false

Check if an array has values

This feature checks if an array contains any elements. It returns true if the array has elements and false if it is empty.

const hasValues = require('has-values');
console.log(hasValues([1, 2, 3])); // true
console.log(hasValues([])); // false

Check if an object has values

This feature checks if an object has any properties. It returns true if the object has properties and false if it is empty.

const hasValues = require('has-values');
console.log(hasValues({a: 1, b: 2})); // true
console.log(hasValues({})); // false

Check if a nested object has values

This feature checks if a nested object contains any values. It returns true if the nested object has values and false if it is empty.

const hasValues = require('has-values');
console.log(hasValues({a: {b: 2}})); // true
console.log(hasValues({a: {}})); // false

Other packages similar to has-values

Readme

Source

has-values NPM version NPM monthly downloads NPM total downloads Linux Build Status

Returns true if any values exist, false if empty. Works for booleans, functions, numbers, strings, nulls, objects and arrays.

Please consider following this project's author, Jon Schlinkert, and consider starring the project to show your :heart: and support.

Install

Install with npm:

$ npm install --save has-values

Usage

const has = require('has-values');

Create an isEmpty function by returning the inverse of the result from has-values:

const isEmpty = val => !has(val);

Supported types

Arrays

console.log(has(['a']));      //=> true
console.log(has([0]));        //=> true
console.log(has([[[]]]));     //=> false
console.log(has([[], []]));   //=> false
console.log(has([]));         //=> false

Booleans

console.log(has(true));  //=> true
console.log(has(false)); //=> true

Buffers

console.log(has(new Buffer()));      //=> false
console.log(has(new Buffer('foo'))); //=> true

Dates

Dates are always true.

console.log(has(new Date())); //=> true

Errors

Returns false if err.message is an empty string.

console.log(has(new Error()));      //=> false
console.log(has(new Error('foo'))); //=> true

Functions

Functions are always true.

console.log(has(function(foo) {})); //=> true
console.log(has(function() {}));    //=> true

Maps

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.

console.log(has(null)); //=> true

Objects

console.log(has({})); //=> false
console.log(has({ a: 'a' }}));        //=> true
console.log(has({ foo: undefined })); //=> false
console.log(has({ foo: null }));      //=> true

Numbers

console.log(has(1)); //=> true
console.log(has(0)); //=> true

Regular expressions

console.log(has(new RegExp()));      //=> false
console.log(has(new RegExp('foo'))); //=> true

Sets

console.log(has(new Set()));               //=> false
console.log(has(new Set(['foo', 'bar']))); //=> true

Strings

console.log(has('a')); //=> true
console.log(has(''));  //=> false

Undefined

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
  • array now recurses, so that an array of empty arrays will return false
  • 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:

$ 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:

$ npm install -g verbose/verb#dev verb-generate-readme && verb

You might also be interested in these projects:

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 January 30, 2018.

Keywords

FAQs

Package last updated on 31 Jan 2018

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc