Skip to content

Commit

Permalink
feat(assignLabels): improve duplicate label handling
Browse files Browse the repository at this point in the history
  • Loading branch information
Joxit committed Feb 5, 2021
1 parent 293e116 commit ee365c8
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 1 deletion.
12 changes: 12 additions & 0 deletions middleware/assignLabels.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,22 @@ function assignLabel(req, res, next, labelGenerator) {
return next();
}

const dedupLabel = {};

res.data.forEach(function (result) {
result.label = labelGenerator(result);
dedupLabel[result.label] = dedupLabel[result.label] || [];
dedupLabel[result.label].push(result);
});

Object.values(dedupLabel)
.filter(results => results.length > 1)
.forEach(results => {
results.forEach(result => {
result.label = labelGenerator(result, {withOptional: true});
});
});

next();
}

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@
"morgan": "^1.8.2",
"pelias-compare": "^0.1.16",
"pelias-config": "^4.0.0",
"pelias-labels": "^1.13.0",
"pelias-labels": "pelias/labels#joxit/feat/with-optional",
"pelias-logger": "^1.2.0",
"pelias-microservice-wrapper": "^1.7.0",
"pelias-model": "^7.0.0",
Expand Down
55 changes: 55 additions & 0 deletions test/unit/middleware/assignLabels.js
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,61 @@ module.exports.tests.serialization = function(test, common) {

});

test('support with optionnal', function(t) {
let withoutOptionCalls = 0;
let withOptionCalls = 0;
var assignLabels = proxyquire('../../../middleware/assignLabels', {
'pelias-labels': function(result, options) {
if(!options) {
withoutOptionCalls++;
return 'lab.el';
} else if (options.withOptional && result.id === 1) {
withOptionCalls++;
return 'lab.el, region 1';
} else if (options.withOptional && result.id === 2) {
withOptionCalls++;
return 'lab.el, region 2';
}
}
})();
var res = {
data: [{
id: 1,
name: {
default: ['lab.el']
}
},{
id: 2,
name: {
default: ['lab.el']
}
}]
};

var expected = {
data: [{
id: 1,
name: {
default: ['lab.el']
},
label: 'lab.el, region 1'
},{
id: 2,
name: {
default: ['lab.el']
},
label: 'lab.el, region 2'
}]
};

assignLabels({}, res, function () {
t.deepEqual(res, expected);
t.deepEqual(withOptionCalls, 2);
t.deepEqual(withoutOptionCalls, 2);
t.end();
});
});

};

module.exports.all = function (tape, common) {
Expand Down

0 comments on commit ee365c8

Please sign in to comment.