Skip to content

Instantly share code, notes, and snippets.

@maxichrome
Last active November 16, 2020 15:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save maxichrome/334d46e1bddd87f3db8d2840a6233801 to your computer and use it in GitHub Desktop.
Save maxichrome/334d46e1bddd87f3db8d2840a6233801 to your computer and use it in GitHub Desktop.
Utility function — checks if all or at least one of provided values is true
/**
* Checks truthiness of any set of inputs
*
* @param {Array<boolean | Function>} operands Values to check
* @param {'all' | 'one'} mode
*
* @returns {boolean}
*/
function allTrue(operands, mode = 'all') {
/**
* @type {boolean[]}
*/
const results = operands.map((operand) =>
typeof operand === 'function' ? operand() : !!operand
)
if (mode === 'all') return !results.includes(false)
if (mode === 'one') return results.includes(true)
console.log(`Mode must be 'one' or 'all'.`)
return false
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment