Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #86 from 201flaviosilva-labs/84-isfalsy
add: isFalsy and isTruthy functions #84
- Loading branch information
Showing
3 changed files
with
66 additions
and
1 deletion.
There are no files selected for viewing
This file contains 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 file contains 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 file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
/** | ||
* Check if the given value is a falsy value | ||
* | ||
* @see {@link https://developer.mozilla.org/en-US/docs/Glossary/Falsy} | ||
* | ||
* @example | ||
* isFalsy(false) // true | ||
* isFalsy("") // true | ||
* isFalsy(0) // true | ||
* isFalsy([]) // true | ||
* isFalsy({}) // true | ||
* isFalsy(null) // true | ||
* isFalsy(undefined) // true | ||
* isFalsy(NaN) // true | ||
* isFalsy("beep") // false | ||
* isFalsy(1) // false | ||
* isFalsy({dog:"Lua"}) // false | ||
* isFalsy(["Snoopy","Ninica","Lua"]) // false | ||
* isFalsy(console) // false | ||
* | ||
* @param {*} value - the value to check | ||
* @returns {boolean} | ||
*/ | ||
export function isFalsy(value) { | ||
try { | ||
if (!value || (typeof value == "object" && (Object.keys(value).length == 0))) return true; | ||
} catch (err) { return true; } | ||
|
||
return false; | ||
}; | ||
|
||
/** | ||
* Check if the given value is a Truthy value | ||
* | ||
* @see {@link https://developer.mozilla.org/en-US/docs/Glossary/Truthy} | ||
* | ||
* @example | ||
* isFalsy(false) // false | ||
* isFalsy("") // false | ||
* isFalsy(0) // false | ||
* isFalsy([]) // false | ||
* isFalsy({}) // false | ||
* isFalsy(null) // false | ||
* isFalsy(undefined) // false | ||
* isFalsy(NaN) // false | ||
* isFalsy("beep") // true | ||
* isFalsy(1) // true | ||
* isFalsy({dog:"Lua"}) // true | ||
* isFalsy(["Snoopy","Ninica","Lua"]) // true | ||
* isFalsy(console) // true | ||
* | ||
* @param {*} value - the value to check | ||
* @returns {boolean} | ||
*/ | ||
export function isTruthy(value) { return !isFalsy(value); } |