Skip to content

Commit ee365c8

Browse files
committed
feat(assignLabels): improve duplicate label handling
1 parent 293e116 commit ee365c8

File tree

3 files changed

+68
-1
lines changed

3 files changed

+68
-1
lines changed

middleware/assignLabels.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,22 @@ function assignLabel(req, res, next, labelGenerator) {
1515
return next();
1616
}
1717

18+
const dedupLabel = {};
19+
1820
res.data.forEach(function (result) {
1921
result.label = labelGenerator(result);
22+
dedupLabel[result.label] = dedupLabel[result.label] || [];
23+
dedupLabel[result.label].push(result);
2024
});
2125

26+
Object.values(dedupLabel)
27+
.filter(results => results.length > 1)
28+
.forEach(results => {
29+
results.forEach(result => {
30+
result.label = labelGenerator(result, {withOptional: true});
31+
});
32+
});
33+
2234
next();
2335
}
2436

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@
5050
"morgan": "^1.8.2",
5151
"pelias-compare": "^0.1.16",
5252
"pelias-config": "^4.0.0",
53-
"pelias-labels": "^1.13.0",
53+
"pelias-labels": "pelias/labels#joxit/feat/with-optional",
5454
"pelias-logger": "^1.2.0",
5555
"pelias-microservice-wrapper": "^1.7.0",
5656
"pelias-model": "^7.0.0",

test/unit/middleware/assignLabels.js

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,61 @@ module.exports.tests.serialization = function(test, common) {
131131

132132
});
133133

134+
test('support with optionnal', function(t) {
135+
let withoutOptionCalls = 0;
136+
let withOptionCalls = 0;
137+
var assignLabels = proxyquire('../../../middleware/assignLabels', {
138+
'pelias-labels': function(result, options) {
139+
if(!options) {
140+
withoutOptionCalls++;
141+
return 'lab.el';
142+
} else if (options.withOptional && result.id === 1) {
143+
withOptionCalls++;
144+
return 'lab.el, region 1';
145+
} else if (options.withOptional && result.id === 2) {
146+
withOptionCalls++;
147+
return 'lab.el, region 2';
148+
}
149+
}
150+
})();
151+
var res = {
152+
data: [{
153+
id: 1,
154+
name: {
155+
default: ['lab.el']
156+
}
157+
},{
158+
id: 2,
159+
name: {
160+
default: ['lab.el']
161+
}
162+
}]
163+
};
164+
165+
var expected = {
166+
data: [{
167+
id: 1,
168+
name: {
169+
default: ['lab.el']
170+
},
171+
label: 'lab.el, region 1'
172+
},{
173+
id: 2,
174+
name: {
175+
default: ['lab.el']
176+
},
177+
label: 'lab.el, region 2'
178+
}]
179+
};
180+
181+
assignLabels({}, res, function () {
182+
t.deepEqual(res, expected);
183+
t.deepEqual(withOptionCalls, 2);
184+
t.deepEqual(withoutOptionCalls, 2);
185+
t.end();
186+
});
187+
});
188+
134189
};
135190

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

0 commit comments

Comments
 (0)