Skip to content

Instantly share code, notes, and snippets.

@mogeko
Created October 31, 2022 00:11
Show Gist options
  • Save mogeko/ac600df36fea577894323215ffb37c16 to your computer and use it in GitHub Desktop.
Save mogeko/ac600df36fea577894323215ffb37c16 to your computer and use it in GitHub Desktop.
This function checks if the given value is empty.
/**
* This function checks if the given value is empty.
*
* @param input - Any data structure that may be empty
* @returns if the `input` is empty
*
* @example
* ```typescript
* isEmpty(""); // true
* isEmpty([]); // true
* isEmpty({}); // true
* isEmpty(undefined); // true
* isEmpty(null); // true
* isEmpty(fasle); // true
* isEmpty(true); // false
* isEmpty("foo"); // false
* isEmpty(() => {}); // false
* ```
*/
export function isEmpty(input: any) {
if (!input) return true;
if (typeof input === "object") {
return Object.keys(input).length === 0;
}
return false;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment