1
0
Fork 0
mirror of https://github.com/koalyptus/TableFilter.git synced 2024-05-12 19:36:41 +02:00
TableFilter/src/string.js

62 lines
1.5 KiB
JavaScript
Raw Normal View History

2014-11-16 01:29:07 +01:00
/**
* String utilities
*/
2015-05-30 14:23:33 +02:00
export default {
lower(text){
return text.toLowerCase();
},
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){
2015-06-13 17:00:31 +02:00
let chars = /[-\/\\^$*+?.()|[\]{}]/g;
let escMatch = '\\$&';
return String(text).replace(chars, escMatch);
2015-05-30 14:23:33 +02:00
},
2015-12-05 14:37:59 +01:00
matchCase(text, caseSensitive){
if(!caseSensitive){
2015-05-30 14:23:33 +02:00
return this.lower(text);
}
return text;
2015-12-05 14:37:59 +01:00
},
/**
* 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){
// 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);
} else {
regexp = new RegExp(this.rgxEsc(term), modifier);
}
return regexp.test(data);
2014-11-16 01:29:07 +01:00
}
};