Lets say you have this amazing util function that looks up some data in some static/global read only Map and does some calculation that you need in many places in your code base, lets imagine the following signature: function wow(number): Metric.
In some other function foo in a different module I'm doing the following to be able to use it easily, but still be able to pass a mock or stub when I want to unit-test foo without invoking wow.
In TypeScript this could look like this:
import {wow as wowGLobal} from '../../[...]';
function foo(data: PayLoad, wow = wowGlobal) {
const metric = wow(data.x);
[...]
}
To be annoyingly pedantic, createFoo above is not a curried function, because it can't be called like createFoo(wow, data) and like createFoo(wow, data) at the same time, it's a higher order function with aurity of 1
Could you provide an example?
Lets say you have this amazing util function that looks up some data in some static/global read only Map and does some calculation that you need in many places in your code base, lets imagine the following signature:
function wow(number): Metric
.In some other function
foo
in a different module I'm doing the following to be able to use it easily, but still be able to pass a mock or stub when I want to unit-testfoo
without invokingwow
.In TypeScript this could look like this:
Is this helpful?
Ah, got it.
I probably would have implemented with a curried function.
in the regular place I'd use
and for testing something different, but I guess your way works too, it's just a bit more implicit.
To be annoyingly pedantic, createFoo above is not a curried function, because it can't be called like createFoo(wow, data) and like createFoo(wow, data) at the same time, it's a higher order function with aurity of 1
I allow it!