1.0.0 • Published 6 months ago
bool2str v1.0.0
bool2str
(cond ? yes : no) but throw if arg types are not (bool, string, string).
Why?
- To help with situations like YAML bool/string accidents: When you evaluate
an unquoted YAML
no
as a naiveif ()
condition in JavaScript or PHP, it becomes equivalent toyes
, since both are non-empty strings.
API
This module exports one function that holds some others:
bool2str(cond, yes, no, trace)
If cond
is a boolean, and yes
and no
both are strings,
return (cond ? yes : no)
.
If any of the arguments have the wrong type, instead throw an error complaining
about them.
The trace
(ideally a string with a hint) will be appended to the error
message in order to help you find where the problem originated.
bool2str.curry(yes, no, trace) (alias: .c)
Verify that the yes
and no
arguments are strings. Throw an error otherwise.
If they are, prepare (and return) a function that takes one argument cond
in
order to then call bool2str
with the other, pre-defined arguments.
Usage
see test/usage.mjs.
Known issues
- Needs more/better tests and docs.
License
ISC
1.0.0
6 months ago