Check if value is empty
export class Utils {
/**
* @static
* @param {*} value
* @param {*} [strict=false]
* @returns {boolean}
* @memberof Utils
*
* Under the rule of strict the values of object and arrays will be checked as well
* Also Number 0 under strict will return false
* Example:
* Utils.IsEmpty(['', '', {}, [{}]]) === true;
*/
static IsEmpty(value: any, strict: any = false): boolean {
if (typeof strict !== 'boolean') {
strict = false;
}
const type = (typeof value).toLocaleLowerCase();
if (type !== 'number' && !Boolean(value)) {
return true;
}
switch (type) {
case 'object':
if (Array.isArray(value)) {
if (strict) {
return value.every(arrayValue => Utils.IsEmpty(arrayValue));
}
return Boolean(value.length === 0);
}
if (strict) {
return Object.keys(value)
.filter(objectKey => Boolean(value[objectKey]))
.every(objectKey => Utils.IsEmpty(value[objectKey]));
}
return Boolean(Object.keys(value).length === 0);
case 'string':
return Boolean(value.length === 0);
case 'number':
return strict ? false : Boolean(value);
default:
return false;
}
}
static NotEmpty(value: any): boolean {
return !Utils.IsEmpty(value);
}
static IsEmptyDeep(value: any): boolean {
return Utils.IsEmpty(value, true);
}
static NotEmptyDeep(value: any): boolean {
return !Utils.IsEmptyDeep(value);
}
}