mirror of
https://github.com/Choices-js/Choices.git
synced 2024-05-10 09:36:35 +02:00
Move regex filter into util
This commit is contained in:
parent
d0a3c99ef5
commit
bfef79d3ee
|
@ -27,7 +27,8 @@ import {
|
|||
sortByScore,
|
||||
generateId,
|
||||
triggerEvent,
|
||||
findAncestorByAttrName
|
||||
findAncestorByAttrName,
|
||||
regexFilter
|
||||
}
|
||||
from './lib/utils.js';
|
||||
import './lib/polyfills.js';
|
||||
|
@ -1321,13 +1322,11 @@ class Choices {
|
|||
}
|
||||
}
|
||||
|
||||
if (this.isTextElement && this.config.addItems && canAddItem) {
|
||||
if (this.isTextElement && this.config.addItems && canAddItem && this.config.regexFilter) {
|
||||
// If a user has supplied a regular expression filter
|
||||
if (this.config.regexFilter) {
|
||||
// Determine whether we can update based on whether
|
||||
// our regular expression passes
|
||||
canAddItem = this._regexFilter(value);
|
||||
}
|
||||
// determine whether we can update based on whether
|
||||
// our regular expression passes
|
||||
canAddItem = regexFilter(value, this.config.regexFilter);
|
||||
}
|
||||
|
||||
// If no duplicates are allowed, and the value already exists
|
||||
|
@ -2080,22 +2079,6 @@ class Choices {
|
|||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests value against a regular expression
|
||||
* @param {string} value Value to test
|
||||
* @return {Boolean} Whether test passed/failed
|
||||
* @private
|
||||
*/
|
||||
_regexFilter(value) {
|
||||
if (!value) {
|
||||
return false;
|
||||
}
|
||||
|
||||
const regex = this.config.regexFilter;
|
||||
const expression = new RegExp(regex.source, 'i');
|
||||
return expression.test(value);
|
||||
}
|
||||
|
||||
/**
|
||||
* Scroll to an option element
|
||||
* @param {HTMLElement} choice Option to scroll to
|
||||
|
|
|
@ -580,3 +580,18 @@ export const triggerEvent = (element, type, customArgs = null) => {
|
|||
|
||||
return element.dispatchEvent(event);
|
||||
};
|
||||
|
||||
/**
|
||||
* Tests value against a regular expression
|
||||
* @param {string} value Value to test
|
||||
* @return {Boolean} Whether test passed/failed
|
||||
* @private
|
||||
*/
|
||||
export const regexFilter = (value, regex) => {
|
||||
if (!value || !regex) {
|
||||
return false;
|
||||
}
|
||||
|
||||
const expression = new RegExp(regex.source, 'i');
|
||||
return expression.test(value);
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue