diff --git a/README.md b/README.md index 5fcf650d0..2218d95ee 100644 --- a/README.md +++ b/README.md @@ -735,16 +735,10 @@ delay(2000).then(() => console.log('Hi!')); // // Promise resolves after 2s Creates a function that invokes the provided function with its arguments arranged according to the specified indexes. -Use `Array.reduce()` and `Array.indexOf()` to reorder arguments based on `indexes` in combination with the spread operator (`...`) to pass the transformed arguments to `fn`. +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( - ...args.reduce( - (acc, val, i) => ((acc[indexes.indexOf(i)] = val), acc), - Array.from({ length: indexes.length }) - ) - ); +const rearg = (fn, indexes) => (...args) => fn(...indexes.map(i => args[i])); ```
diff --git a/docs/adapter.html b/docs/adapter.html index 08e857dbd..bf63e24cb 100644 --- a/docs/adapter.html +++ b/docs/adapter.html @@ -133,13 +133,7 @@ Object.assig );
const delay = promisify((d, cb) => setTimeout(cb, d));
 delay(2000).then(() => console.log('Hi!')); // // Promise resolves after 2s
-

rearg

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

Use Array.reduce() and Array.indexOf() 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(
-    ...args.reduce(
-      (acc, val, i) => ((acc[indexes.indexOf(i)] = val), acc),
-      Array.from({ length: indexes.length })
-    )
-  );
+

rearg

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.

const rearg = (fn, indexes) => (...args) => fn(...indexes.map(i => args[i]));
 
var rearged = rearg(
   function(a, b, c) {
     return [a, b, c];