Normalize unicode search (#263)
* Normalize unicode search * Add option to setup function * Make the call to normalize more explicit
This commit is contained in:
parent
a67b32b3ce
commit
ab3d9ddb13
1 changed files with 12 additions and 5 deletions
|
@ -15,6 +15,7 @@ class AsyncSearch extends EventEmitter {
|
||||||
this.searchKeys = null;
|
this.searchKeys = null;
|
||||||
this.isContain = false;
|
this.isContain = false;
|
||||||
this.isCaseSensitive = false;
|
this.isCaseSensitive = false;
|
||||||
|
this.normalizeUnicode = true;
|
||||||
this.ignoreWhitespace = true;
|
this.ignoreWhitespace = true;
|
||||||
this.limit = null;
|
this.limit = null;
|
||||||
this.findingList = [];
|
this.findingList = [];
|
||||||
|
@ -39,6 +40,7 @@ class AsyncSearch extends EventEmitter {
|
||||||
* @param {string | [string]} [opts.keys=null]
|
* @param {string | [string]} [opts.keys=null]
|
||||||
* @param {boolean} [opts.isContain=false] - Add finding to result if it contain search term
|
* @param {boolean} [opts.isContain=false] - Add finding to result if it contain search term
|
||||||
* @param {boolean} [opts.isCaseSensitive=false]
|
* @param {boolean} [opts.isCaseSensitive=false]
|
||||||
|
* @param {boolean} [opts.normalizeUnicode=true]
|
||||||
* @param {boolean} [opts.ignoreWhitespace=true]
|
* @param {boolean} [opts.ignoreWhitespace=true]
|
||||||
* @param {number} [opts.limit=null] - Stop search after limit
|
* @param {number} [opts.limit=null] - Stop search after limit
|
||||||
*/
|
*/
|
||||||
|
@ -48,6 +50,7 @@ class AsyncSearch extends EventEmitter {
|
||||||
this.searchKeys = opts?.keys || null;
|
this.searchKeys = opts?.keys || null;
|
||||||
this.isContain = opts?.isContain || false;
|
this.isContain = opts?.isContain || false;
|
||||||
this.isCaseSensitive = opts?.isCaseSensitive || false;
|
this.isCaseSensitive = opts?.isCaseSensitive || false;
|
||||||
|
this.normalizeUnicode = opts?.normalizeUnicode || true;
|
||||||
this.ignoreWhitespace = opts?.ignoreWhitespace || true;
|
this.ignoreWhitespace = opts?.ignoreWhitespace || true;
|
||||||
this.limit = opts?.limit || null;
|
this.limit = opts?.limit || null;
|
||||||
}
|
}
|
||||||
|
@ -55,8 +58,7 @@ class AsyncSearch extends EventEmitter {
|
||||||
search(term) {
|
search(term) {
|
||||||
this._softReset();
|
this._softReset();
|
||||||
|
|
||||||
this.term = (this.isCaseSensitive) ? term : term.toLocaleLowerCase();
|
this.term = this._normalize(term);
|
||||||
if (this.ignoreWhitespace) this.term = this.term.replaceAll(' ', '');
|
|
||||||
if (this.term === '') {
|
if (this.term === '') {
|
||||||
this._sendFindings();
|
this._sendFindings();
|
||||||
return;
|
return;
|
||||||
|
@ -113,13 +115,18 @@ class AsyncSearch extends EventEmitter {
|
||||||
|
|
||||||
_compare(item) {
|
_compare(item) {
|
||||||
if (typeof item !== 'string') return false;
|
if (typeof item !== 'string') return false;
|
||||||
let myItem = (this.isCaseSensitive) ? item : item.toLocaleLowerCase();
|
const myItem = this._normalize(item);
|
||||||
if (this.ignoreWhitespace) myItem = myItem.replaceAll(' ', '');
|
|
||||||
|
|
||||||
if (this.isContain) return myItem.indexOf(this.term) !== -1;
|
if (this.isContain) return myItem.indexOf(this.term) !== -1;
|
||||||
return myItem.startsWith(this.term);
|
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() {
|
_sendFindings() {
|
||||||
this.emit(this.RESULT_SENT, this.findingList, this.term);
|
this.emit(this.RESULT_SENT, this.findingList, this.term);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue