diff --git a/README.md b/README.md index 39697ed58..8adf0c58b 100644 --- a/README.md +++ b/README.md @@ -218,6 +218,7 @@ average(1, 2, 3); View contents * [`bind`](#bind) +* [`bindKey`](#bindkey) * [`chainAsync`](#chainasync) * [`compose`](#compose) * [`composeRight`](#composeright) @@ -3185,6 +3186,39 @@ console.log(freddyBound('hi', '!')); // 'hi fred!'
[⬆ Back to top](#table-of-contents) +### bindKey + +Creates a function that invokes the method at a given key of an object, optionally adding any additional supplied parameters to the beginning of the arguments. + +Return a `function` that uses `Function.apply()` to bind `context[fn]` to `context`. +Use `Array.concat()` to prepend any additional supplied parameters to the arguments. + +```js +const bindKey = (context, fn, ...args) => + function() { + return context[fn].apply(context, args.concat(...arguments)); + }; +``` + +
+Examples + +```js +const freddy = { + user: 'fred', + greet: function(greeting, punctuation) { + return greeting + ' ' + this.user + punctuation; + } +}; +const freddyBound = bindKey(freddy, 'greet'); +console.log(freddyBound('hi', '!')); // 'hi fred!' +``` + +
+ +
[⬆ Back to top](#table-of-contents) + + ### chainAsync Chains asynchronous functions. diff --git a/docs/index.html b/docs/index.html index 708ed4bb5..9daf0db4e 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);
@@ -701,6 +701,18 @@ document.bodyconst freddy = { user: 'fred' };
 const freddyBound = bind(greet, freddy);
 console.log(freddyBound('hi', '!')); // 'hi fred!'
+

bindKey

Creates a function that invokes the method at a given key of an object, optionally adding any additional supplied parameters to the beginning of the arguments.

Return a function that uses Function.apply() to bind context[fn] to context. Use Array.concat() to prepend any additional supplied parameters to the arguments.

const bindKey = (context, fn, ...args) =>
+  function() {
+    return context[fn].apply(context, args.concat(...arguments));
+  };
+
const freddy = {
+  user: 'fred',
+  greet: function(greeting, punctuation) {
+    return greeting + ' ' + this.user + punctuation;
+  }
+};
+const freddyBound = bindKey(freddy, 'greet');
+console.log(freddyBound('hi', '!')); // 'hi fred!'
 

chainAsync

Chains asynchronous functions.

Loop through an array of functions containing asynchronous events, calling next when each asynchronous event has completed.

const chainAsync = fns => {
   let curr = 0;
   const next = () => fns[curr++](next);
diff --git a/snippets/bindKey.md b/snippets/bindKey.md
index 2ff80f861..147ad2ff7 100644
--- a/snippets/bindKey.md
+++ b/snippets/bindKey.md
@@ -17,7 +17,7 @@ const freddy = {
   user: 'fred',
   greet: function(greeting, punctuation) {
     return greeting + ' ' + this.user + punctuation;
-  },
+  }
 };
 const freddyBound = bindKey(freddy, 'greet');
 console.log(freddyBound('hi', '!')); // 'hi fred!'