30-seconds-of-code
Version:
A collection of useful JavaScript snippets.
20 lines (15 loc) • 507 B
Markdown
Creates a function that invokes the provided function with its arguments arranged according to the specified indexes.
Use `Array.map()` to reorder arguments based on `indexes` in combination with the spread operator (`...`) to pass the transformed arguments to `fn`.
```js
const rearg = (fn, indexes) => (...args) => fn(...indexes.map(i => args[i]));
```
```js
var rearged = rearg(
function(a, b, c) {
return [a, b, c];
},
[]
);
rearged('b', 'c', 'a'); // ['a', 'b', 'c']
```