method-combinators
method-combinators copied to clipboard
added sed
The sed combinator transforms a parameter at whatever index you input. Useful in situations analogous to Rails route helpers. user_path(user) is sometimes fed a user and sometimes fed a user id. In cases where it is fed a user's id, sed can transform that id into an actual user as the function parameter (or vice-versa). This prevents the function from needing to contain an if or switch statement that explicitly does the transformation.
So it world be like this
findUser = (sed (user)->
if _.isObject(user)
user
else
@lookupUserById user
)()
class RouteHelpers
userPath: findUser (user)->