Created
February 29, 2024 15:54
-
-
Save Aicirou/8f5a6b94ae381618b1e22316c1f14e09 to your computer and use it in GitHub Desktop.
// This function checks if an array or object is empty, null, or has no values.
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
export function hasValueAndNotNull(data) { | |
// This function checks if an array or object is empty, null, or has no values. | |
// Parameters: | |
// - data: The array or object to be checked. | |
// Returns: | |
// - Returns true if the data is not empty, null, or has values. | |
// - Otherwise, prints an error message to the console and returns false. | |
if (data === null) { | |
console.error("Error: Data is null."); | |
return false; | |
} else if (!data || typeof data === "undefined") { | |
console.error("Error: Value is undefined or empty."); | |
return false; | |
} else if (Array.isArray(data) && data.length === 0) { | |
console.error("Error: Array is empty."); | |
return false; | |
} else if (typeof data === "object" && Object.keys(data).length === 0) { | |
console.error("Error: Object is empty."); | |
return false; | |
} else { | |
return true; | |
} | |
// Example usage: | |
// let myArray = []; | |
// let myObject = {}; | |
// let myNullValue = null; | |
// let myUndefinedValue = undefined; | |
// let string = ""; | |
// let number = 0 || NaN; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment