wallet-core/node_modules/has-values/index.js

61 lines
1.2 KiB
JavaScript
Raw Normal View History

2017-08-14 05:01:11 +02:00
/*!
* has-values <https://github.com/jonschlinkert/has-values>
*
2017-12-10 21:51:33 +01:00
* Copyright (c) 2014-2015, 2017, Jon Schlinkert.
* Released under the MIT License.
2017-08-14 05:01:11 +02:00
*/
'use strict';
2017-12-10 21:51:33 +01:00
var typeOf = require('kind-of');
var isNumber = require('is-number');
2017-08-14 05:01:11 +02:00
2017-12-10 21:51:33 +01:00
module.exports = function hasValue(val) {
// is-number checks for NaN and other edge cases
if (isNumber(val)) {
2017-08-14 05:01:11 +02:00
return true;
}
2017-12-10 21:51:33 +01:00
switch (typeOf(val)) {
case 'null':
case 'boolean':
case 'function':
2017-08-14 05:01:11 +02:00
return true;
2017-12-10 21:51:33 +01:00
case 'string':
case 'arguments':
return val.length !== 0;
case 'error':
return val.message !== '';
case 'array':
var len = val.length;
if (len === 0) {
return false;
}
for (var i = 0; i < len; i++) {
if (hasValue(val[i])) {
return true;
}
}
return false;
case 'file':
case 'map':
case 'set':
return val.size !== 0;
case 'object':
var keys = Object.keys(val);
if (keys.length === 0) {
return false;
}
for (var i = 0; i < keys.length; i++) {
var key = keys[i];
if (hasValue(val[key])) {
return true;
}
}
return false;
default: {
return false;
2017-08-14 05:01:11 +02:00
}
}
};