how to use the overargs function from lodash in javascript

The _.overArgs function from Lodash creates a new function which invokes a provided function with its arguments transformed. Each argument transformation is specified as a separate function.

Here is an example usage:

index.tsx
const _ = require('lodash');

function concatenateStrings(prefix, str1, str2, suffix) {
  return prefix + str1 + str2 + suffix;
}

const transformArgs = _.overArgs(concatenateStrings, [
  arg => arg.toUpperCase(),
  arg => arg.toLowerCase(),
  arg => arg.toUpperCase(),
  arg => arg.toLowerCase(),
]);

console.log(transformArgs('start', 'HELLO', 'WoRlD', 'end')); // output: startHELLOworldend
395 chars
15 lines

In the example above, the _.overArgs function is used to create a new function transformArgs which transforms the second and third arguments to concatenateStrings to uppercase and lowercase respectively. This new function is then invoked with the transformed arguments to return the concatenated string.

Note that the second argument to _.overArgs is an array of functions which are applied to the corresponding arguments of the original function. In this case, we are transforming the second argument to uppercase and the third argument to lowercase.

gistlibby LogSnag