Skip to content

Commit aedc7c0

Browse files
author
bjvickers
committed
feat: add support for descending sort
all tests now working fixes issue #6
1 parent a0efda3 commit aedc7c0

File tree

5 files changed

+25
-110
lines changed

5 files changed

+25
-110
lines changed

index.js

Lines changed: 15 additions & 95 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
const arrayify = require('array-back')
21
const t = require('typical')
32

43
/**
@@ -91,27 +90,31 @@ module.exports = sortBy
9190
* ```
9291
*/
9392
function sortBy (recordset, sortBy, computedProperties) {
94-
if (computedProperties) {
95-
return recordset.sort(sortWithComputedProperties(sortBy))
96-
} else {
97-
return recordset.sort(sortWithoutComputedProperties(sortBy))
98-
}
93+
return recordset.sort(compare(sortBy, computedProperties))
9994
}
10095

101-
// @TODO: Question - do we support crap data in? E.g. testing for 'isArrayLike',
96+
// @TODO: Question - do we support crap data in? E.g. testing for 't.isArrayLike',
10297
// @TODO: rather than just assuming the data in is correct?
103-
function sortWithoutComputedProperties (sortBy) {
104-
let sorts = Object.entries(sortBy) // equiv: let sorts = sortBy.slice(0)
98+
function compare (sortBy, computedProperties) {
99+
let sorts = Object.entries(sortBy)
105100
let propSort = sorts.shift()
106101
let property = t.isArrayLike(propSort) && propSort[0] || undefined
107102
let sort = t.isArrayLike(propSort) && propSort[1] || 'asc'
108103

109104
return function sorter (a, b) {
105+
let x
106+
let y
110107
let result
111-
const x = a[property]
112-
const y = b[property]
113-
let currentSort = sort
114108
let recurse
109+
let currentSort = sort
110+
111+
if (t.isDefined(computedProperties) && computedProperties.hasOwnProperty(property)) {
112+
x = computedProperties[property](a)
113+
y = computedProperties[property](b)
114+
} else {
115+
x = a[property]
116+
y = b[property]
117+
}
115118

116119
if (t.isArrayLike(sort)) {
117120
// Custom sort the current property.
@@ -157,86 +160,3 @@ function sortWithoutComputedProperties (sortBy) {
157160
}
158161
}
159162
}
160-
161-
/*
162-
function sortByCustom (properties, customOrder) {
163-
let props = properties.slice(0)
164-
let property = props.shift()
165-
return function sorter (a, b) {
166-
const x = a[property]
167-
const y = b[property]
168-
169-
let result = customOrder[property].indexOf(x) - customOrder[property].indexOf(y)
170-
171-
if (result === 0) {
172-
if (props.length) {
173-
property = props.shift()
174-
return sorter(a, b)
175-
} else {
176-
props = properties.slice(0)
177-
property = props.shift()
178-
return 0
179-
}
180-
} else {
181-
props = properties.slice(0)
182-
property = props.shift()
183-
return result
184-
}
185-
}
186-
}
187-
188-
function sortByConvention (sortBy, customOrder) {
189-
let sorts = sortBy.slice(0)
190-
let propSort = sorts.shift()
191-
let property = t.isArrayLike(propSort) && propSort[0] || undefined
192-
let sort = t.isArrayLike(propSort) && propSort[1] || customOrder ? 'custom' : 'asc'
193-
194-
return function sorter (a, b) {
195-
let result
196-
const x = a[property]
197-
const y = b[property]
198-
let currentSort = sort
199-
let recurse
200-
201-
if (customOrder) {
202-
result = customOrder[property].indexOf(x) - customOrder[property].indexOf(y)
203-
} else {
204-
// Perform the initial asc sort
205-
if (x === null && y === null) {
206-
result = 0
207-
} else if ((!t.isDefined(x) || x === null) && t.isDefined(y)) {
208-
result = -1
209-
} else if (t.isDefined(x) && (!t.isDefined(y) || y === null)) {
210-
result = 1
211-
} else if (!t.isDefined(x) && !t.isDefined(y)) {
212-
result = 0
213-
} else {
214-
result = x < y ? -1 : x > y ? 1 : 0
215-
}
216-
}
217-
218-
// Reset this sorting function and parent, unless we have an equal
219-
// result and there are more sorts still to perform, in which case
220-
// move on to the next one.
221-
if (result === 0 && sorts.length) {
222-
recurse = true
223-
} else {
224-
recurse = false
225-
sorts = sortBy.slice(0)
226-
}
227-
228-
propSort = sorts.shift()
229-
property = t.isArrayLike(propSort) && propSort[0] || undefined
230-
sort = t.isArrayLike(propSort) && propSort[1] || 'asc'
231-
232-
// Present the result
233-
if (recurse) {
234-
return sorter(a, b)
235-
} else if ((result === 0) || (currentSort === 'asc')) {
236-
return result
237-
} else {
238-
return result * -1
239-
}
240-
}
241-
}
242-
*/

package-lock.json

Lines changed: 0 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,10 @@
2020
},
2121
"scripts": {
2222
"test": "test-runner test/*.js",
23+
"test:computed": "node test/computed-property.js",
2324
"docs": "jsdoc2md -t README.hbs index.js > README.md; echo"
2425
},
2526
"dependencies": {
26-
"array-back": "^3.0.0",
2727
"typical": "^3.0.0"
2828
},
2929
"devDependencies": {

test/computed-property.js

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ runner.skip('computed property: anonymous', function () {
2727
a.deepStrictEqual(result, expected)
2828
})
2929

30-
runner.skip('computed property: named', function () {
30+
runner.test('computed property: named', function () {
3131
const fixture = [
3232
{ inner: { a: 5, b: 10 } },
3333
{ inner: { a: 2, b: 10 } },
@@ -75,7 +75,7 @@ runner.skip('computed property: anonymous, descending order', function () {
7575
a.deepStrictEqual(result, expected)
7676
})
7777

78-
runner.skip('computed property: named, descending custom order', function () {
78+
runner.test('computed property: named, descending custom order', function () {
7979
const fixture = [
8080
{ inner: { number: 5 } },
8181
{ inner: { number: 2 } },
@@ -84,11 +84,11 @@ runner.skip('computed property: named, descending custom order', function () {
8484
{ inner: { number: 4 } }
8585
]
8686
const expected = [
87-
{ inner: { number: 1 } },
88-
{ inner: { number: 2 } },
89-
{ inner: { number: 3 } },
87+
{ inner: { number: 5 } },
9088
{ inner: { number: 4 } },
91-
{ inner: { number: 5 } }
89+
{ inner: { number: 3 } },
90+
{ inner: { number: 2 } },
91+
{ inner: { number: 1 } }
9292
]
9393
const sortBy = {
9494
total: 'desc'
@@ -100,7 +100,7 @@ runner.skip('computed property: named, descending custom order', function () {
100100
a.deepStrictEqual(result, expected)
101101
})
102102

103-
runner.skip('computed property: custom order', function () {
103+
runner.test('computed property: custom order', function () {
104104
const fixture = [
105105
{ inner: { number: 5 } },
106106
{ inner: { number: 2 } },
@@ -127,7 +127,7 @@ runner.skip('computed property: custom order', function () {
127127
a.deepStrictEqual(result, expected)
128128
})
129129

130-
runner.skip('computed property: custom order, nulls', function () {
130+
runner.test('computed property: custom order, nulls', function () {
131131
const fixture = [
132132
{ inner: { number: 5 } },
133133
{ inner: { number: 2 } },

test/sort-order.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -387,7 +387,7 @@ runner.test('sort order: desc|asc|desc', function () {
387387
a.deepStrictEqual(result, expected)
388388
})
389389

390-
runner.skip('sort order: computed property', function () {
390+
runner.test('sort order: computed property', function () {
391391
const fixture = [
392392
{ inner: { number: 2 } },
393393
{ inner: { number: 3 } },

0 commit comments

Comments
 (0)