Skip to content

Instantly share code, notes, and snippets.

@nhalstead
Created June 13, 2019 03:05
Show Gist options
  • Save nhalstead/b1c2d569f5b22b47452ee4546c2e843a to your computer and use it in GitHub Desktop.
Save nhalstead/b1c2d569f5b22b47452ee4546c2e843a to your computer and use it in GitHub Desktop.
Turn `"true"`. `"yes"`, `"1"` into a true or false value.
/**
* Truthy, Extends the Normal Truthy and makes it only true values.
* Normal Truthy will be true if the value is defined.
*
* @author Noah Halstead <[email protected]>
* @param {string|number|boolean} value Something the Evaluate for the "TRUE" value.
* @return boolean If the Value is a "TRUE" value.
*/
function truthy(value){
if (value === undefined) return false;
if (!["string", "number", "boolean"].includes(typeof value)) return false;
if(typeof value == "string") value = value.toLowerCase();
if(value === "true" || value === "yes" || value === true || value === 1 || value === "1") return true;
return false;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment