Skip to content

Commit

Permalink
feat(custom_layer_sorting): sorting for unknown layers fix to ensure …
Browse files Browse the repository at this point in the history
…stable sorting order
  • Loading branch information
missinglink committed Oct 9, 2018
1 parent 8cdb46f commit d67890f
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 2 deletions.
8 changes: 6 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -161,8 +161,12 @@ const fallbacks = [
{ either: isSmallLocality, resolve: resolveSmallLocality },
{ either: isSmallLocaladmin, resolve: resolveSmallLocaladmin },
{ either: isNonPopularNeighbourhood, resolve: resolveNonPopularNeighbourhood },
// the 'else' case, always returns true
{ either: _.constant(true), resolve: (result1, result2) => { return result1 < result2; } }
// the 'else' case, always sort by score, avoiding returning 0 because js sort is unstable
{ either: _.constant(true), resolve: (result1, result2) => {
if( isNaN( result1._score ) || isNaN( result2._score ) ){ return -1; }
if( result1._score === result2._score ){ return -1; }
return result1._score > result2._score ? -1 : 1;
}}
];

module.exports = (clean) => {
Expand Down
17 changes: 17 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -342,4 +342,21 @@ tape('neighbourhood', (test) => {

});

test.test('custom layers should be sorted by score', t => {
const sorter = require('../index')({});

const result_one = new ResultBuilder('stops').score(27.178297).build();
const result_two = new ResultBuilder('stops').score(1.4754213).build();
const result_three = new ResultBuilder('stops').score(1.4754213).build();

t.equals(sorter(result_one, result_two), -1);
t.equals(sorter(result_two, result_one), +1);
t.equals(sorter(result_two, result_three), -1);
t.equals(sorter(result_three, result_two), -1);
t.equals(sorter(result_one, result_three), -1);
t.equals(sorter(result_three, result_one), +1);
t.end();

});

});

0 comments on commit d67890f

Please sign in to comment.