Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

falsey

Package Overview
Dependencies
Maintainers
1
Versions
7
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

falsey

Returns true if `value` is falsey. Strings, arrays and `arguments` objects with a length of `0`, and objects with no own enumerable properties are considered falsey.

  • 0.1.0
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
121K
decreased by-11.99%
Maintainers
1
Weekly downloads
 
Created
Source

falsey NPM version

Returns true if value is falsey. Strings, arrays and arguments objects with a length of 0, and objects with no own enumerable properties are considered falsey.

Install

Install with npm:

npm i falsey --save-dev

Example

var isFalsey = require('falsey');
console.log(isFalsey('');
//=> `true`

console.log(isFalsey(0);
//=> `true`

console.log(isFalsey(null);
//=> `true`

console.log(isFalsey('nil', ['nil', 'false']); // pass special cases
//=> `true`

Why?

There are other libs that do similar things, but I wanted a lib that was geared towards options handling, so that if a user defines "null" in YAML and it's returned as a string it will still be evaluated as null - and thus falsey. See the special cases for examples.

Examples

All of the following return true

isFalsey(undefined);
isFalsey(null);
isFalsey(false);
!isFalsey(true);
isFalsey(0);
!isFalsey(1);
isFalsey('');
!isFalsey('1');
isFalsey(NaN);
isFalsey({});
!isFalsey({a: 'b'});
isFalsey([]);
!isFalsey([0]);

(function(){
  isFalsey(arguments);
}();
(function(one){
  !isFalsey(arguments);
}(0);

(function(){
  var args = [].slice.call(arguments);
  isFalsey(args);
}();
(function(one){
  var args = [].slice.call(arguments);
  !isFalsey(args);
}(0);

Special cases

There are only four "special cases", ['false', 'none', 'nil', 'null], and these are easily overridden by passing a value as a second parameter, e.g.

isFalsey(foo, []);

// other examples...
isFalsey('false');
!isFalsey('false', []); // override special cases
!isFalsey('true');
isFalsey('nil');
!isFalsey('nil', []); // override special cases
isFalsey('none');
!isFalsey('none', []); // override special cases
isFalsey('null');
!isFalsey('null', []); // override special cases

Or, pass an array of values that should return true when evaluated as falsey:

isFalsey(foo, ['no', 'nope', 'nada', 'zilch']);
// you get the point

Author

Jon Schlinkert

License

Copyright (c) 2014 Jon Schlinkert, contributors. Released under the MIT license


This file was generated by verb-cli on July 08, 2014.

Keywords

FAQs

Package last updated on 09 Jul 2014

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

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc