diff --git a/README.md b/README.md index 22b6a898a..9c32997bd 100644 --- a/README.md +++ b/README.md @@ -272,6 +272,7 @@ average(1, 2, 3); * [`select`](#select) * [`shallowClone`](#shallowclone) * [`size`](#size) +* [`transform`](#transform) * [`truthCheckCollection`](#truthcheckcollection) @@ -4230,6 +4231,35 @@ size({ one: 1, two: 2, three: 3 }); // 3
[⬆ Back to top](#table-of-contents) +### transform + +Applies a function against an accumulator and each key in the object (from left to right). + +Use `Object.keys(obj)` to iterate over each key in the object, `Array.reduce()` to call the apply the specified function against the given accumulator. + +```js +const transform = (obj, fn, acc) => Object.keys(obj).reduce((a, k) => fn(a, obj[k], k, obj), acc); +``` + +
+Examples + +```js +transform( + { a: 1, b: 2, c: 1 }, + (r, v, k) => { + (r[v] || (r[v] = [])).push(k); + return r; + }, + {} +); // { '1': ['a', 'c'], '2': ['b'] } +``` + +
+ +
[⬆ Back to top](#table-of-contents) + + ### truthCheckCollection Checks if the predicate (second argument) is truthy on all elements of a collection (first argument). diff --git a/docs/index.html b/docs/index.html index fc405c7be..6f54b83cb 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

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);
+      }

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

 

Adapter

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);
 
Promise.resolve([1, 2, 3])
   .then(call('map', x => 2 * x))
   .then(console.log); //[ 2, 4, 6 ]
@@ -924,6 +924,15 @@ Foo.prototypeShow examples
size([1, 2, 3, 4, 5]); // 5
 size('size'); // 4
 size({ one: 1, two: 2, three: 3 }); // 3
+

transform

Applies a function against an accumulator and each key in the object (from left to right).

Use Object.keys(obj) to iterate over each key in the object, Array.reduce() to call the apply the specified function against the given accumulator.

const transform = (obj, fn, acc) => Object.keys(obj).reduce((a, k) => fn(a, obj[k], k, obj), acc);
+
transform(
+  { a: 1, b: 2, c: 1 },
+  (r, v, k) => {
+    (r[v] || (r[v] = [])).push(k);
+    return r;
+  },
+  {}
+); // { '1': ['a', 'c'], '2': ['b'] }
 

truthCheckCollection

Checks if the predicate (second argument) is truthy on all elements of a collection (first argument).

Use Array.every() to check if each passed object has the specified property and if it returns a truthy value.

const truthCheckCollection = (collection, pre) => collection.every(obj => obj[pre]);
 
truthCheckCollection([{ user: 'Tinky-Winky', sex: 'male' }, { user: 'Dipsy', sex: 'male' }], 'sex'); // true
 

String

anagrams

⚠️ WARNING: This function's execution time increases exponentially with each character. Anything more than 8 to 10 characters will cause your browser to hang as it tries to solve all the different combinations.

Generates all anagrams of a string (contains duplicates).

Use recursion. For each letter in the given string, create all the partial anagrams for the rest of its letters. Use Array.map() to combine the letter with each partial anagram, then Array.reduce() to combine all anagrams in one array. Base cases are for string length equal to 2 or 1.

const anagrams = str => {
diff --git a/snippets/transform.md b/snippets/transform.md
index de8d1236d..0e7573494 100644
--- a/snippets/transform.md
+++ b/snippets/transform.md
@@ -5,13 +5,16 @@ Applies a function against an accumulator and each key in the object (from left
 Use `Object.keys(obj)` to iterate over each key in the object, `Array.reduce()` to call the apply the specified function against the given accumulator.
 
 ```js
-const transform = (obj, fn, acc) =>
-  Object.keys(obj).reduce((a, k) => fn(a, obj[k], k, obj), acc);
+const transform = (obj, fn, acc) => Object.keys(obj).reduce((a, k) => fn(a, obj[k], k, obj), acc);
 ```
 
 ```js
-transform({ 'a': 1, 'b': 2, 'c': 1 }, (r, v, k) => {
-  (r[v] || (r[v] = [])).push(k);
-  return r;
-}, {}); // { '1': ['a', 'c'], '2': ['b'] }
+transform(
+  { a: 1, b: 2, c: 1 },
+  (r, v, k) => {
+    (r[v] || (r[v] = [])).push(k);
+    return r;
+  },
+  {}
+); // { '1': ['a', 'c'], '2': ['b'] }
 ```