if-else-throw
If X, return Y, else throw Z.
X can be a boolean or a function.
Installation
Requires Node.js 6.0.0 or above.
npm i
API
The module exports a single function.
Parameters
test
(any): The “if” condition, evaluated for truthiness. If a function is provided, it is called withval
as its only argument and its return value is used.- Optional:
val
(any): The “then” value to return iftest
is truthy. If omitted,test
is returned. - Optional:
error
(Error or string): The Error to throw iftest
is falsely. If a string is provided, it is wrapped in anew Error
. If omitted, defaults tonew Error()
.
Return Value
If test
is truthy, returns val
(or returns test
if val
is omitted). If test
is falsey, there is no return value because an error is thrown.
Examples
const ifElseThrow = const value = // [] // Uncaught TypeError: Not an array // 'this is truthy' // Uncaught Error: Must be truthy