Skip to content

Commit

Permalink
update pelias/sorting module to fix numeric sorting bug (pelias#1626)
Browse files Browse the repository at this point in the history
* feat(deps): update sorting module to fix numeric sorting bug

* feat(deps): stable sort native since node 12+
  • Loading branch information
missinglink authored Jun 20, 2022
1 parent 418bf04 commit 10d56c5
Show file tree
Hide file tree
Showing 4 changed files with 7 additions and 16 deletions.
3 changes: 1 addition & 2 deletions middleware/interpolate.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ const async = require('async');
const logger = require( 'pelias-logger' ).get( 'api' );
const source_mapping = require('../helper/type_mapping').source_mapping;
const _ = require('lodash');
const stable = require('stable');
const Debug = require('../helper/debug');
const debugLog = new Debug('middleware:interpolate');

Expand Down Expand Up @@ -140,7 +139,7 @@ function setup(service, should_execute, interpolationConfiguration) {

// sort the results to ensure that addresses show up higher than street centroids
if (_.has(res, 'data')) {
res.data = stable(res.data, (a, b) => {
res.data.sort((a, b) => {
if (a.layer === 'address' && b.layer !== 'address') { return -1; }
if (a.layer !== 'address' && b.layer === 'address') { return 1; }
return 0;
Expand Down
4 changes: 1 addition & 3 deletions middleware/sortResponseData.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
const _ = require('lodash');
const stable = require('stable');

const logger = require('pelias-logger').get('api');

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

// stable operates on array in place
stable.inplace(res.data, comparator(req.clean));
res.data.sort(comparator(req.clean));

// capture the post-sort order
const postsort_order = res.data.map(_.property('_id'));
Expand Down
3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -56,13 +56,12 @@
"pelias-model": "^9.0.0",
"pelias-parser": "2.2.0",
"pelias-query": "^11.0.0",
"pelias-sorting": "^1.2.0",
"pelias-sorting": "^1.7.0",
"predicates": "^2.0.0",
"regenerate": "^1.4.0",
"remove-accents": "^0.4.2",
"require-all": "^3.0.0",
"retry": "^0.12.0",
"stable": "^0.1.8",
"stats-lite": "^2.0.4",
"through2": "^3.0.0"
},
Expand Down
13 changes: 4 additions & 9 deletions test/unit/middleware/sortResponseData.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,12 +103,7 @@ module.exports.tests.general_tests = (test, common) => {
module.exports.tests.successful_sort = (test, common) => {
test('comparator should be sort res.data', (t) => {
const logger = mock_logger();

const comparator = () => {
return (a, b) => {
return a._id > b._id;
};
};
const comparator = () => (a, b) => (a._id > b._id) ? +1 : -1;

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

sort(req, res, () => {
t.deepEquals(res.data.shift(), { _id: 1 });
t.deepEquals(res.data.shift(), { _id: 2 });
t.deepEquals(res.data.shift(), { _id: 3 });
t.deepEquals(res.data[0], { _id: 1 });
t.deepEquals(res.data[1], { _id: 2 });
t.deepEquals(res.data[2], { _id: 3 });

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

0 comments on commit 10d56c5

Please sign in to comment.