sort-array
Advanced tools
Comparing version 3.0.3 to 3.0.4
@@ -5,3 +5,3 @@ (function (global, factory) { | ||
(global = global || self, global.sortArray = factory()); | ||
}(this, function () { 'use strict'; | ||
}(this, (function () { 'use strict'; | ||
@@ -350,7 +350,7 @@ /** | ||
* @param {Array} array - The input array to sort. It is sorted in place. | ||
* @param {object} [options] - Config object. | ||
* @param {object} [options] - Sort options. | ||
* @param {string[]} [options.by] - One or more property names or computed fields to sort by. Specifying property names is only relevant when sorting an array of objects. | ||
* @param {string[]} [options.order] - One or more sort orders. Specify `asc`, `desc` or a property name from the `options.customOrders` object. | ||
* @param {object} [options.customOrders] - A dictionary object containing one or more custom orders. Each custom order value must be an array defining the order expected values must be sorted in. | ||
* @param {object} [options.computed] - A dictionary object containing one or more computed field functions. | ||
* @param {object} [options.computed] - A dictionary object containing one or more computed field functions. The function will be invoked once per item in the array. Each invocation will receive the array item as input and must return a primitive value by which the array can be sorted. | ||
* @returns {Array} Returns the array that was passed in. | ||
@@ -430,2 +430,2 @@ * @alias module:sort-array | ||
})); | ||
}))); |
@@ -7,3 +7,3 @@ { | ||
], | ||
"version": "3.0.3", | ||
"version": "3.0.4", | ||
"description": "Isomorphic, load-anywhere function to sort an array by scalar, deep or computed values in any standard or custom order", | ||
@@ -38,3 +38,3 @@ "repository": "https://github.com/75lb/sort-array", | ||
"jsdoc-to-markdown": "^5.0.2", | ||
"rollup": "^1.25.1", | ||
"rollup": "^1.25.2", | ||
"test-runner": "^0.6.0" | ||
@@ -41,0 +41,0 @@ }, |
@@ -38,24 +38,22 @@ [![view on npm](http://img.shields.io/npm/v/sort-array.svg)](https://www.npmjs.org/package/sort-array) | ||
Sort by a computed field, e.g. an algorithm to rank boxers by influence. Define your computed fields in the `computed` object, each value being a function which takes an array member as input and returns the value to be sorted by. In this example we sort by `rank` (the name of our computed field supplied in `computed`). | ||
Sort by a computed field, i.e. a computed value that doesn't exist in the input dataset. Define your computed fields in the `options.computed` object, each value being a function which takes an array member as input and returns the primitive value to be sorted by. In this example we sort by `total` (the name of the computed field supplied in `options.computed`). | ||
```js | ||
> const boxers = [ | ||
{ name: 'Amir', ticketsSold: 30000, titlesHeld: 2 }, | ||
{ name: 'Vasiliy', ticketsSold: 20000, titlesHeld: 4 }, | ||
{ name: 'Josh', ticketsSold: 10000, titlesHeld: 3 }, | ||
{ name: 'Anthony', ticketsSold: 90000, titlesHeld: 0 } | ||
> const repositories = [ | ||
{ name: '75lb/sort-array', openIssues: 0, closedIssues: 4 }, | ||
{ name: 'lwsjs/local-web-server', openIssues: 4, closedIssues: 80 }, | ||
{ name: 'jsdoc2md/jsdoc-api', openIssues: 3, closedIssues: 47 } | ||
] | ||
> sortArray(boxers, { | ||
by: 'rank', | ||
> sortArray(repositories, { | ||
by: 'total', | ||
order: 'desc', | ||
computed: { | ||
rank: boxer => boxer.ticketsSold + (boxer.titlesHeld * 10000) | ||
total: repository => repository.openIssues + repository.closedIssues | ||
} | ||
}) | ||
[ | ||
{ name: 'Anthony', ticketsSold: 90000, titlesHeld: 0 }, | ||
{ name: 'Vasiliy', ticketsSold: 20000, titlesHeld: 4 }, | ||
{ name: 'Amir', ticketsSold: 30000, titlesHeld: 2 }, | ||
{ name: 'Josh', ticketsSold: 10000, titlesHeld: 3 } | ||
{ name: 'lwsjs/local-web-server', openIssues: 4, closedIssues: 80 }, | ||
{ name: 'jsdoc2md/jsdoc-api', openIssues: 3, closedIssues: 47 }, | ||
{ name: '75lb/sort-array', openIssues: 0, closedIssues: 4 } | ||
] | ||
@@ -144,3 +142,3 @@ ``` | ||
| [options.customOrders] | <code>object</code> | A dictionary object containing one or more custom orders. Each custom order value must be an array defining the order expected values must be sorted in. | | ||
| [options.computed] | <code>object</code> | A dictionary object containing one or more computed field functions. | | ||
| [options.computed] | <code>object</code> | A dictionary object containing one or more computed field functions. The function will be invoked once per item in the array. Each invocation will receive the array item as input and must return a primitive value by which the array can be sorted. | | ||
@@ -147,0 +145,0 @@ |
Sorry, the diff of this file is not supported yet
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
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
37861
175