diff --git a/README.md b/README.md index be766e7a6..0896ddf02 100644 --- a/README.md +++ b/README.md @@ -275,6 +275,7 @@ average(1, 2, 3); * [`cleanObj`](#cleanobj) * [`equals`](#equals-) * [`functions`](#functions) +* [`get`](#get) * [`invertKeyValues`](#invertkeyvalues) * [`lowercaseKeys`](#lowercasekeys) * [`mapKeys`](#mapkeys) @@ -371,15 +372,6 @@ average(1, 2, 3); -### _Uncategorized_ - -
-View contents - -* [`get`](#get) - -
- --- ## 🔌 Adapter @@ -4263,6 +4255,36 @@ functions(new Foo(), true); // ['a', 'b', 'c']
[⬆ Back to top](#table-of-contents) +### get + +Retrieve a set of properties indicated by the given selectors from an object. + +Use `Array.map()` for each selector, `String.replace()` to replace square brackets with dots, `String.split('.')` to split each selector, `Array.filter()` to remove empty values and `Array.reduce()` to get the value indicated by it. + +```js +const get = (from, ...selectors) => + [...selectors].map(s => + s + .replace(/\[([^\[\]]*)\]/g, '.$1.') + .split('.') + .filter(t => t !== '') + .reduce((prev, cur) => prev && prev[cur], from) + ); +``` + +
+Examples + +```js +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'] +``` + +
+ +
[⬆ Back to top](#table-of-contents) + + ### invertKeyValues Inverts the key-value pairs of an object, without mutating it. @@ -6183,33 +6205,6 @@ yesNo('Foo', true); // true
[⬆ Back to top](#table-of-contents) ---- - ## _Uncategorized_ - -### get - -Retrieve a set of properties indicated by the given selectors from an object. - -Use `Array.map()` for each selector, `String.replace()` to replace square brackets with dots, `String.split('.')` to split each selector, `Array.filter()` to remove empty values and `Array.reduce()` to get the value indicated by it. - -```js -const get = (from, ...selectors) => - [...selectors].map(s => - s - .replace(/\[([^\[\]]*)\]/g, '.$1.') - .split('.') - .filter(t => t !== '') - .reduce((prev, cur) => prev && prev[cur], from) - ); -``` - -```js -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'] -``` - -
[⬆ back to top](#table-of-contents) - ## Collaborators diff --git a/docs/index.html b/docs/index.html index 6e3b2d4bc..60ef59e4e 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 ]
@@ -951,6 +951,16 @@ console.log<
 Foo.prototype.c = () => 3;
 functions(new Foo()); // ['a', 'b']
 functions(new Foo(), true); // ['a', 'b', 'c']
+

get

Retrieve a set of properties indicated by the given selectors from an object.

Use Array.map() for each selector, String.replace() to replace square brackets with dots, String.split('.') to split each selector, Array.filter() to remove empty values and Array.reduce() to get the value indicated by it.

const get = (from, ...selectors) =>
+  [...selectors].map(s =>
+    s
+      .replace(/\[([^\[\]]*)\]/g, '.$1.')
+      .split('.')
+      .filter(t => t !== '')
+      .reduce((prev, cur) => prev && prev[cur], from)
+  );
+
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 =>
   Object.keys(obj).reduce((acc, key) => {
     acc[obj[key]] = key;
@@ -1438,14 +1448,4 @@ Logs: {
 yesNo('yes'); // true
 yesNo('No'); // false
 yesNo('Foo', true); // true
-

Uncategorized

get

Retrieve a set of properties indicated by the given selectors from an object.

Use Array.map() for each selector, String.replace() to replace square brackets with dots, String.split('.') to split each selector, Array.filter() to remove empty values and Array.reduce() to get the value indicated by it.

const get = (from, ...selectors) =>
-  [...selectors].map(s =>
-    s
-      .replace(/\[([^\[\]]*)\]/g, '.$1.')
-      .split('.')
-      .filter(t => t !== '')
-      .reduce((prev, cur) => prev && prev[cur], from)
-  );
-
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']
-
\ No newline at end of file +
\ No newline at end of file