@@ -1,4 +0,2 @@ | ||
declare namespace sortOn { | ||
type Property<T> = string | ((element: T) => unknown) | (string | ((element: T) => unknown))[]; | ||
} | ||
export type Property<T> = string | ((element: T) => unknown) | Array<string | ((element: T) => unknown)>; | ||
@@ -9,3 +7,3 @@ /** | ||
@param array - The array to sort. | ||
@param property - The string can be a [dot path](https://github.com/sindresorhus/dot-prop) to a nested object property. Prepend it with `-` to sort it in descending order. | ||
@param property - The string can be a [dot path](https://github.com/sindresorhus/dot-prop) to a nested object property. Prefix it with `-` to sort it in descending order. | ||
@returns A new sorted version of the given array. | ||
@@ -15,3 +13,3 @@ | ||
``` | ||
import sortOn = require('sort-on'); | ||
import sortOn from 'sort-on'; | ||
@@ -43,4 +41,2 @@ // Sort by an object property | ||
*/ | ||
declare function sortOn<T>(array: readonly T[], property: sortOn.Property<T>): T[]; | ||
export = sortOn; | ||
export default function sortOn<T>(array: readonly T[], property: Property<T>): T[]; |
14
index.js
@@ -1,8 +0,6 @@ | ||
'use strict'; | ||
const dotProp = require('dot-prop'); | ||
const arrify = require('arrify'); | ||
import dotProp from 'dot-prop'; | ||
const dotPropGet = dotProp.get; | ||
const {get: dotPropGet} = dotProp; | ||
module.exports = (array, property) => { | ||
export default function sortOn(array, property) { | ||
if (!Array.isArray(array)) { | ||
@@ -12,6 +10,6 @@ throw new TypeError(`Expected type \`Array\`, got \`${typeof array}\``); | ||
return array.slice().sort((a, b) => { | ||
return [...array].sort((a, b) => { | ||
let returnValue = 0; | ||
arrify(property).some(element => { | ||
[property].flat().some(element => { | ||
let isDescending; | ||
@@ -65,2 +63,2 @@ let x; | ||
}); | ||
}; | ||
} |
{ | ||
"name": "sort-on", | ||
"version": "4.1.1", | ||
"version": "5.0.0", | ||
"description": "Sort an array on an object property", | ||
"license": "MIT", | ||
"repository": "sindresorhus/sort-on", | ||
"funding": "https://github.com/sponsors/sindresorhus", | ||
"author": { | ||
"name": "Sindre Sorhus", | ||
"email": "sindresorhus@gmail.com", | ||
"url": "sindresorhus.com" | ||
"url": "https://sindresorhus.com" | ||
}, | ||
"type": "module", | ||
"exports": "./index.js", | ||
"engines": { | ||
"node": ">=8" | ||
"node": "^12.20.0 || ^14.13.1 || >=16.0.0" | ||
}, | ||
@@ -34,10 +37,9 @@ "scripts": { | ||
"dependencies": { | ||
"arrify": "^2.0.1", | ||
"dot-prop": "^5.0.0" | ||
"dot-prop": "^6.0.1" | ||
}, | ||
"devDependencies": { | ||
"ava": "^1.4.1", | ||
"tsd": "^0.7.4", | ||
"xo": "^0.24.0" | ||
"ava": "^3.15.0", | ||
"tsd": "^0.18.0", | ||
"xo": "^0.46.4" | ||
} | ||
} |
@@ -5,14 +5,12 @@ # sort-on | ||
## Install | ||
```sh | ||
npm install sort-on | ||
``` | ||
$ npm install sort-on | ||
``` | ||
## Usage | ||
```js | ||
const sortOn = require('sort-on'); | ||
import sortOn from 'sort-on'; | ||
@@ -40,7 +38,6 @@ // Sort by an object property | ||
// Sort by the returned value | ||
sortOn([{x: 'b'}, {x: 'a'}, {x: 'c'}], el => el.x); | ||
sortOn([{x: 'b'}, {x: 'a'}, {x: 'c'}], element => element.x); | ||
//=> [{x: 'a'}, {x: 'b'}, {x: 'c'}] | ||
``` | ||
## API | ||
@@ -62,7 +59,4 @@ | ||
The string can be a [dot path](https://github.com/sindresorhus/dot-prop) to a nested object property. Prepend it with `-` to sort it in descending order. | ||
The string can be a [dot path](https://github.com/sindresorhus/dot-prop) to a nested object property. | ||
## License | ||
MIT © [Sindre Sorhus](https://sindresorhus.com) | ||
Prefix it with `-` to sort it in descending order. |
Sorry, the diff of this file is not supported yet
1
-50%Yes
NaN5760
-0.4%80
-5.88%60
-9.09%