Logic functions behave unpredictably with `undefined` as predicate
michsa opened this issue · 2 comments
Our logic functions (ifElse
, when
, unless
, etc) accept booleans and lodash iteratees as predicates, so you can do
F.when('foo', _.merge({ bar: 1 }), { foo: 1 }) //-> { foo: 1, bar: 1 }
or
let shouldDoExtraStuff = true
F.when(shouldDoExtraStuff, doExtraStuff, value) //-> doExtraStuff(value)
Futil's behavior is to return the predicate's value if it is a boolean and wrap it in _.iteratee
otherwise, which causes odd behavior when the value is undefined
.
One would most likely expect F.when(undefined, fn, x)
to produce the same result as F.when(false, fn, x)
. However, since apparently _.iteratee(undefined)(x)
behaves equivalently to _.identity(x)
, F.when(undefined, fn, x)
instead acts like F.whenTruthy(fn, x)
.
What about when it's null
? If we only care about undefined
, we can solve this elegantly with a default value on condition.
@chris110408 if we care about null, use _.isNil
to grab them both - otherwise use a default value
We went the route of treating null and undefined the same