|
1 |
| -const arrayify = require('array-back') |
2 | 1 | const t = require('typical')
|
3 | 2 |
|
4 | 3 | /**
|
@@ -91,27 +90,31 @@ module.exports = sortBy
|
91 | 90 | * ```
|
92 | 91 | */
|
93 | 92 | 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)) |
99 | 94 | }
|
100 | 95 |
|
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', |
102 | 97 | // @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) |
105 | 100 | let propSort = sorts.shift()
|
106 | 101 | let property = t.isArrayLike(propSort) && propSort[0] || undefined
|
107 | 102 | let sort = t.isArrayLike(propSort) && propSort[1] || 'asc'
|
108 | 103 |
|
109 | 104 | return function sorter (a, b) {
|
| 105 | + let x |
| 106 | + let y |
110 | 107 | let result
|
111 |
| - const x = a[property] |
112 |
| - const y = b[property] |
113 |
| - let currentSort = sort |
114 | 108 | 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 | + } |
115 | 118 |
|
116 | 119 | if (t.isArrayLike(sort)) {
|
117 | 120 | // Custom sort the current property.
|
@@ -157,86 +160,3 @@ function sortWithoutComputedProperties (sortBy) {
|
157 | 160 | }
|
158 | 161 | }
|
159 | 162 | }
|
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 |
| -*/ |
0 commit comments