Use lodash to remove accents and ther diacritics

https://lodash.com/docs/4.17.15#deburr
This commit is contained in:
Ronan Amicel 2022-08-17 18:35:50 +02:00
parent 0249b3d31b
commit 4003d2d7c6
2 changed files with 6 additions and 9 deletions

View File

@ -7,9 +7,10 @@
* "lush" would only match the "L" in "Lavender". * "lush" would only match the "L" in "Lavender".
*/ */
import {localeCompare, nativeCompare, removeDiacritics, sortedIndex} from 'app/common/gutil'; import {localeCompare, nativeCompare, sortedIndex} from 'app/common/gutil';
import {DomContents} from 'grainjs'; import {DomContents} from 'grainjs';
import escapeRegExp = require("lodash/escapeRegExp"); import escapeRegExp = require("lodash/escapeRegExp");
import deburr = require("lodash/deburr");
export interface ACItem { export interface ACItem {
// This should be a trimmed lowercase version of the item's text. It may be an accessor. // This should be a trimmed lowercase version of the item's text. It may be an accessor.
@ -17,10 +18,11 @@ export interface ACItem {
cleanText: string; cleanText: string;
} }
// Returns a normalized, trimmed, lowercase version of a string, so that autocomplete is case- // Returns a trimmed, lowercase version of a string,
// and accent-insensitive. // from which accents and other diacritics have been removed,
// so that autocomplete is case- and accent-insensitive.
export function cleanText(text: string): string { export function cleanText(text: string): string {
return removeDiacritics(text).trim().toLowerCase(); return deburr(text).trim().toLowerCase();
} }
// Regexp used to split text into words; includes nearly all punctuation. This means that // Regexp used to split text into words; includes nearly all punctuation. This means that

View File

@ -55,11 +55,6 @@ export function capitalizeFirstWord(str: string): string {
return str.replace(/\b[a-z]/i, c => c.toUpperCase()); return str.replace(/\b[a-z]/i, c => c.toUpperCase());
} }
// Remove diacritics (accents and other signs).
export function removeDiacritics(text: string): string {
return text.normalize("NFKD").replace(/[\u0300-\u036f]/g, "")
}
// Returns whether the string n represents a valid number. // Returns whether the string n represents a valid number.
// http://stackoverflow.com/questions/18082/validate-numbers-in-javascript-isnumeric // http://stackoverflow.com/questions/18082/validate-numbers-in-javascript-isnumeric
export function isNumber(n: string): boolean { export function isNumber(n: string): boolean {