takeIf and takeUnless
In addition to scope functions, the Katxupa standard library contains the functions takeIf and takeUnless. These functions let you embed checks of an object's state in call chains.
When called on an object along with a predicate, takeIf returns this object if it satisfies the given predicate. Otherwise, it returns undefined. So, takeIf is a filtering function for a single object.
takeUnless has the opposite logic of takeIf. When called on an object along with a predicate, takeUnless returns undefined if it satisfies the given predicate. Otherwise, it returns the object.
When using takeIf or takeUnless, the object is available as a lambda argument (it).
When chaining other functions after takeIf and takeUnless, don't forget to perform a null check or use a safe call (?.) because their return value is nullable.
takeIf and takeUnless are especially useful in combination with scope functions. For example, you can chain takeIf and takeUnless with letIt to runIt a code block on objects that match the given predicate. To do this, call takeIf on the object and then call let with a safe call (?). For objects that don't match the predicate, takeIf returns undefined and letIt isn't invoked.
Last updated