Created
October 31, 2022 00:11
-
-
Save mogeko/ac600df36fea577894323215ffb37c16 to your computer and use it in GitHub Desktop.
This function checks if the given value is empty.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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