sort-array
Advanced tools
Comparing version 2.0.0 to 3.0.0-0
{ | ||
"name": "sort-array", | ||
"author": "Lloyd Brookes <75pound@gmail.com>", | ||
"version": "2.0.0", | ||
"contributors": [ | ||
"Ben Vickers <bjvickers@protonmail.com>" | ||
], | ||
"version": "3.0.0-0", | ||
"description": "Sort an array of objects by any property value, at any depth, in any custom order.", | ||
"repository": "https://github.com/75lb/sort-array.git", | ||
"license": "MIT", | ||
"main": "./lib/sort-array.js", | ||
"main": "dist/index.js", | ||
"module": "index.mjs", | ||
"keywords": [ | ||
@@ -20,17 +24,17 @@ "sort", | ||
"engines": { | ||
"node": ">=4" | ||
"node": ">=8" | ||
}, | ||
"scripts": { | ||
"lint": "standard --fix index.mjs test/*.js", | ||
"test": "test-runner test/*.js", | ||
"docs": "jsdoc2md -t jsdoc2md/README.hbs lib/*.js > README.md; echo" | ||
"docs": "jsdoc2md -t README.hbs index.js > README.md; echo", | ||
"dist": "rollup -f umd -o dist/index.js -n sortArray index.mjs" | ||
}, | ||
"dependencies": { | ||
"array-back": "^1.0.4", | ||
"object-get": "^2.1.0", | ||
"typical": "^2.6.0" | ||
}, | ||
"dependencies": {}, | ||
"devDependencies": { | ||
"jsdoc-to-markdown": "^3.0.0", | ||
"test-runner": "^0.3.0" | ||
"jsdoc-to-markdown": "^5.0.1", | ||
"rollup": "^1.20.3", | ||
"standard": "^14.1.0", | ||
"test-runner": "^0.6.0" | ||
} | ||
} |
[![view on npm](http://img.shields.io/npm/v/sort-array.svg)](https://www.npmjs.org/package/sort-array) | ||
[![npm module downloads](http://img.shields.io/npm/dt/sort-array.svg)](https://www.npmjs.org/package/sort-array) | ||
[![Build Status](https://travis-ci.org/75lb/sort-array.svg?branch=master)](https://travis-ci.org/75lb/sort-array) | ||
[![Dependency Status](https://david-dm.org/75lb/sort-array.svg)](https://david-dm.org/75lb/sort-array) | ||
[![Dependency Status](https://badgen.net/david/dep/75lb/sort-array)](https://david-dm.org/75lb/sort-array) | ||
[![js-standard-style](https://img.shields.io/badge/code%20style-standard-brightgreen.svg)](https://github.com/feross/standard) | ||
@@ -10,3 +10,3 @@ | ||
## sort-array | ||
Sort an array of objects by any property value, at any depth, in any custom order. | ||
Sort an array of objects or primitives, by any property value, in any combindation of ascending, descending, custom or calculated order. | ||
@@ -19,3 +19,3 @@ **Example** | ||
### sortBy(recordset, columnNames, [customOrder]) ⇒ <code>Array</code> ⏏ | ||
### sortBy(recordset, sortBy, sortTypes, [namedConfigs]) ⇒ <code>Array</code> ⏏ | ||
**Kind**: Exported function | ||
@@ -25,5 +25,8 @@ | ||
| --- | --- | --- | | ||
| recordset | <code>Array.<object></code> | Input array of objects | | ||
| columnNames | <code>string</code> \| <code>Array.<string></code> | One or more property expressions to sort by, e.g. `'name'` or `'name.first'`. | | ||
| [customOrder] | <code>object</code> | Custom sort order definitions. An object where each key is the property expression and the value is an array specifying the sort order. Example: <br> `{ importance: [ 'speed', 'strength', 'intelligence' ]}` | | ||
| recordset | <code>Array</code> | Input array of objects or primitive values. | | ||
| sortBy | <code>Array.<(string\|function())></code> | One or more property expressions to sort by. Expressions may be strings which refer to properties in the input array; they may be strings which refer to properties in the optional `namedConfigs.namedComputedProps` parameter; or they may be inline functions which dynamically calculate values for each property in the input array. | | ||
| sortTypes | <code>Array.<(string\|Array.<\*>)></code> | The sort types for each of the sortBy expressions. Values may be 'asc', 'desc', an array of custom values, and strings which refer to properties in the optional `namedConfigs.namedCustomOrders` parameter. | | ||
| [namedConfigs] | <code>object</code> | Provides a means of reusing computed property functions and custom sort types. | | ||
| [namedConfigs.namedComputedProps] | <code>object</code> | Key/value pairs, where the keys correspond to strings given in the sortBy property list, and the values are functions which will dynamically calculated values for each property in the input array. | | ||
| [namedConfigs.namedCustomOrders] | <code>object</code> | Key/value pairs, where the keys correspond to strings given in the sortTypes list, and the values are arrays of custom values which define the sort type. | | ||
@@ -43,5 +46,5 @@ **Example** | ||
sort by `slot` using the default sort order (alphabetical) | ||
sort by `slot` using an ascending sort type | ||
```js | ||
> sortBy(DJs, 'slot') | ||
> sortBy(DJs, [ 'slot' ], [ 'asc' ]) | ||
[ { name: 'Mike', slot: 'afternoon' }, | ||
@@ -55,6 +58,16 @@ { name: 'Zane', slot: 'evening' }, | ||
specify a custom sort order for `slot` | ||
sort by `slot` using a descending sort type | ||
```js | ||
> const slotOrder = [ 'morning', 'afternoon', 'evening', 'twilight' ] | ||
> sortBy(DJs, 'slot', { slot: slotOrder }) | ||
> sortBy(DJs, [ 'slot' ], [ 'desc' ]) | ||
[ { name: 'Chris', slot: 'twilight' }, | ||
{ name: 'Trevor', slot: 'twilight' }, | ||
{ name: 'Chris', slot: 'morning' }, | ||
{ name: 'Rodney', slot: 'morning' }, | ||
{ name: 'Zane', slot: 'evening' }, | ||
{ name: 'Mike', slot: 'afternoon' }] | ||
``` | ||
sort by `slot` using an 'inline' custom sort type | ||
```js | ||
> sortBy(DJs, [ 'slot' ], [ 'morning', 'afternoon', 'evening', 'twilight' ]) | ||
[ { name: 'Rodney', slot: 'morning' }, | ||
@@ -68,5 +81,21 @@ { name: 'Chris', slot: 'morning' }, | ||
sort by `slot` then `name` | ||
sort by `slot` using an 'named' custom sort type | ||
```js | ||
> sortBy(DJs, ['slot', 'name'], { slot: slotOrder }) | ||
> let namedConfigs = { | ||
namedCustomOrders: { | ||
custOrder1: [ 'morning', 'afternoon', 'evening', 'twilight' ] | ||
} | ||
} | ||
> sortBy(DJs, [ 'slot' ], [ 'custOrder1' ], namedConfigs) | ||
[ { name: 'Rodney', slot: 'morning' }, | ||
{ name: 'Chris', slot: 'morning' }, | ||
{ name: 'Mike', slot: 'afternoon' }, | ||
{ name: 'Zane', slot: 'evening' }, | ||
{ name: 'Trevor', slot: 'twilight' }, | ||
{ name: 'Chris', slot: 'twilight' } ] | ||
``` | ||
sort by `slot` (with a custom sort type) then `name` (with an ascending sort type) | ||
```js | ||
> sortBy(DJs, ['slot', 'name'], [ [ 'morning', 'afternoon', 'evening', 'twilight' ], 'asc' ]) | ||
[ { name: 'Chris', slot: 'morning' }, | ||
@@ -80,29 +109,4 @@ { name: 'Rodney', slot: 'morning' }, | ||
sort by nested property values (at any depth) using dot notation (e.g. `'inner.number'`) | ||
```js | ||
> input = [ | ||
{ inner: { number: 5 } }, | ||
{ inner: { number: 2 } }, | ||
{ inner: { number: 3 } }, | ||
{ inner: { number: 1 } }, | ||
{ inner: { number: 4 } } | ||
] | ||
> sortBy(input, 'inner.number') | ||
[ { inner: { number: 1 } }, | ||
{ inner: { number: 2 } }, | ||
{ inner: { number: 3 } }, | ||
{ inner: { number: 4 } }, | ||
{ inner: { number: 5 } } ] | ||
``` | ||
a custom order for a nested property looks like this: | ||
```js | ||
const customOrder = { | ||
'inner.number': [ 1, 2, 4, 3, 5 ] | ||
} | ||
``` | ||
* * * | ||
© 2015-17 Lloyd Brookes \<75pound@gmail.com\>. Documented by [jsdoc-to-markdown](https://github.com/jsdoc2md/jsdoc-to-markdown). | ||
© 2015-19 Lloyd Brookes \<75pound@gmail.com\>. Documented by [jsdoc-to-markdown](https://github.com/jsdoc2md/jsdoc-to-markdown). |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
59391
0
17
1739
106
4
2
1
- Removedarray-back@^1.0.4
- Removedobject-get@^2.1.0
- Removedtypical@^2.6.0
- Removedarray-back@1.0.4(transitive)
- Removedobject-get@2.1.1(transitive)
- Removedtypical@2.6.1(transitive)