🚀 Big News: Socket Acquires Coana to Bring Reachability Analysis to Every Appsec Team.Learn more
Socket
Book a DemoInstallSign in
Socket

sort-array

Package Overview
Dependencies
Maintainers
1
Versions
30
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

sort-array - npm Package Compare versions

Comparing version

to
2.0.0

32

lib/sort-array.js
'use strict'
var arrayify = require('array-back')
var t = require('typical')
var objectGet = require('object-get')
const arrayify = require('array-back')
const t = require('typical')
const objectGet = require('object-get')

@@ -12,3 +12,3 @@ /**

* @example
* var sortBy = require('sort-array')
* const sortBy = require('sort-array')
*/

@@ -50,3 +50,3 @@ module.exports = sortBy

* ```js
* > var slotOrder = [ 'morning', 'afternoon', 'evening', 'twilight' ]
* > const slotOrder = [ 'morning', 'afternoon', 'evening', 'twilight' ]
* > sortBy(DJs, 'slot', { slot: slotOrder })

@@ -92,3 +92,3 @@ * [ { name: 'Rodney', slot: 'morning' },

* ```js
* var customOrder = {
* const customOrder = {
* 'inner.number': [ 1, 2, 4, 3, 5 ]

@@ -103,17 +103,19 @@ * }

function sortByFunc (properties, customOrder) {
var props = properties.slice(0)
var property = props.shift()
let props = properties.slice(0)
let property = props.shift()
return function sorter (a, b) {
var result
var x = objectGet(a, property)
var y = objectGet(b, property)
let result
const x = objectGet(a, property)
const y = objectGet(b, property)
if (!t.isDefined(x) && t.isDefined(y)) {
if (customOrder && customOrder[property]) {
result = customOrder[property].indexOf(x) - customOrder[property].indexOf(y)
} else if (x === null && y === null) {
result = 0
} else if ((!t.isDefined(x) || x === null) && t.isDefined(y)) {
result = -1
} else if (t.isDefined(x) && !t.isDefined(y)) {
} else if (t.isDefined(x) && (!t.isDefined(y) || y === null)) {
result = 1
} else if (!t.isDefined(x) && !t.isDefined(y)) {
result = 0
} else if (customOrder && customOrder[property]) {
result = customOrder[property].indexOf(x) - customOrder[property].indexOf(y)
} else {

@@ -120,0 +122,0 @@ result = x < y ? -1 : x > y ? 1 : 0

{
"name": "sort-array",
"author": "Lloyd Brookes <75pound@gmail.com>",
"version": "1.1.1",
"version": "2.0.0",
"description": "Sort an array of objects by any property value, at any depth, in any custom order.",

@@ -20,17 +20,17 @@ "repository": "https://github.com/75lb/sort-array.git",

"engines": {
"node": ">=0.10.0"
"node": ">=4"
},
"scripts": {
"test": "tape test/*.js",
"test": "test-runner test/*.js",
"docs": "jsdoc2md -t jsdoc2md/README.hbs lib/*.js > README.md; echo"
},
"dependencies": {
"array-back": "^1.0.3",
"object-get": "^2.0.4",
"typical": "^2.4.2"
"array-back": "^1.0.4",
"object-get": "^2.1.0",
"typical": "^2.6.0"
},
"devDependencies": {
"tape": "^4.5.1",
"jsdoc-to-markdown": "^1.3.6"
"jsdoc-to-markdown": "^3.0.0",
"test-runner": "^0.3.0"
}
}

@@ -14,3 +14,3 @@ [![view on npm](http://img.shields.io/npm/v/sort-array.svg)](https://www.npmjs.org/package/sort-array)

```js
var sortBy = require('sort-array')
const sortBy = require('sort-array')
```

@@ -25,3 +25,3 @@ <a name="exp_module_sort-array--sortBy"></a>

| recordset | <code>Array.&lt;object&gt;</code> | Input array of objects |
| columnNames | <code>string</code> &#124; <code>Array.&lt;string&gt;</code> | One or more property expressions to sort by, e.g. `'name'` or `'name.first'`. |
| columnNames | <code>string</code> \| <code>Array.&lt;string&gt;</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' ]}` |

@@ -55,3 +55,3 @@

```js
> var slotOrder = [ 'morning', 'afternoon', 'evening', 'twilight' ]
> const slotOrder = [ 'morning', 'afternoon', 'evening', 'twilight' ]
> sortBy(DJs, 'slot', { slot: slotOrder })

@@ -97,3 +97,3 @@ [ { name: 'Rodney', slot: 'morning' },

```js
var customOrder = {
const customOrder = {
'inner.number': [ 1, 2, 4, 3, 5 ]

@@ -105,2 +105,2 @@ }

&copy; 2015-16 Lloyd Brookes \<75pound@gmail.com\>. Documented by [jsdoc-to-markdown](https://github.com/jsdoc2md/jsdoc-to-markdown).
&copy; 2015-17 Lloyd Brookes \<75pound@gmail.com\>. Documented by [jsdoc-to-markdown](https://github.com/jsdoc2md/jsdoc-to-markdown).

@@ -1,6 +0,9 @@

var test = require('tape')
var sortBy = require('../')
const TestRunner = require('test-runner')
const sortBy = require('../')
const a = require('assert')
test('sortBy', function (t) {
var fixture = [
const runner = new TestRunner()
runner.test('sortBy', function () {
const fixture = [
{ a: 4, b: 1, c: 1 },

@@ -16,3 +19,3 @@ { a: 4, b: 3, c: 1 },

]
var expected = [
const expected = [
{ a: 1, b: 1, c: 4 },

@@ -28,30 +31,74 @@ { a: 1, b: 2, c: 4 },

]
t.deepEqual(sortBy(fixture, ['a', 'b', 'c']), expected)
t.end()
a.deepStrictEqual(sortBy(fixture, ['a', 'b', 'c']), expected)
})
test('sortBy, with undefined vals', function (t) {
var fixture = [ { a: 1 }, { }, { a: 0 } ]
var expected = [ { }, { a: 0 }, { a: 1 } ]
t.deepEqual(sortBy(fixture, 'a'), expected)
t.end()
runner.test('sortBy, with undefined vals', function () {
const fixture = [ { a: 1 }, { }, { a: 0 } ]
const expected = [ { }, { a: 0 }, { a: 1 } ]
a.deepStrictEqual(sortBy(fixture, 'a'), expected)
})
test('sortBy, with undefined vals 2', function (t) {
var fixture = [ { a: 'yeah' }, { }, { a: 'what' } ]
var expected = [ { }, { a: 'what' }, { a: 'yeah' } ]
t.deepEqual(sortBy(fixture, 'a'), expected)
t.end()
runner.test('sortBy, with undefined vals 2', function () {
const fixture = [ { a: 'yeah' }, { }, { a: 'what' } ]
const expected = [ { }, { a: 'what' }, { a: 'yeah' } ]
a.deepStrictEqual(sortBy(fixture, 'a'), expected)
})
test('custom order', function (t) {
var fixture = [{ fruit: 'apple' }, { fruit: 'orange' }, { fruit: 'banana' }, { fruit: 'pear' }]
var expected = [{ fruit: 'banana' }, { fruit: 'pear' }, { fruit: 'apple' }, { fruit: 'orange' }]
var fruitOrder = [ 'banana', 'pear', 'apple', 'orange' ]
t.deepEqual(sortBy(fixture, 'fruit', { fruit: fruitOrder }), expected)
t.end()
runner.test('sortBy, with undefined vals 3', function () {
const fixture = [
{ a: 2 },
{ a: undefined },
{ a: 1 },
]
const expected = [
{ a: undefined },
{ a: 1 },
{ a: 2 }
]
a.deepStrictEqual(sortBy(fixture, 'a'), expected)
})
test('sort by two columns, both custom', function (t) {
var expected = [
runner.test('sortBy, with undefined vals 3, customOrder', function () {
const fixture = [
{ a: 2 },
{ a: undefined },
{ a: 1 },
]
const expected = [
{ a: 1 },
{ a: 2 },
{ a: undefined },
]
const customOrder = {
a: [ 1, 2, undefined ]
}
a.deepStrictEqual(sortBy(fixture, 'a', customOrder), expected)
})
runner.test('sortBy, with null vals, customOrder', function () {
const fixture = [
{ a: 2 },
{ a: null },
{ a: 1 },
]
const expected = [
{ a: 1 },
{ a: 2 },
{ a: null },
]
const customOrder = {
a: [ 1, 2, null ]
}
a.deepStrictEqual(sortBy(fixture, 'a', customOrder), expected)
})
runner.test('custom order', function () {
const fixture = [{ fruit: 'apple' }, { fruit: 'orange' }, { fruit: 'banana' }, { fruit: 'pear' }]
const expected = [{ fruit: 'banana' }, { fruit: 'pear' }, { fruit: 'apple' }, { fruit: 'orange' }]
const fruitOrder = [ 'banana', 'pear', 'apple', 'orange' ]
a.deepStrictEqual(sortBy(fixture, 'fruit', { fruit: fruitOrder }), expected)
})
runner.test('sort by two columns, both custom', function () {
const expected = [
{ importance: 'speed', weight: 'low' },

@@ -67,3 +114,3 @@ { importance: 'speed', weight: 'medium' },

]
var fixture = [
const fixture = [
{ importance: 'intelligence', weight: 'medium' },

@@ -79,3 +126,3 @@ { importance: 'strength', weight: 'high' },

]
var customOrder = {
const customOrder = {
importance: [ 'speed', 'strength', 'intelligence' ],

@@ -85,11 +132,10 @@ weight: [ 'low', 'medium', 'high' ]

var result = sortBy(fixture, [ 'importance', 'weight' ], customOrder)
t.deepEqual(result, expected)
t.end()
const result = sortBy(fixture, [ 'importance', 'weight' ], customOrder)
a.deepStrictEqual(result, expected)
})
test('jsdoc-parse', function (t) {
var fixture = require('./fixture/jsdoc-parse')
var expected = require('./expected/jsdoc-parse')
var customOrder = {
runner.test('jsdoc-parse', function () {
const fixture = require('./fixture/jsdoc-parse')
const expected = require('./expected/jsdoc-parse')
const customOrder = {
kind: [ 'class', 'constructor', 'mixin', 'member', 'namespace', 'enum',

@@ -99,9 +145,8 @@ 'constant', 'function', 'event', 'typedef', 'external' ],

}
var result = sortBy(fixture, ['kind', 'scope'], customOrder)
t.deepEqual(result, expected)
t.end()
const result = sortBy(fixture, ['kind', 'scope'], customOrder)
a.deepStrictEqual(result, expected)
})
test('sort by deep value', function (t) {
var fixture = [
runner.test('sort by deep value', function () {
const fixture = [
{ inner: { number: 5 } },

@@ -113,3 +158,3 @@ { inner: { number: 2 } },

]
var expected = [
const expected = [
{ inner: { number: 1 } },

@@ -121,9 +166,8 @@ { inner: { number: 2 } },

]
var result = sortBy(fixture, 'inner.number')
t.deepEqual(result, expected)
t.end()
const result = sortBy(fixture, 'inner.number')
a.deepStrictEqual(result, expected)
})
test('sort by deep value, custom order', function (t) {
var fixture = [
runner.test('sort by deep value, custom order', function () {
const fixture = [
{ inner: { number: 5 } },

@@ -135,3 +179,3 @@ { inner: { number: 2 } },

]
var expected = [
const expected = [
{ inner: { number: 1 } },

@@ -143,8 +187,114 @@ { inner: { number: 2 } },

]
var customOrder = {
const customOrder = {
'inner.number': [ 1, 2, 4, 3, 5 ]
}
var result = sortBy(fixture, 'inner.number', customOrder)
t.deepEqual(result, expected)
t.end()
const result = sortBy(fixture, 'inner.number', customOrder)
a.deepStrictEqual(result, expected)
})
runner.test('sort nulls', function () {
const expected = [
{ importance: 'speed', weight: null },
{ importance: 'strength', weight: null },
{ importance: 'intelligence', weight: null },
{ importance: 'strength', weight: 'high' },
{ importance: 'speed', weight: 'high' },
{ importance: 'intelligence', weight: 'high' },
{ importance: 'intelligence', weight: 'medium' },
{ importance: 'speed', weight: 'medium' },
{ importance: 'strength', weight: 'medium' }
]
const fixture = [
{ importance: 'intelligence', weight: 'medium' },
{ importance: 'strength', weight: 'high' },
{ importance: 'speed', weight: null },
{ importance: 'strength', weight: null },
{ importance: 'speed', weight: 'high' },
{ importance: 'intelligence', weight: null },
{ importance: 'speed', weight: 'medium' },
{ importance: 'intelligence', weight: 'high' },
{ importance: 'strength', weight: 'medium' }
]
const result = sortBy(fixture, 'weight')
a.deepStrictEqual(result, expected)
})
runner.test('sort nulls, 2 column customOrder', function () {
const expected = [
{ importance: undefined, weight: null },
{ importance: 1, weight: 'a' },
{ importance: 1, weight: 'b' },
{ importance: 1, weight: null },
{ importance: 2, weight: 'a' },
{ importance: 2, weight: null },
{ importance: null, weight: 'a' },
{ importance: 3, weight: 'b' },
{ importance: 3, weight: null }
]
const fixture = [
{ importance: 3, weight: 'b' },
{ importance: 1, weight: 'b' },
{ importance: 2, weight: 'a' },
{ importance: undefined, weight: null },
{ importance: 2, weight: null },
{ importance: 1, weight: 'a' },
{ importance: null, weight: 'a' },
{ importance: 1, weight: null },
{ importance: 3, weight: null },
]
const customOrder = {
importance: [ undefined, 1, 2, null, 3 ],
weight: [ 'a', 'b', null ]
}
const result = sortBy(fixture, [ 'importance', 'weight' ], customOrder)
a.deepStrictEqual(result, expected)
})
runner.test('sortBy with nulls', function () {
const fixture = [
{ a: 4, b: null, c: 3 },
{ a: 4, b: 2, c: null },
{ a: 2, b: 2, c: 3 },
{ a: 2, b: 2, c: 2 },
{ a: null, b: 3, c: 4 },
{ a: null, b: null, c: 4 },
{ a: null, b: 2, c: 4 },
{ a: 3, b: 3, c: 3 },
{ a: 4, b: 3, c: null }
]
const expected = [
{ a: null, b: null, c: 4 },
{ a: null, b: 2, c: 4 },
{ a: null, b: 3, c: 4 },
{ a: 2, b: 2, c: 2 },
{ a: 2, b: 2, c: 3 },
{ a: 3, b: 3, c: 3 },
{ a: 4, b: null, c: 3 },
{ a: 4, b: 2, c: null },
{ a: 4, b: 3, c: null }
]
const result = sortBy(fixture, ['a', 'b', 'c'])
a.deepStrictEqual(result, expected)
})
runner.test('sort by deep value, custom order, nulls', function () {
const fixture = [
{ inner: { number: 5 } },
{ inner: { number: 2 } },
{ inner: { number: 3 } },
{ inner: { number: 1 } },
{ inner: { number: null } }
]
const expected = [
{ inner: { number: 1 } },
{ inner: { number: 2 } },
{ inner: { number: null } },
{ inner: { number: 3 } },
{ inner: { number: 5 } }
]
const customOrder = {
'inner.number': [ 1, 2, null, 3, 5 ]
}
const result = sortBy(fixture, 'inner.number', customOrder)
a.deepStrictEqual(result, expected)
})

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