Build
This commit is contained in:
30
README.md
30
README.md
@ -112,6 +112,7 @@
|
|||||||
* [`cleanObj`](#cleanobj)
|
* [`cleanObj`](#cleanobj)
|
||||||
* [`objectFromPairs`](#objectfrompairs)
|
* [`objectFromPairs`](#objectfrompairs)
|
||||||
* [`objectToPairs`](#objecttopairs)
|
* [`objectToPairs`](#objecttopairs)
|
||||||
|
* [`orderBy`](#orderby)
|
||||||
* [`select`](#select)
|
* [`select`](#select)
|
||||||
* [`shallowClone`](#shallowclone)
|
* [`shallowClone`](#shallowclone)
|
||||||
* [`truthCheckCollection`](#truthcheckcollection)
|
* [`truthCheckCollection`](#truthcheckcollection)
|
||||||
@ -1534,6 +1535,35 @@ const objectToPairs = obj => Object.keys(obj).map(k => [k, obj[k]]);
|
|||||||
// objectToPairs({a: 1, b: 2}) -> [['a',1],['b',2]])
|
// objectToPairs({a: 1, b: 2}) -> [['a',1],['b',2]])
|
||||||
```
|
```
|
||||||
|
|
||||||
|
[⬆ back to top](#table-of-contents)
|
||||||
|
|
||||||
|
### orderBy
|
||||||
|
|
||||||
|
Returns a sorted array of objects ordered by properties and orders.
|
||||||
|
|
||||||
|
Uses a custom implementation of sort, that reduces the props array argument with a default value of 0, it uses destructuring to swap the properties position depending on the order passed.
|
||||||
|
If no orders array is passed it sort by 'asc' by default.
|
||||||
|
|
||||||
|
```js
|
||||||
|
const orderBy = (arr, props, orders) =>
|
||||||
|
arr.sort((a, b) =>
|
||||||
|
props.reduce((acc, prop, i) => {
|
||||||
|
if (acc === 0) {
|
||||||
|
const [p1, p2] = orders[i] === 'asc' ? [a[prop], b[prop]] : [b[prop], a[prop]];
|
||||||
|
acc = p1 > p2 ? 1 : p1 < p2 ? -1 : 0;
|
||||||
|
}
|
||||||
|
return acc;
|
||||||
|
}, 0)
|
||||||
|
);
|
||||||
|
/*
|
||||||
|
const users = [{ 'name': 'fred', 'age': 48 },{ 'name': 'barney', 'age': 36 },
|
||||||
|
{ 'name': 'fred', 'age': 40 },{ 'name': 'barney', 'age': 34 }];
|
||||||
|
orderby(users, ['name', 'age'], ['asc', 'desc']) -> [{name: 'barney', age: 36}, {name: 'barney', age: 34}, {name: 'fred', age: 48}, {name: 'fred', age: 40}]
|
||||||
|
orderby(users, ['name', 'age']) -> [{name: 'barney', age: 34}, {name: 'barney', age: 36}, {name: 'fred', age: 40}, {name: 'fred', age: 48}]
|
||||||
|
*/
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
[⬆ back to top](#table-of-contents)
|
[⬆ back to top](#table-of-contents)
|
||||||
|
|
||||||
### select
|
### select
|
||||||
|
|||||||
@ -154,6 +154,7 @@
|
|||||||
</h3><a class="sublink-1" href="#cleanobj">cleanObj</a>
|
</h3><a class="sublink-1" href="#cleanobj">cleanObj</a>
|
||||||
<a class="sublink-1" href="#objectfrompairs">objectFromPairs</a>
|
<a class="sublink-1" href="#objectfrompairs">objectFromPairs</a>
|
||||||
<a class="sublink-1" href="#objecttopairs">objectToPairs</a>
|
<a class="sublink-1" href="#objecttopairs">objectToPairs</a>
|
||||||
|
<a class="sublink-1" href="#orderby">orderBy</a>
|
||||||
<a class="sublink-1" href="#select">select</a>
|
<a class="sublink-1" href="#select">select</a>
|
||||||
<a class="sublink-1" href="#shallowclone">shallowClone</a>
|
<a class="sublink-1" href="#shallowclone">shallowClone</a>
|
||||||
<a class="sublink-1" href="#truthcheckcollection">truthCheckCollection</a>
|
<a class="sublink-1" href="#truthcheckcollection">truthCheckCollection</a>
|
||||||
@ -964,6 +965,27 @@ Also if you give it a special key (<code>childIndicator</code>) it will search d
|
|||||||
<pre><code class="language-js">const objectToPairs = obj => Object.keys(obj).map(k => [k, obj[k]]);
|
<pre><code class="language-js">const objectToPairs = obj => Object.keys(obj).map(k => [k, obj[k]]);
|
||||||
// objectToPairs({a: 1, b: 2}) -> [['a',1],['b',2]])
|
// objectToPairs({a: 1, b: 2}) -> [['a',1],['b',2]])
|
||||||
</code></pre>
|
</code></pre>
|
||||||
|
</div></div><br/><div class="card fluid"><div class="section double-padded"><h3 id="orderby">orderBy</h3></div><div class="section double-padded">
|
||||||
|
<p>Returns a sorted array of objects ordered by properties and orders.</p>
|
||||||
|
<p>Uses a custom implementation of sort, that reduces the props array argument with a default value of 0, it uses destructuring to swap the properties position depending on the order passed.
|
||||||
|
If no orders array is passed it sort by 'asc' by default.</p>
|
||||||
|
<pre><code class="language-js">const orderBy = (arr, props, orders) =>
|
||||||
|
arr.sort((a, b) =>
|
||||||
|
props.reduce((acc, prop, i) => {
|
||||||
|
if (acc === 0) {
|
||||||
|
const [p1, p2] = orders[i] === 'asc' ? [a[prop], b[prop]] : [b[prop], a[prop]];
|
||||||
|
acc = p1 > p2 ? 1 : p1 < p2 ? -1 : 0;
|
||||||
|
}
|
||||||
|
return acc;
|
||||||
|
}, 0)
|
||||||
|
);
|
||||||
|
/*
|
||||||
|
const users = [{ 'name': 'fred', 'age': 48 },{ 'name': 'barney', 'age': 36 },
|
||||||
|
{ 'name': 'fred', 'age': 40 },{ 'name': 'barney', 'age': 34 }];
|
||||||
|
orderby(users, ['name', 'age'], ['asc', 'desc']) -> [{name: 'barney', age: 36}, {name: 'barney', age: 34}, {name: 'fred', age: 48}, {name: 'fred', age: 40}]
|
||||||
|
orderby(users, ['name', 'age']) -> [{name: 'barney', age: 34}, {name: 'barney', age: 36}, {name: 'fred', age: 40}, {name: 'fred', age: 48}]
|
||||||
|
*/
|
||||||
|
</code></pre>
|
||||||
</div></div><br/><div class="card fluid"><div class="section double-padded"><h3 id="select">select</h3></div><div class="section double-padded">
|
</div></div><br/><div class="card fluid"><div class="section double-padded"><h3 id="select">select</h3></div><div class="section double-padded">
|
||||||
<p>Retrieve a property that indicated by the selector from object.</p>
|
<p>Retrieve a property that indicated by the selector from object.</p>
|
||||||
<p>If property not exists returns <code>undefined</code>.</p>
|
<p>If property not exists returns <code>undefined</code>.</p>
|
||||||
|
|||||||
@ -71,6 +71,7 @@ median:math
|
|||||||
nthElement:array
|
nthElement:array
|
||||||
objectFromPairs:object
|
objectFromPairs:object
|
||||||
objectToPairs:object
|
objectToPairs:object
|
||||||
|
orderBy:object
|
||||||
palindrome:math
|
palindrome:math
|
||||||
percentile:math
|
percentile:math
|
||||||
pick:array
|
pick:array
|
||||||
|
|||||||
Reference in New Issue
Block a user