Skip to content

Commit 10d56c5

Browse files
authored
update pelias/sorting module to fix numeric sorting bug (#1626)
* feat(deps): update sorting module to fix numeric sorting bug * feat(deps): stable sort native since node 12+
1 parent 418bf04 commit 10d56c5

File tree

4 files changed

+7
-16
lines changed

4 files changed

+7
-16
lines changed

middleware/interpolate.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ const async = require('async');
22
const logger = require( 'pelias-logger' ).get( 'api' );
33
const source_mapping = require('../helper/type_mapping').source_mapping;
44
const _ = require('lodash');
5-
const stable = require('stable');
65
const Debug = require('../helper/debug');
76
const debugLog = new Debug('middleware:interpolate');
87

@@ -140,7 +139,7 @@ function setup(service, should_execute, interpolationConfiguration) {
140139

141140
// sort the results to ensure that addresses show up higher than street centroids
142141
if (_.has(res, 'data')) {
143-
res.data = stable(res.data, (a, b) => {
142+
res.data.sort((a, b) => {
144143
if (a.layer === 'address' && b.layer !== 'address') { return -1; }
145144
if (a.layer !== 'address' && b.layer === 'address') { return 1; }
146145
return 0;

middleware/sortResponseData.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
11
const _ = require('lodash');
2-
const stable = require('stable');
3-
42
const logger = require('pelias-logger').get('api');
53

64
function setup(comparator, should_execute) {
@@ -14,7 +12,7 @@ function setup(comparator, should_execute) {
1412
const presort_order = res.data.map(_.property('_id'));
1513

1614
// stable operates on array in place
17-
stable.inplace(res.data, comparator(req.clean));
15+
res.data.sort(comparator(req.clean));
1816

1917
// capture the post-sort order
2018
const postsort_order = res.data.map(_.property('_id'));

package.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,13 +56,12 @@
5656
"pelias-model": "^9.0.0",
5757
"pelias-parser": "2.2.0",
5858
"pelias-query": "^11.0.0",
59-
"pelias-sorting": "^1.2.0",
59+
"pelias-sorting": "^1.7.0",
6060
"predicates": "^2.0.0",
6161
"regenerate": "^1.4.0",
6262
"remove-accents": "^0.4.2",
6363
"require-all": "^3.0.0",
6464
"retry": "^0.12.0",
65-
"stable": "^0.1.8",
6665
"stats-lite": "^2.0.4",
6766
"through2": "^3.0.0"
6867
},

test/unit/middleware/sortResponseData.js

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -103,12 +103,7 @@ module.exports.tests.general_tests = (test, common) => {
103103
module.exports.tests.successful_sort = (test, common) => {
104104
test('comparator should be sort res.data', (t) => {
105105
const logger = mock_logger();
106-
107-
const comparator = () => {
108-
return (a, b) => {
109-
return a._id > b._id;
110-
};
111-
};
106+
const comparator = () => (a, b) => (a._id > b._id) ? +1 : -1;
112107

113108
const sortResponseData = proxyquire('../../../middleware/sortResponseData', {
114109
'pelias-logger': logger
@@ -130,9 +125,9 @@ module.exports.tests.successful_sort = (test, common) => {
130125
};
131126

132127
sort(req, res, () => {
133-
t.deepEquals(res.data.shift(), { _id: 1 });
134-
t.deepEquals(res.data.shift(), { _id: 2 });
135-
t.deepEquals(res.data.shift(), { _id: 3 });
128+
t.deepEquals(res.data[0], { _id: 1 });
129+
t.deepEquals(res.data[1], { _id: 2 });
130+
t.deepEquals(res.data[2], { _id: 3 });
136131

137132
t.ok(logger.isDebugMessage(
138133
'req.clean: {"field":"value"}, pre-sort: [3,2,1], post-sort: [1,2,3]'));

0 commit comments

Comments
 (0)