how to use the rearg function from lodash in javascript

In JavaScript, you can use the rearg() function from the lodash library to create a new function with an order of arguments that is different from the original function.

Here's an example that demonstrates how to use rearg():

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

function foo(a, b, c) {
  console.log(`${a} ${b} ${c}`);
}

const newFoo = _.rearg(foo, [2, 0, 1]);

newFoo('hello', 'world', '!');
// Output: "world hello !"
189 chars
11 lines

In this example, foo() is the original function that takes three arguments: a, b, and c. We then use _.rearg() to create a new function called newFoo() that has a different order of arguments.

The second argument of _.rearg() is an array that specifies the new order of arguments for newFoo(). In this example, [2, 0, 1] means that the third argument of the original function (c) should be the first argument of the new function, the first argument of the original function (a) should be the second argument of the new function, and the second argument of the original function (b) should be the third argument of the new function.

When we call newFoo('hello', 'world', '!'), it outputs "world hello !", which shows that the arguments have been rearranged according to the specified order.

gistlibby LogSnag