-
Notifications
You must be signed in to change notification settings - Fork 6
/
index.js
99 lines (93 loc) · 3.69 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
import arrayify from 'array-back'
import t from 'typical'
/**
* Isomorphic, load-anywhere function to sort an array by scalar, deep or computed values in any standard or custom order.
*
* @module sort-array
* @typicalname sortArray
* @example
* const sortArray = require('sort-array')
*/
/**
* @param {Array} array - The input array to sort. It is sorted in place.
* @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. 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.
* @param {number} [options.nullRank] - Configures whether `null` values will be sorted before or after defined values. Set to `-1` for before, `1` for after. Defaults to `1`.
* @param {number} [options.undefinedRank] - Configures whether `undefined` values will be sorted before or after defined values. Set to `-1` for before, `1` for after. Defaults to `1`.
* @returns {Array} Returns the array that was passed in.
* @alias module:sort-array
*/
function sortArray (arr, options = {}) {
options = Object.assign(
{
computed: {},
customOrders: {},
nullRank: 1,
undefinedRank: 1
},
options
)
arr.sort(getCompareFunc(options))
return arr
}
function getCompareFunc (options = {}) {
const by = arrayify(options.by)
const order = arrayify(options.order)
const { customOrders, computed } = options
return function compareFunc (xIn, yIn, byIndex = 0) {
const currOrder = order[byIndex] || 'asc'
if (!(currOrder === 'asc' || currOrder === 'desc' || customOrders[currOrder])) {
return 0
}
let result, x, y
if (by.length) {
x = t.isDefined(xIn[by[byIndex]])
? xIn[by[byIndex]]
: computed[by[byIndex]] && computed[by[byIndex]](xIn)
y = t.isDefined(yIn[by[byIndex]])
? yIn[by[byIndex]]
: computed[by[byIndex]] && computed[by[byIndex]](yIn)
} else {
x = xIn
y = yIn
}
if (customOrders && customOrders[currOrder]) {
result = customOrders[currOrder].indexOf(x) - customOrders[currOrder].indexOf(y)
} else if (x === y) {
result = 0
} else if (t.isNull(x) && t.isUndefined(y)) {
result = currOrder === 'asc'
? 1
: currOrder === 'desc'
? -1
: 0
} else if (t.isUndefined(x) && t.isNull(y)) {
result = currOrder === 'asc'
? -1
: currOrder === 'desc'
? 1
: 0
} else if (t.isNull(x) && t.isDefinedValue(y)) {
result = options.nullRank
} else if (t.isUndefined(x) && t.isDefinedValue(y)) {
result = options.undefinedRank
} else if (t.isNull(y) && t.isDefinedValue(x)) {
result = -options.nullRank
} else if (t.isUndefined(y) && t.isDefinedValue(x)) {
result = -options.undefinedRank
} else {
result = x < y ? -1 : x > y ? 1 : 0
if (currOrder === 'desc') {
result = result * -1
}
}
if (result === 0 && t.isDefined(by[byIndex + 1])) {
result = compareFunc(xIn, yIn, byIndex + 1)
}
return result
}
}
export default sortArray