New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

sort-on

Package Overview
Dependencies
Maintainers
1
Versions
18
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

sort-on - npm Package Compare versions

Comparing version

to
4.0.0

48

index.js

@@ -7,23 +7,23 @@ 'use strict';

module.exports = (arr, prop) => {
if (!Array.isArray(arr)) {
throw new TypeError('Expected an array');
module.exports = (array, property) => {
if (!Array.isArray(array)) {
throw new TypeError(`Expected type \`Array\`, got \`${typeof array}\``);
}
return arr.slice().sort((a, b) => {
let ret = 0;
return array.slice().sort((a, b) => {
let returnValue = 0;
arrify(prop).some(el => {
arrify(property).some(element => {
let isDescending;
let x;
let y;
let desc;
if (typeof el === 'function') {
x = el(a);
y = el(b);
} else if (typeof el === 'string') {
desc = el.charAt(0) === '-';
el = desc ? el.slice(1) : el;
x = dotPropGet(a, el);
y = dotPropGet(b, el);
if (typeof element === 'function') {
x = element(a);
y = element(b);
} else if (typeof element === 'string') {
isDescending = element.charAt(0) === '-';
element = isDescending ? element.slice(1) : element;
x = dotPropGet(a, element);
y = dotPropGet(b, element);
} else {

@@ -35,3 +35,3 @@ x = a;

if (x === y) {
ret = 0;
returnValue = 0;
return false;

@@ -41,3 +41,3 @@ }

if (y !== 0 && !y) {
ret = desc ? 1 : -1;
returnValue = isDescending ? 1 : -1;
return true;

@@ -47,3 +47,3 @@ }

if (x !== 0 && !x) {
ret = desc ? -1 : 1;
returnValue = isDescending ? -1 : 1;
return true;

@@ -53,10 +53,10 @@ }

if (typeof x === 'string' && typeof y === 'string') {
ret = desc ? y.localeCompare(x) : x.localeCompare(y);
return ret !== 0;
returnValue = isDescending ? y.localeCompare(x) : x.localeCompare(y);
return returnValue !== 0;
}
if (desc) {
ret = x < y ? 1 : -1;
if (isDescending) {
returnValue = x < y ? 1 : -1;
} else {
ret = x < y ? -1 : 1;
returnValue = x < y ? -1 : 1;
}

@@ -67,4 +67,4 @@

return ret;
return returnValue;
});
};
{
"name": "sort-on",
"version": "3.0.0",
"version": "4.0.0",
"description": "Sort an array on an object property",

@@ -13,3 +13,3 @@ "license": "MIT",

"engines": {
"node": ">=4"
"node": ">=8"
},

@@ -26,7 +26,4 @@ "scripts": {

"array",
"arr",
"by",
"obj",
"object",
"prop",
"property",

@@ -38,9 +35,9 @@ "dot",

"dependencies": {
"arrify": "^1.0.0",
"dot-prop": "^4.1.1"
"arrify": "^2.0.1",
"dot-prop": "^5.0.0"
},
"devDependencies": {
"ava": "*",
"xo": "*"
"ava": "^1.4.1",
"xo": "^0.24.0"
}
}

@@ -18,23 +18,23 @@ # sort-on [![Build Status](https://travis-ci.org/sindresorhus/sort-on.svg?branch=master)](https://travis-ci.org/sindresorhus/sort-on)

// sort by an object property
// Sort by an object property
sortOn([{x: 'b'}, {x: 'a'}, {x: 'c'}], 'x');
//=> [{x: 'a'}, {x: 'b'}, {x: 'c'}]
// sort descending by an object property
// Sort descending by an object property
sortOn([{x: 'b'}, {x: 'a'}, {x: 'c'}], '-x');
//=> [{x: 'c'}, {x: 'b'}, {x: 'a'}]
// sort by a nested object property
// Sort by a nested object property
sortOn([{x: {y: 'b'}}, {x: {y: 'a'}}], 'x.y');
//=> [{x: {y: 'a'}}, {x: {y: 'b'}}]
// sort descending by a nested object property
// Sort descending by a nested object property
sortOn([{x: {y: 'b'}}, {x: {y: 'a'}}], '-x.y');
//=> [{x: {y: 'b'}, {x: {y: 'a'}}}]
// sort by the `x` propery, then `y`
// Sort by the `x` propery, then `y`
sortOn([{x: 'c', y: 'c'}, {x: 'b', y: 'a'}, {x: 'b', y: 'b'}], ['x', 'y']);
//=> [{x: 'b', y: 'a'}, {x: 'b', y: 'b'}, {x: 'c', y: 'c'}]
// sort by the returned value
// Sort by the returned value
sortOn([{x: 'b'}, {x: 'a'}, {x: 'c'}], el => el.x);

@@ -47,13 +47,15 @@ //=> [{x: 'a'}, {x: 'b'}, {x: 'c'}]

### sortOn(input, property)
### sortOn(array, property)
Returns a new sorted array.
Returns a new sorted version of the given array.
#### input
#### array
Type: `Array`
Type: `unknown[]`
The array to sort.
#### property
Type: `string` `string[]` `Function`
Type: `string | string[] | Function`

@@ -60,0 +62,0 @@ 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.