idea-toolbox
Advanced tools
Comparing version 0.9.3 to 0.10.0
29
index.js
@@ -36,3 +36,3 @@ 'use strict'; | ||
// OTHER | ||
ISODateToItalianFormat, cleanStr, joinArraysOnKeys | ||
ISODateToItalianFormat, cleanStr, joinArraysOnKeys, isEmpty | ||
} | ||
@@ -471,1 +471,28 @@ | ||
} | ||
/** | ||
* Check if a field (/variable) is empty, based on its type. | ||
* If the type isn't passed as a parameter, it will be auto-detected. | ||
* @param {*} field the field to check | ||
* @param {*} type (optional, to set to force a type check); enum: string, number, date, boolean | ||
*/ | ||
function isEmpty(field, type) { | ||
if(!field) return true; // null, undefined | ||
if(!type) type = typeof field; // try to auto-detect | ||
if(!type) return true; // undefined | ||
// check emptiness based on the type | ||
switch(type) { | ||
case 'string': return field.trim().length <= 0; | ||
case 'number': return field <= 0; | ||
case 'boolean': return !Boolean(field); | ||
case 'date': | ||
case 'object': { | ||
if(field instanceof Date || type == 'date') { | ||
let d = new Date(field); | ||
return !(d instanceof Date) || isNaN(field); | ||
} else if(field instanceof Array) return field.length <= 0; | ||
else return true; | ||
} | ||
default: return true; | ||
} | ||
} |
{ | ||
"name": "idea-toolbox", | ||
"version": "0.9.3", | ||
"version": "0.10.0", | ||
"description": "IDEA's utility functions", | ||
@@ -5,0 +5,0 @@ "engines": { |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
22764
495