Ignore diacritics in autocomplete

Works for:
- Choice
- Choice List
- Reference
- Reference List

Co-Authored-By: Louis Delbosc <louis.delbosc.prestataire@anct.gouv.fr>
This commit is contained in:
Ronan Amicel
2022-08-09 15:43:44 +02:00
parent 103c795aa2
commit 2a05d04e35
6 changed files with 22 additions and 11 deletions

View File

@@ -7,7 +7,7 @@
* "lush" would only match the "L" in "Lavender".
*/
import {localeCompare, nativeCompare, sortedIndex} from 'app/common/gutil';
import {localeCompare, nativeCompare, removeDiacritics, sortedIndex} from 'app/common/gutil';
import {DomContents} from 'grainjs';
import escapeRegExp = require("lodash/escapeRegExp");
@@ -17,6 +17,12 @@ export interface ACItem {
cleanText: string;
}
// Returns a normalized, trimmed, lowercase version of a string, so that autocomplete is case-
// and accent-insensitive.
export function cleanText(text: string): string {
return removeDiacritics(text).trim().toLowerCase();
}
// Regexp used to split text into words; includes nearly all punctuation. This means that
// "foo-bar" may be searched by "bar", but it's impossible to search for punctuation itself (e.g.
// "a-b" and "a+b" are not distinguished). (It's easy to exclude unicode punctuation too if the
@@ -91,7 +97,7 @@ export class ACIndexImpl<Item extends ACItem> implements ACIndex<Item> {
// The main search function. SearchText will be cleaned (trimmed and lowercased) at the start.
// Empty search text returns the first N items in the search universe.
public search(searchText: string): ACResults<Item> {
const cleanedSearchText = searchText.trim().toLowerCase();
const cleanedSearchText = cleanText(searchText);
const searchWords = cleanedSearchText.split(wordSepRegexp).filter(w => w);
// Maps item index in _allItems to its score.