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.
Install
Install with npm:
npm i falsey --save-dev
Example
var isFalsey = require('falsey');
console.log(isFalsey('');
console.log(isFalsey(0);
console.log(isFalsey(null);
console.log(isFalsey('nil', ['nil', 'false']);
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, []);
isFalsey('false');
!isFalsey('false', []);
!isFalsey('true');
isFalsey('nil');
!isFalsey('nil', []);
isFalsey('none');
!isFalsey('none', []);
isFalsey('null');
!isFalsey('null', []);
Or, pass an array of values that should return true
when evaluated as falsey:
isFalsey(foo, ['no', 'nope', 'nada', 'zilch']);
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.