Skip to content
Snippets Groups Projects
empty.util.ts 2.33 KiB
Newer Older
Art Lowel's avatar
Art Lowel committed
/**
William Welling's avatar
William Welling committed
 * Returns true if the passed value is null or undefined.
 * hasNoValue();              // true
 * hasNoValue(null);          // true
 * hasNoValue(undefined);     // true
 * hasNoValue('');            // false
 * hasNoValue([]);            // false
 * hasNoValue(function() {}); // false
Art Lowel's avatar
Art Lowel committed
 */
export function hasNoValue(obj?: any): boolean {
  return obj === null || obj === undefined;
}

/**
William Welling's avatar
William Welling committed
 * Returns true if the passed value is not null or undefined.
 * hasValue();              // false
 * hasValue(null);          // false
 * hasValue(undefined);     // false
 * hasValue('');            // true
 * hasValue([]);            // true
 * hasValue(function() {}); // true
Art Lowel's avatar
Art Lowel committed
 */
export function hasValue(obj?: any): boolean {
  return !hasNoValue(obj);
}

/**
William Welling's avatar
William Welling committed
 * Verifies that a value is `null` or an empty string, empty array,
 * or empty function.
 * isEmpty();                // true
 * isEmpty(null);            // true
 * isEmpty(undefined);       // true
 * isEmpty('');              // true
 * isEmpty([]);              // true
 * isEmpty({});              // false
 * isEmpty('Adam Hawkins');  // false
 * isEmpty([0,1,2]);         // false
 * isEmpty('\n\t');          // false
 * isEmpty('  ');            // false
Art Lowel's avatar
Art Lowel committed
 */
export function isEmpty(obj?: any): boolean {
  if (hasNoValue(obj)) {
    return true;
  }

  if (typeof obj.size === 'number') {
    return !obj.size;
  }

William Welling's avatar
William Welling committed
  const objectType = typeof obj;
Art Lowel's avatar
Art Lowel committed

  if (objectType === 'object') {
William Welling's avatar
William Welling committed
    const size = obj.size;
Art Lowel's avatar
Art Lowel committed
    if (typeof size === 'number') {
      return !size;
    }
  }

  if (typeof obj.length === 'number' && objectType !== 'function') {
    return !obj.length;
  }

  if (objectType === 'object') {
William Welling's avatar
William Welling committed
    const length = obj.length;
Art Lowel's avatar
Art Lowel committed
    if (typeof length === 'number') {
      return !length;
    }
  }

  return false;
}

/**
William Welling's avatar
William Welling committed
 * Verifies that a value is not `null`, an empty string, empty array,
 * or empty function.
 * isNotEmpty();                // false
 * isNotEmpty(null);            // false
 * isNotEmpty(undefined);       // false
 * isNotEmpty('');              // false
 * isNotEmpty([]);              // false
 * isNotEmpty({});              // true
 * isNotEmpty('Adam Hawkins');  // true
 * isNotEmpty([0,1,2]);         // true
 * isNotEmpty('\n\t');          // true
 * isNotEmpty('  ');            // true
Art Lowel's avatar
Art Lowel committed
 */
export function isNotEmpty(obj?: any): boolean {
  return !isEmpty(obj);
}