All files / objects isEmpty.ts

100% Statements 10/10
100% Branches 8/8
100% Functions 1/1
100% Lines 9/9

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30                          7x 18x   16x 6x     10x 5x     5x 3x     2x  
/**
 * Checks if a value is considered empty.
 * Supports strings, arrays, objects, maps, and sets.
 *
 * @param value - The value to check.
 * @returns `true` if the value is empty, `false` otherwise.
 *
 * @example
 * isEmpty(''); // true
 * isEmpty([]); // true
 * isEmpty({}); // true
 * isEmpty([1]); // false
 */
export function isEmpty(value: any): boolean {
  if (value == null) return true;
 
  if (typeof value === 'string' || Array.isArray(value)) {
    return value.length === 0;
  }
 
  if (value instanceof Map || value instanceof Set) {
    return value.size === 0;
  }
 
  if (typeof value === 'object') {
    return Object.keys(value).length === 0;
  }
 
  return false;
}