From d47f85acc06f384dcb90eda9c11dcd56d5ba5d62 Mon Sep 17 00:00:00 2001 From: 30secondsofcode <30secondsofcode@gmail.com> Date: Tue, 23 Jan 2018 15:34:14 +0000 Subject: [PATCH] Travis build: 1337 --- README.md | 14 +++++++++----- docs/index.html | 11 +++++++---- 2 files changed, 16 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index dfbf147c2..fc9d10648 100644 --- a/README.md +++ b/README.md @@ -4354,14 +4354,17 @@ get(obj, 'selector.to.val', 'target[0]', 'target[2].a'); // ['val to select', 1, ### invertKeyValues -Inverts the key-value pairs of an object, without mutating it. +Inverts the key-value pairs of an object, without mutating it. The corresponding inverted value of each inverted key is an array of keys responsible for generating the inverted value. If a function is supplied, it is applied to each inverted key. -Use `Object.keys()` and `Array.reduce()` to invert the key-value pairs of an object. +Use `Object.keys()` and `Array.reduce()` to invert the key-value pairs of an object and apply the function provided (if any). +Omit the second argument, `fn`, to get the inverted keys without applying a function to them. ```js -const invertKeyValues = obj => +const invertKeyValues = (obj, fn) => Object.keys(obj).reduce((acc, key) => { - acc[obj[key]] = key; + const val = fn ? fn(obj[key]) : obj[key]; + acc[val] = acc[val] || []; + acc[val].push(key); return acc; }, {}); ``` @@ -4370,7 +4373,8 @@ const invertKeyValues = obj => Examples ```js -invertKeyValues({ name: 'John', age: 20 }); // { 20: 'age', John: 'name' } +invertKeyValues({ a: 1, b: 2, c: 1 }); // { 1: [ 'a', 'c' ], 2: [ 'b' ] } +invertKeyValues({ a: 1, b: 2, c: 1 }, value => 'group' + value); // { group1: [ 'a', 'c' ], group2: [ 'b' ] } ``` diff --git a/docs/index.html b/docs/index.html index ff75776a2..90e378a0e 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 ]
@@ -978,12 +978,15 @@ Foo.prototypeShow examples
const obj = { selector: { to: { val: 'val to select' } }, target: [1, 2, { a: 'test' }] };
 get(obj, 'selector.to.val', 'target[0]', 'target[2].a'); // ['val to select', 1, 'test']
-

invertKeyValues

Inverts the key-value pairs of an object, without mutating it.

Use Object.keys() and Array.reduce() to invert the key-value pairs of an object.

const invertKeyValues = obj =>
+

invertKeyValues

Inverts the key-value pairs of an object, without mutating it. The corresponding inverted value of each inverted key is an array of keys responsible for generating the inverted value. If a function is supplied, it is applied to each inverted key.

Use Object.keys() and Array.reduce() to invert the key-value pairs of an object and apply the function provided (if any). Omit the second argument, fn, to get the inverted keys without applying a function to them.

const invertKeyValues = (obj, fn) =>
   Object.keys(obj).reduce((acc, key) => {
-    acc[obj[key]] = key;
+    const val = fn ? fn(obj[key]) : obj[key];
+    acc[val] = acc[val] || [];
+    acc[val].push(key);
     return acc;
   }, {});
-
invertKeyValues({ name: 'John', age: 20 }); // { 20: 'age', John: 'name' }
+
invertKeyValues({ a: 1, b: 2, c: 1 }); // { 1: [ 'a', 'c' ], 2: [ 'b' ] }
+invertKeyValues({ a: 1, b: 2, c: 1 }, value => 'group' + value); // { group1: [ 'a', 'c' ], group2: [ 'b' ] }
 

lowercaseKeys

Creates a new object from the specified object, where all the keys are in lowercase.

Use Object.keys() and Array.reduce() to create a new object from the specified object. Convert each key in the original object to lowercase, using String.toLowerCase().

const lowercaseKeys = obj =>
   Object.keys(obj).reduce((acc, key) => {
     acc[key.toLowerCase()] = obj[key];