From 8796a412f8098cde968817e47db928d3cc9d4595 Mon Sep 17 00:00:00 2001 From: Martin Raifer Date: Tue, 24 May 2022 17:09:31 +0200 Subject: [PATCH] support aliases in preset search closes #6139 --- CHANGELOG.md | 2 ++ modules/presets/category.js | 3 +++ modules/presets/collection.js | 4 ++-- modules/presets/preset.js | 38 ++++++++++++++++++++++++++++++----- 4 files changed, 40 insertions(+), 7 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index a081a967c..0643249c0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/modules/presets/category.js b/modules/presets/category.js index 5a9ee0945..aadecbbd4 100644 --- a/modules/presets/category.js +++ b/modules/presets/category.js @@ -58,5 +58,8 @@ export function presetCategory(categoryID, category, allPresets) { return _searchNameStripped; }; + _this.searchAliases = () => []; + _this.searchAliasesStripped = () => []; + return _this; } diff --git a/modules/presets/collection.js b/modules/presets/collection.js index 07552de5e..df87d8430 100644 --- a/modules/presets/collection.js +++ b/modules/presets/collection.js @@ -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 diff --git a/modules/presets/preset.js b/modules/presets/preset.js index 9e96e1ae4..15de0317d 100644 --- a/modules/presets/preset.js +++ b/modules/presets/preset.js @@ -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; }