Skip to content

Instantly share code, notes, and snippets.

@uwemneku
Last active February 13, 2022 20:26
Show Gist options
  • Save uwemneku/f8db13cd52be13dc741f4bdd8443dccd to your computer and use it in GitHub Desktop.
Save uwemneku/f8db13cd52be13dc741f4bdd8443dccd to your computer and use it in GitHub Desktop.
Checking is a variable is empty

This funtion is able to check if a variable is empty. For objects, it uses recurssion to check if the proprties are empty.

const isVariableNotEmpty = (v: string |number | Array<any> | object): boolean => {
    if (typeof v === "string" || Array.isArray(v)) {
      return v.length > 0;
    } else if (typeof v === "number") {
      return v > 0;
    } else if (typeof v === "object") {
      const allValues = Object.values(v);
      if (allValues.length === 0) {
        return false;
      }
      return allValues.every((_) => isVariableNotEmpty(_) === true);
    } else {
      return false;
    }
  };
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment