Skip to content

Commit

Permalink
feat: take platform number into account in sorting
Browse files Browse the repository at this point in the history
  • Loading branch information
vesameskanen committed Jan 17, 2022
1 parent b0e81f6 commit ebbdd6a
Showing 1 changed file with 31 additions and 0 deletions.
31 changes: 31 additions & 0 deletions middleware/confidenceScoreDT.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ var check = require('check-types');
var _ = require('lodash');
var fuzzy = require('../helper/fuzzyMatch');
var stringUtils = require('../helper/stringUtils');
const codec = require('pelias-model').codec;
var normalize = stringUtils.normalize;
var removeNumbers = stringUtils.removeNumbers;
var languages = ['default'];
Expand Down Expand Up @@ -102,6 +103,17 @@ function compareProperty(p1, p2) {
return (p1<p2?-1:(p1>p2?1:0));
}

function decodeAddendum(a_dum) {
let addendum = {};
for(let namespace in a_dum){
try {
addendum[namespace] = codec.decode(a_dum[namespace]);
} catch( e ){
logger.warn(`failed to decode addendum namespace ${namespace}`);
}
}
return addendum;
}

/* Quite heavily fi specific sorting */
function compareResults(a, b) {
Expand Down Expand Up @@ -145,6 +157,25 @@ function compareResults(a, b) {
return diff;
}
}
if(a.addendum && b.addendum) {
var plat1 = _.get(decodeAddendum(a.addendum), 'GTFS.platform');
var plat2 = _.get(decodeAddendum(b.addendum), 'GTFS.platform');
if (plat1 && plat2) {
var n1 = parseInt(plat1);
var n2 = parseInt(plat2);
var l1 = n1 + '';
var l2 = n2 + '';
if (!isNaN(n1) && !isNaN(n2) && l1.length==plat1.length && l2.length==plat2.length) {
// use numeric comparison
plat1 = n1;
plat2 = n2;
}
diff = compareProperty(plat1, plat2);
if (diff) {
return diff;
}
}
}
if(a.layer !== b.layer) { // larger has higher priority
return layers.indexOf(b.layer) - layers.indexOf(a.layer);
}
Expand Down

0 comments on commit ebbdd6a

Please sign in to comment.