empty-value
:package: Check if a given value is empty in JS extending his "truthy" and "falsy" nature
Status
Installation
Using npm
npm install --save empty-value
The following values are considered to be empty:
- NaN
- 0 (0 as an integer)
- 0.0 (0 as a float)
- "0" (0 as a string)
- null
- false
- undefined
- "" (an empty string)
- " " (an string with only spaces)
- array( ) / [ ] (an empty array)
- { } (an empty object / no direct keys)
Usage
var empty = require('empty-value');
empty(true)
empty(12345)
empty(-12345)
empty('Lorem Ipsum is simply dummy text')
empty([1, 2, 3])
empty({ foo: 'bar' })
empty('{ "id" : 1 }')
empty(function () { })
empty(NaN)
empty(0)
empty(0.0)
empty(null)
empty(false)
empty(undefined)
empty('0')
empty('');
empty(' ');
empty([])
empty({ })
var proto = { foo: 'bar' };
var obj = Object.create(proto);
empty(obj)
Using hooks
If empty can't guess the correct answer (IE: value is a function) before return FALSE you could use your own hook. See example bellow:
var myEmptyHook = function (value) {
return typeof value === 'function' ? true : false;
}
var func = function () {
return 'bar';
}
empty(func, myEmptyHook);
License
MIT