1
0
Fork 0
mirror of https://github.com/koalyptus/TableFilter.git synced 2026-03-16 07:35:45 +01:00

Refactored string module

This commit is contained in:
Max Guglielmi 2016-05-20 18:08:39 +10:00
commit b6c4fd6675
13 changed files with 182 additions and 238 deletions

View file

@ -2,60 +2,47 @@
* String utilities
*/
export default {
export const trim = text => {
if (text.trim) {
return text.trim();
}
return text.replace(/^\s*|\s*$/g, '');
}
lower(text){
export const isEmpty = (text) => trim(text) === '';
export const rgxEsc = text => {
let chars = /[-\/\\^$*+?.()|[\]{}]/g;
let escMatch = '\\$&';
return String(text).replace(chars, escMatch);
}
export const matchCase = (text, caseSensitive) => {
if (!caseSensitive) {
return text.toLowerCase();
},
}
return text;
}
upper(text){
return text.toUpperCase();
},
trim(text){
if (text.trim){
return text.trim();
}
return text.replace(/^\s*|\s*$/g, '');
},
isEmpty(text){
return this.trim(text) === '';
},
rgxEsc(text){
let chars = /[-\/\\^$*+?.()|[\]{}]/g;
let escMatch = '\\$&';
return String(text).replace(chars, escMatch);
},
matchCase(text, caseSensitive){
if(!caseSensitive){
return this.lower(text);
}
return text;
},
/**
* Checks if passed data contains the searched term
* @param {String} term Searched term
* @param {String} data Data string
* @param {Boolean} exactMatch Exact match
* @param {Boolean} caseSensitive Case sensitive
* @return {Boolean}
*/
contains(term, data, exactMatch=false, caseSensitive=false){
/**
* Checks if passed data contains the searched term
* @param {String} term Searched term
* @param {String} data Data string
* @param {Boolean} exactMatch Exact match
* @param {Boolean} caseSensitive Case sensitive
* @return {Boolean}
*/
export const contains =
(term, data, exactMatch = false, caseSensitive = false) => {
// Improved by Cedric Wartel (cwl) automatic exact match for selects and
// special characters are now filtered
let regexp,
modifier = caseSensitive ? 'g' : 'gi';
if(exactMatch){
regexp = new RegExp(
'(^\\s*)'+ this.rgxEsc(term) +'(\\s*$)', modifier);
let regexp;
let modifier = caseSensitive ? 'g' : 'gi';
if (exactMatch) {
regexp = new RegExp('(^\\s*)' + rgxEsc(term) + '(\\s*$)',
modifier);
} else {
regexp = new RegExp(this.rgxEsc(term), modifier);
regexp = new RegExp(rgxEsc(term), modifier);
}
return regexp.test(data);
}
};