diff --git a/README.md b/README.md index 69cd7b84d..e1a4779c6 100644 --- a/README.md +++ b/README.md @@ -82,6 +82,7 @@ average(1, 2, 3); * [`collectInto`](#collectinto) * [`flip`](#flip) * [`over`](#over) +* [`overArgs`](#overargs) * [`pipeAsyncFunctions`](#pipeasyncfunctions) * [`pipeFunctions`](#pipefunctions) * [`promisify`](#promisify) @@ -556,6 +557,34 @@ minMax(1, 2, 3, 4, 5); // [1,5]
[⬆ Back to top](#table-of-contents) +### overArgs + +Creates a function that invokes the provided function with its arguments transformed. + +Use `Array.map()` to apply `transforms` to `args` in combination with the spread operator (`...`) to pass the transformed arguments to `fn`. + +```js +const overArgs = (fn, transforms) => (...args) => fn(...args.map((val, i) => transforms[i](val))); +``` + +
+Examples + +```js +var func = overArgs( + function(x, y) { + return [x, y]; + }, + [square, doubled] +); +func(9, 3); // [81, 6] +``` + +
+ +
[⬆ Back to top](#table-of-contents) + + ### pipeAsyncFunctions Performs left-to-right function composition for asynchronous functions. diff --git a/docs/index.html b/docs/index.html index 89387e63a..ca0cc9869 100644 --- a/docs/index.html +++ b/docs/index.html @@ -50,7 +50,7 @@ scrollToTop(); } }, false); - }

logo 30 seconds of code Curated collection of useful JavaScript snippets that you can understand in 30 seconds or less.

 

Adapter

ary

Creates a function that accepts up to n arguments, ignoring any additional arguments.

Call the provided function, fn, with up to n arguments, using Array.slice(0,n) and the spread operator (...).

const ary = (fn, n) => (...args) => fn(...args.slice(0, n));
+      }

logo 30 seconds of code Curated collection of useful JavaScript snippets that you can understand in 30 seconds or less.

 

Adapter

ary

Creates a function that accepts up to n arguments, ignoring any additional arguments.

Call the provided function, fn, with up to n arguments, using Array.slice(0,n) and the spread operator (...).

const ary = (fn, n) => (...args) => fn(...args.slice(0, n));
 
const firstTwoMax = ary(Math.max, 2);
 [[2, 6, 'a'], [8, 4, 6], [10]].map(x => firstTwoMax(...x)); // [6, 8, 10]
 

call

Given a key and a set of arguments, call them when given a context. Primarily useful in composition.

Use a closure to call a stored key with stored arguments.

const call = (key, ...args) => context => context[key](...args);
@@ -78,6 +78,14 @@ Object.assig
 

over

Creates a function that invokes each provided function with the arguments it receives and returns the results.

Use Array.map() and Function.apply() to apply each function to the given arguments.

const over = (...fns) => (...args) => fns.map(fn => fn.apply(null, args));
 
const minMax = over(Math.min, Math.max);
 minMax(1, 2, 3, 4, 5); // [1,5]
+

overArgs

Creates a function that invokes the provided function with its arguments transformed.

Use Array.map() to apply transforms to args in combination with the spread operator (...) to pass the transformed arguments to fn.

const overArgs = (fn, transforms) => (...args) => fn(...args.map((val, i) => transforms[i](val)));
+
var func = overArgs(
+  function(x, y) {
+    return [x, y];
+  },
+  [square, doubled]
+);
+func(9, 3); // [81, 6]
 

pipeAsyncFunctions

Performs left-to-right function composition for asynchronous functions.

Use Array.reduce() with the spread operator (...) to perform left-to-right function composition using Promise.then(). The functions can return a combination of: simple values, Promise's, or they can be defined as async ones returning through await. All functions must be unary.

const pipeAsyncFunctions = (...fns) => arg => fns.reduce((p, f) => p.then(f), Promise.resolve(arg));
 
const sum = pipeAsyncFunctions(
   x => x + 1,
diff --git a/snippets/overArgs.md b/snippets/overArgs.md
index 0c54fa726..9a913e049 100644
--- a/snippets/overArgs.md
+++ b/snippets/overArgs.md
@@ -5,8 +5,7 @@ Creates a function that invokes the provided function with its arguments transfo
 Use `Array.map()` to apply `transforms` to `args` in combination with the spread operator (`...`) to pass the transformed arguments to `fn`.
 
 ```js
-const overArgs = (fn, transforms) => (...args) =>
-  fn(...args.map((val, i) => transforms[i](val)));
+const overArgs = (fn, transforms) => (...args) => fn(...args.map((val, i) => transforms[i](val)));
 ```
 
 ```js