Move regex filter into util

This commit is contained in:
Josh Johnson 2017-08-10 11:57:17 +01:00
parent d0a3c99ef5
commit bfef79d3ee
2 changed files with 21 additions and 23 deletions

View file

@ -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

View file

@ -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);
}