The cond
function in the Lodash library allows us to create a conditional function based on an array of [predicate, transformer] pairs. Here's how you can use it in Typescript:
First, make sure you have installed the lodash library:
19 chars2 lines
Then, import the cond
function from the library:
index.ts32 chars2 lines
Next, define the array of [predicate, transformer] pairs. The predicate function will take one argument and return a boolean value, and the transformer function will take the same argument and return the result:
index.ts323 chars7 lines
In this example, we have four conditions:
Finally, pass the array of conditions to the cond
function to create the conditional function:
index.ts38 chars2 lines
Now you can use the transformer
function to transform any value based on the conditions you specified:
index.ts231 chars5 lines
gistlibby LogSnag