From 5ce977959b12a1e220cefe27031f847213dab47f Mon Sep 17 00:00:00 2001 From: Evgeny Zinoviev Date: Wed, 5 Jan 2022 18:48:19 +0300 Subject: fix for accented characters (#8) --- autocomplete.js | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/autocomplete.js b/autocomplete.js index aa2ad12..82a5d6c 100644 --- a/autocomplete.js +++ b/autocomplete.js @@ -67,7 +67,9 @@ class Autocomplete { createItem(lookup, item) { let label; if (this.options.highlightTyped) { - const idx = item.label.toLowerCase().indexOf(lookup.toLowerCase()); + const idx = removeDiacritics(item.label) + .toLowerCase() + .indexOf(removeDiacritics(lookup).toLowerCase()); const className = Array.isArray(this.options.highlightClass) ? this.options.highlightClass.join(' ') : (typeof this.options.highlightClass == 'string' ? this.options.highlightClass : ''); label = item.label.substring(0, idx) @@ -109,7 +111,7 @@ class Autocomplete { value: this.options.value ? entry[this.options.value] : entry }; - if (item.label.toLowerCase().indexOf(lookup.toLowerCase()) >= 0) { + if (removeDiacritics(item.label).toLowerCase().indexOf(removeDiacritics(lookup).toLowerCase()) >= 0) { items.appendChild(this.createItem(lookup, item)); if (this.options.maximumItems > 0 && ++count >= this.options.maximumItems) break; @@ -156,3 +158,13 @@ function ce(html) { function insertAfter(elem, refElem) { return refElem.parentNode.insertBefore(elem, refElem.nextSibling); } + +/** + * @param {String} str + * @returns {String} + */ +function removeDiacritics(str) { + return str + .normalize('NFD') + .replace(/[\u0300-\u036f]/g, ''); +} \ No newline at end of file -- cgit v1.2.3