From fe193fcbcdf86012448d06638d43db4078eb7079 Mon Sep 17 00:00:00 2001 From: 30secondsofcode <30secondsofcode@gmail.com> Date: Fri, 26 Jan 2018 12:16:06 +0000 Subject: [PATCH] Travis build: 1441 --- README.md | 36 ++++++++++++++++++++++++++++++++++++ docs/index.html | 19 +++++++++++++++++-- snippets/bindAll.md | 4 ++-- tag_database | 2 +- 4 files changed, 56 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index f0fba24b5..4968bf09f 100644 --- a/README.md +++ b/README.md @@ -304,6 +304,7 @@ average(1, 2, 3);
View contents +* [`bindAll`](#bindall) * [`deepClone`](#deepclone) * [`defaults`](#defaults) * [`equals`](#equals-) @@ -5100,6 +5101,41 @@ UUIDGeneratorNode(); // '79c7c136-60ee-40a2-beb2-856f1feabefc' --- ## 🗃️ Object +### bindAll + +Explain briefly what the snippet does. + +Use `Array.forEach()` to return a `function` that uses `Function.apply()` to apply the given context (`obj`) to `fn` for each function specified. + +```js +const bindAll = (obj, ...fns) => + fns.forEach( + fn => + (obj[fn] = function() { + return fn.apply(obj); + }) + ); +``` + +
+Examples + +```js +var view = { + label: 'docs', + click: function() { + console.log('clicked ' + this.label); + } +}; +bindAll(view, 'click'); +jQuery(element).on('click', view.click); // Logs 'clicked docs' when clicked. +``` + +
+ +
[⬆ Back to top](#table-of-contents) + + ### deepClone Creates a deep clone of an object. diff --git a/docs/index.html b/docs/index.html index d23af6397..202787e70 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);
@@ -1137,7 +1137,22 @@ console.log<
     (c ^ (crypto.randomBytes(1)[0] & (15 >> (c / 4)))).toString(16)
   );
 
UUIDGeneratorNode(); // '79c7c136-60ee-40a2-beb2-856f1feabefc'
-

Object

deepClone

Creates a deep clone of an object.

Use recursion. Use Object.assign() and an empty object ({}) to create a shallow clone of the original. Use Object.keys() and Array.forEach() to determine which key-value pairs need to be deep cloned.

const deepClone = obj => {
+

Object

bindAll

Explain briefly what the snippet does.

Use Array.forEach() to return a function that uses Function.apply() to apply the given context (obj) to fn for each function specified.

const bindAll = (obj, ...fns) =>
+  fns.forEach(
+    fn =>
+      (obj[fn] = function() {
+        return fn.apply(obj);
+      })
+  );
+
var view = {
+  label: 'docs',
+  click: function() {
+    console.log('clicked ' + this.label);
+  }
+};
+bindAll(view, 'click');
+jQuery(element).on('click', view.click); // Logs 'clicked docs' when clicked.
+

deepClone

Creates a deep clone of an object.

Use recursion. Use Object.assign() and an empty object ({}) to create a shallow clone of the original. Use Object.keys() and Array.forEach() to determine which key-value pairs need to be deep cloned.

const deepClone = obj => {
   let clone = Object.assign({}, obj);
   Object.keys(clone).forEach(
     key => (clone[key] = typeof obj[key] === 'object' ? deepClone(obj[key]) : obj[key])
diff --git a/snippets/bindAll.md b/snippets/bindAll.md
index 0fcd01a90..e5e618ae0 100644
--- a/snippets/bindAll.md
+++ b/snippets/bindAll.md
@@ -16,8 +16,8 @@ const bindAll = (obj, ...fns) =>
 
 ```js
 var view = {
-  'label': 'docs',
-  'click': function() {
+  label: 'docs',
+  click: function() {
     console.log('clicked ' + this.label);
   }
 };
diff --git a/tag_database b/tag_database
index 9a84845df..5cef0dae1 100644
--- a/tag_database
+++ b/tag_database
@@ -215,7 +215,7 @@ sortedLastIndexBy:array,math,function
 splitLines:string
 spreadOver:adapter
 standardDeviation:math,array
-stripHTMLTags:string,utility,regexp
+stripHTMLtags:uncategorized
 sum:math,array
 sumBy:math,array,function
 sumPower:math