Last active
November 16, 2020 15:58
-
-
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
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
/** | |
* 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