support aliases in preset search

closes #6139
This commit is contained in:
Martin Raifer
2022-05-24 17:09:31 +02:00
parent b4d0f0cf9d
commit 8796a412f8
4 changed files with 40 additions and 7 deletions
+2
View File
@@ -60,12 +60,14 @@ _Breaking developer changes, which may affect downstream projects or sites that
* Imply `access=no` in access field of `highway=construction` objects ([#9102])
* Don't show non-language tag-suffixes in multilingual name field ([#9124], thanks [@wcedmisten])
* Render horse riding centers like farmyards ([#9118])
* Support searching presets by their `aliases` ([#6139])
#### Other
* Redact more API tokens from custom imagery sources in changeset metadata tags ([#8976], thanks [@k-yle])
#### :hammer: Development
* Switch build system to [esbuild](https://esbuild.github.io/) for much faster builds ([#8774], thanks [@mbrzakovic] and [@bhousel])
* Upgrade some dependencies: maki to `v7.1`, `fontawesome` to `v6.1`, `d3` to `v7.3`, `node-diff` to `v3.1`, `mocha` to `v9.2`, `svg-sprite` to `v1.5.4`, `marked` to `v4.0`
[#6139]: https://github.com/openstreetmap/iD/issues/6139
[#8774]: https://github.com/openstreetmap/iD/pull/8774
[#8811]: https://github.com/openstreetmap/iD/issues/8811
[#8905]: https://github.com/openstreetmap/iD/issues/8905
+3
View File
@@ -58,5 +58,8 @@ export function presetCategory(categoryID, category, allPresets) {
return _searchNameStripped;
};
_this.searchAliases = () => [];
_this.searchAliasesStripped = () => [];
return _this;
}
+2 -2
View File
@@ -98,7 +98,7 @@ export function presetCollection(collection) {
// matches value to preset.name
const leadingNames = searchable
.filter(a => leading(a.searchName()))
.filter(a => leading(a.searchName()) || a.searchAliases().some(leading))
.sort(sortPresets('searchName'));
// matches value to preset suggestion name
@@ -107,7 +107,7 @@ export function presetCollection(collection) {
.sort(sortPresets('searchName'));
const leadingNamesStripped = searchable
.filter(a => leading(a.searchNameStripped()))
.filter(a => leading(a.searchNameStripped()) || a.searchAliasesStripped().some(leading))
.sort(sortPresets('searchNameStripped'));
const leadingSuggestionsStripped = suggestions
+33 -5
View File
@@ -17,6 +17,8 @@ export function presetPreset(presetID, preset, addable, allFields, allPresets) {
let _resolvedMoreFields; // cache
let _searchName; // cache
let _searchNameStripped; // cache
let _searchAliases; // cache
let _searchAliasesStripped; // cache
_this.id = presetID;
@@ -26,6 +28,8 @@ export function presetPreset(presetID, preset, addable, allFields, allPresets) {
_this.originalName = _this.name || '';
_this.originalAliases = _this.aliases || '';
_this.originalScore = _this.matchScore || 1;
_this.originalReference = _this.reference || {};
@@ -123,6 +127,11 @@ export function presetPreset(presetID, preset, addable, allFields, allPresets) {
};
_this.aliases = () => {
return _this.t('aliases', { 'default': _this.originalAliases }).trim().split(/\s*\n\s*/);
};
_this.terms = () => _this.t('terms', { 'default': _this.originalTerms })
.toLowerCase().trim().split(/\s*,+\s*/);
@@ -135,15 +144,26 @@ export function presetPreset(presetID, preset, addable, allFields, allPresets) {
_this.searchNameStripped = () => {
if (!_searchNameStripped) {
_searchNameStripped = _this.searchName();
// split combined diacritical characters into their parts
if (_searchNameStripped.normalize) _searchNameStripped = _searchNameStripped.normalize('NFD');
// remove diacritics
_searchNameStripped = _searchNameStripped.replace(/[\u0300-\u036f]/g, '');
_searchNameStripped = stripDiacritics(_this.searchName());
}
return _searchNameStripped;
};
_this.searchAliases = () => {
if (!_searchAliases) {
_searchAliases = _this.aliases().map(alias => alias.toLowerCase());
}
return _searchAliases;
};
_this.searchAliasesStripped = () => {
if (!_searchAliasesStripped) {
_searchAliasesStripped = _this.searchAliases();
_searchAliasesStripped = _searchAliasesStripped.map(stripDiacritics);
}
return _searchAliasesStripped;
};
_this.isFallback = () => {
const tagCount = Object.keys(_this.tags).length;
return tagCount === 0 || (tagCount === 1 && _this.tags.hasOwnProperty('area'));
@@ -306,5 +326,13 @@ export function presetPreset(presetID, preset, addable, allFields, allPresets) {
}
function stripDiacritics(s) {
// split combined diacritical characters into their parts
if (s.normalize) s = s.normalize('NFD');
// remove diacritics
s = s.replace(/[\u0300-\u036f]/g, '');
return s;
}
return _this;
}