30 seconds of code

  Back to Home

rearg

adapterfunctionintermediate

Creates a function that invokes the provided function with its arguments arranged according to the specified indexes.

Use Array.prototype.map() to reorder arguments based on indexes in combination with the spread operator (...) to pass the transformed arguments to fn.

const rearg = (fn, indexes) => (...args) => fn(...indexes.map(i => args[i]));