Skip to content

Commit d486ab1

Browse files
committed
sort preset search results by matching aliases, add tests
1 parent 8796a41 commit d486ab1

File tree

3 files changed

+22
-8
lines changed

3 files changed

+22
-8
lines changed

modules/presets/collection.js

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,10 +65,21 @@ export function presetCollection(collection) {
6565
return index === 0;
6666
}
6767

68-
function sortPresets(nameProp) {
68+
function sortPresets(nameProp, aliasesProp) {
6969
return function sortNames(a, b) {
7070
let aCompare = a[nameProp]();
7171
let bCompare = b[nameProp]();
72+
if (aliasesProp) {
73+
// also search in aliases
74+
const findMatchingAlias = strings => {
75+
if (strings.some(s => s === value))
76+
return strings.find(s => s === value);
77+
else
78+
return strings.find(s => s.includes(value));
79+
}
80+
aCompare = findMatchingAlias([aCompare].concat(a[aliasesProp]()));
81+
bCompare = findMatchingAlias([bCompare].concat(b[aliasesProp]()));
82+
}
7283

7384
// priority if search string matches preset name exactly - #4325
7485
if (value === aCompare) return -1;
@@ -99,7 +110,7 @@ export function presetCollection(collection) {
99110
// matches value to preset.name
100111
const leadingNames = searchable
101112
.filter(a => leading(a.searchName()) || a.searchAliases().some(leading))
102-
.sort(sortPresets('searchName'));
113+
.sort(sortPresets('searchName', 'searchAliases'));
103114

104115
// matches value to preset suggestion name
105116
const leadingSuggestions = suggestions
@@ -108,7 +119,7 @@ export function presetCollection(collection) {
108119

109120
const leadingNamesStripped = searchable
110121
.filter(a => leading(a.searchNameStripped()) || a.searchAliasesStripped().some(leading))
111-
.sort(sortPresets('searchNameStripped'));
122+
.sort(sortPresets('searchNameStripped', 'searchAliasesStripped'));
112123

113124
const leadingSuggestionsStripped = suggestions
114125
.filter(a => leadingStrict(a.searchNameStripped()))

modules/presets/preset.js

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ export function presetPreset(presetID, preset, addable, allFields, allPresets) {
2828

2929
_this.originalName = _this.name || '';
3030

31-
_this.originalAliases = _this.aliases || '';
31+
_this.originalAliases = (_this.aliases || []).join('\n');
3232

3333
_this.originalScore = _this.matchScore || 1;
3434

@@ -126,12 +126,10 @@ export function presetPreset(presetID, preset, addable, allFields, allPresets) {
126126
return null;
127127
};
128128

129-
130129
_this.aliases = () => {
131-
return _this.t('aliases', { 'default': _this.originalAliases }).trim().split(/\s*\n\s*/);
130+
return _this.t('aliases', { 'default': _this.originalAliases }).trim().split(/\s*[\r\n]+\s*/);
132131
};
133132

134-
135133
_this.terms = () => _this.t('terms', { 'default': _this.originalTerms })
136134
.toLowerCase().trim().split(/\s*,+\s*/);
137135

test/spec/presets/collection.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ describe('iD.presetCollection', function() {
2828
{ name: 'Ğṝȁß', tags: { landuse: 'ğṝȁß' }, geometry: ['point', 'area'], terms: [] }
2929
),
3030
park: iD.presetPreset('__TEST/leisure/park',
31-
{ name: 'Park', tags: { leisure: 'park' }, geometry: ['point', 'area'], terms: [ 'grass' ], matchScore: 0.5 }
31+
{ name: 'Park', tags: { leisure: 'park' }, geometry: ['point', 'area'], aliases: ['Field'], terms: [ 'grass' ], matchScore: 0.5 }
3232
),
3333
parking: iD.presetPreset('__TEST/amenity/parking',
3434
{ name: 'Parking', tags: { amenity: 'parking' }, geometry: ['point', 'area'], terms: [ 'cars' ] }
@@ -86,6 +86,11 @@ describe('iD.presetCollection', function() {
8686
expect(result.indexOf(p.park), 'Park').to.be.within(3,5); // 6. 'Park' (similar term 'grass')
8787
});
8888

89+
it('matches alias', function() {
90+
var result = c.search('Field', 'area').collection;
91+
expect(result.indexOf(p.park)).to.eql(0); // 1. 'Park' (by alias)
92+
});
93+
8994
it('sorts preset with matchScore penalty below others', function() {
9095
var result = c.search('par', 'point').matchGeometry('point').collection;
9196
expect(result.indexOf(p.parking), 'Parking').to.eql(0); // 1. 'Parking' (default matchScore)

0 commit comments

Comments
 (0)