From ab3d9ddb132ae09fa752f72a3136e56207449cd5 Mon Sep 17 00:00:00 2001 From: ginnyTheCat Date: Thu, 27 Jan 2022 11:05:53 +0100 Subject: [PATCH] Normalize unicode search (#263) * Normalize unicode search * Add option to setup function * Make the call to normalize more explicit --- src/util/AsyncSearch.js | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/src/util/AsyncSearch.js b/src/util/AsyncSearch.js index 82123c7..eb39f29 100644 --- a/src/util/AsyncSearch.js +++ b/src/util/AsyncSearch.js @@ -15,6 +15,7 @@ class AsyncSearch extends EventEmitter { this.searchKeys = null; this.isContain = false; this.isCaseSensitive = false; + this.normalizeUnicode = true; this.ignoreWhitespace = true; this.limit = null; this.findingList = []; @@ -39,6 +40,7 @@ class AsyncSearch extends EventEmitter { * @param {string | [string]} [opts.keys=null] * @param {boolean} [opts.isContain=false] - Add finding to result if it contain search term * @param {boolean} [opts.isCaseSensitive=false] + * @param {boolean} [opts.normalizeUnicode=true] * @param {boolean} [opts.ignoreWhitespace=true] * @param {number} [opts.limit=null] - Stop search after limit */ @@ -48,6 +50,7 @@ class AsyncSearch extends EventEmitter { this.searchKeys = opts?.keys || null; this.isContain = opts?.isContain || false; this.isCaseSensitive = opts?.isCaseSensitive || false; + this.normalizeUnicode = opts?.normalizeUnicode || true; this.ignoreWhitespace = opts?.ignoreWhitespace || true; this.limit = opts?.limit || null; } @@ -55,8 +58,7 @@ class AsyncSearch extends EventEmitter { search(term) { this._softReset(); - this.term = (this.isCaseSensitive) ? term : term.toLocaleLowerCase(); - if (this.ignoreWhitespace) this.term = this.term.replaceAll(' ', ''); + this.term = this._normalize(term); if (this.term === '') { this._sendFindings(); return; @@ -113,13 +115,18 @@ class AsyncSearch extends EventEmitter { _compare(item) { if (typeof item !== 'string') return false; - let myItem = (this.isCaseSensitive) ? item : item.toLocaleLowerCase(); - if (this.ignoreWhitespace) myItem = myItem.replaceAll(' ', ''); - + const myItem = this._normalize(item); if (this.isContain) return myItem.indexOf(this.term) !== -1; return myItem.startsWith(this.term); } + _normalize(item) { + let myItem = item.normalize(this.normalizeUnicode ? 'NFKC' : 'NFC'); + if (!this.isCaseSensitive) myItem = myItem.toLocaleLowerCase(); + if (this.ignoreWhitespace) myItem = myItem.replaceAll(' ', ''); + return myItem; + } + _sendFindings() { this.emit(this.RESULT_SENT, this.findingList, this.term); }