diff --git a/README.md b/README.md index dc1bd4f18..be766e7a6 100644 --- a/README.md +++ b/README.md @@ -283,7 +283,6 @@ average(1, 2, 3); * [`objectFromPairs`](#objectfrompairs) * [`objectToPairs`](#objecttopairs) * [`orderBy`](#orderby) -* [`select`](#select) * [`shallowClone`](#shallowclone) * [`size`](#size) * [`transform`](#transform) @@ -372,6 +371,15 @@ average(1, 2, 3); +### _Uncategorized_ + +
+View contents + +* [`get`](#get) + +
+ --- ## 🔌 Adapter @@ -436,20 +444,20 @@ Flip takes a function as an argument, then makes the first argument the last. Return a closure that takes variadic inputs, and splices the last argument to make it the first argument before applying the rest. ```js -const flip = fn => (first, ...rest) => fn(...rest, first); +const flip = fn => (first, ...rest) => fn(...rest, first); ```
Examples ```js -let a = { name: 'John Smith' }; -let b = {}; -const mergeFrom = flip(Object.assign); -let mergePerson = mergeFrom.bind(null, a); -mergePerson(b); // == b -b = {}; -Object.assign(b, a); // == b +let a = { name: 'John Smith' }; +let b = {}; +const mergeFrom = flip(Object.assign); +let mergePerson = mergeFrom.bind(null, a); +mergePerson(b); // == b +b = {}; +Object.assign(b, a); // == b ```
@@ -4485,31 +4493,6 @@ orderBy(users, ['name', 'age']); // [{name: 'barney', age: 36}, {name: 'fred', a
[⬆ Back to top](#table-of-contents) -### select - -Retrieve a set of properties indicated by the given selectors from an object. - -Use `Array.map()` for each selector, `String.split('.')` to split each selector and `Array.reduce()` to get the value indicated by it. - -```js -const select = (from, ...selectors) => - [...selectors].map(s => s.split('.').reduce((prev, cur) => prev && prev[cur], from)); -``` - -
-Examples - -```js -const obj = { selector: { to: { val: 'val to select' } } }; -select(obj, 'selector.to.val'); // ['val to select'] -select(obj, 'selector.to.val', 'selector.to'); // ['val to select', { val: 'val to select' }] -``` - -
- -
[⬆ Back to top](#table-of-contents) - - ### shallowClone Creates a shallow clone of an object. @@ -6200,6 +6183,33 @@ 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 89cf4a175..6e3b2d4bc 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 ]
@@ -1016,11 +1016,6 @@ Foo.prototypeShow examples
const users = [{ name: 'fred', age: 48 }, { name: 'barney', age: 36 }, { name: 'fred', age: 40 }];
 orderBy(users, ['name', 'age'], ['asc', 'desc']); // [{name: 'barney', age: 36}, {name: 'fred', age: 48}, {name: 'fred', age: 40}]
 orderBy(users, ['name', 'age']); // [{name: 'barney', age: 36}, {name: 'fred', age: 40}, {name: 'fred', age: 48}]
-

select

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

Use Array.map() for each selector, String.split('.') to split each selector and Array.reduce() to get the value indicated by it.

const select = (from, ...selectors) =>
-  [...selectors].map(s => s.split('.').reduce((prev, cur) => prev && prev[cur], from));
-
const obj = { selector: { to: { val: 'val to select' } } };
-select(obj, 'selector.to.val'); // ['val to select']
-select(obj, 'selector.to.val', 'selector.to'); // ['val to select', { val: 'val to select' }]
 

shallowClone

Creates a shallow clone of an object.

Use Object.assign() and an empty object ({}) to create a shallow clone of the original.

const shallowClone = obj => Object.assign({}, obj);
 
const a = { x: true, y: 1 };
 const b = shallowClone(a); // a !== b
@@ -1443,4 +1438,14 @@ Logs: {
 yesNo('yes'); // true
 yesNo('No'); // false
 yesNo('Foo', true); // true
-
\ No newline at end of file +

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 diff --git a/snippets/flip.md b/snippets/flip.md index 7473527ef..109471dba 100644 --- a/snippets/flip.md +++ b/snippets/flip.md @@ -5,15 +5,15 @@ Flip takes a function as an argument, then makes the first argument the last. Return a closure that takes variadic inputs, and splices the last argument to make it the first argument before applying the rest. ```js -const flip = fn => (first, ...rest) => fn(...rest, first); +const flip = fn => (first, ...rest) => fn(...rest, first); ``` ```js -let a = { name: 'John Smith' }; -let b = {}; -const mergeFrom = flip(Object.assign); -let mergePerson = mergeFrom.bind(null, a); -mergePerson(b); // == b -b = {}; -Object.assign(b, a); // == b +let a = { name: 'John Smith' }; +let b = {}; +const mergeFrom = flip(Object.assign); +let mergePerson = mergeFrom.bind(null, a); +mergePerson(b); // == b +b = {}; +Object.assign(b, a); // == b ``` diff --git a/snippets/get.md b/snippets/get.md index 48a485cd6..803276536 100644 --- a/snippets/get.md +++ b/snippets/get.md @@ -16,6 +16,6 @@ const get = (from, ...selectors) => ``` ```js -const obj = { selector: { to: { val: 'val to select' } }, target: [1, 2, {a: 'test'}] }; +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'] ``` diff --git a/tag_database b/tag_database index fe9a9ef38..7cff35c12 100644 --- a/tag_database +++ b/tag_database @@ -57,6 +57,7 @@ functionName:function,utility functions:object,function gcd:math,recursion geometricProgression:math +get:uncategorized getDaysDiffBetweenDates:date getScrollPosition:browser getStyle:browser,css @@ -164,7 +165,6 @@ sample:array,random sampleSize:array,random scrollToTop:browser sdbm:math,utility -select:object serializeCookie:utility,string setStyle:browser shallowClone:object