2014-11-16 01:29:07 +01:00
|
|
|
/**
|
|
|
|
* String utilities
|
|
|
|
*/
|
|
|
|
|
2016-05-25 09:31:53 +02:00
|
|
|
/**
|
|
|
|
* Removes whitespace from both sides of passed string
|
|
|
|
* @param {String} text
|
|
|
|
* @return {String}
|
|
|
|
*/
|
2016-05-20 10:08:39 +02:00
|
|
|
export const trim = text => {
|
|
|
|
if (text.trim) {
|
|
|
|
return text.trim();
|
|
|
|
}
|
|
|
|
return text.replace(/^\s*|\s*$/g, '');
|
|
|
|
}
|
2015-05-30 14:23:33 +02:00
|
|
|
|
2016-05-25 09:31:53 +02:00
|
|
|
/**
|
|
|
|
* Checks if passed string is empty
|
|
|
|
* @param {String} text
|
|
|
|
* @return {Boolean}
|
|
|
|
*/
|
2016-05-20 10:08:39 +02:00
|
|
|
export const isEmpty = (text) => trim(text) === '';
|
2015-05-30 14:23:33 +02:00
|
|
|
|
2016-05-25 09:31:53 +02:00
|
|
|
/**
|
|
|
|
* Makes regex safe string by escaping special characters from passed string
|
|
|
|
* @param {String} text
|
|
|
|
* @return {String} escaped string
|
|
|
|
*/
|
2016-05-20 10:08:39 +02:00
|
|
|
export const rgxEsc = text => {
|
|
|
|
let chars = /[-\/\\^$*+?.()|[\]{}]/g;
|
|
|
|
let escMatch = '\\$&';
|
|
|
|
return String(text).replace(chars, escMatch);
|
|
|
|
}
|
2015-05-30 14:23:33 +02:00
|
|
|
|
2016-05-25 09:31:53 +02:00
|
|
|
/**
|
|
|
|
* Returns passed string as lowercase if caseSensitive flag set false. By
|
|
|
|
* default it returns the string with no casing changes.
|
|
|
|
* @param {String} text
|
|
|
|
* @return {String} string
|
|
|
|
*/
|
2016-05-26 06:36:36 +02:00
|
|
|
export const matchCase = (text, caseSensitive = false) => {
|
2016-05-20 10:08:39 +02:00
|
|
|
if (!caseSensitive) {
|
|
|
|
return text.toLowerCase();
|
|
|
|
}
|
|
|
|
return text;
|
|
|
|
}
|
2015-12-05 14:37:59 +01:00
|
|
|
|
2016-05-20 10:08:39 +02: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}
|
|
|
|
*/
|
|
|
|
export const contains =
|
|
|
|
(term, data, exactMatch = false, caseSensitive = false) => {
|
2015-12-05 14:37:59 +01:00
|
|
|
// Improved by Cedric Wartel (cwl) automatic exact match for selects and
|
|
|
|
// special characters are now filtered
|
2016-05-20 10:08:39 +02:00
|
|
|
let regexp;
|
|
|
|
let modifier = caseSensitive ? 'g' : 'gi';
|
|
|
|
if (exactMatch) {
|
|
|
|
regexp = new RegExp('(^\\s*)' + rgxEsc(term) + '(\\s*$)',
|
|
|
|
modifier);
|
2015-12-05 14:37:59 +01:00
|
|
|
} else {
|
2016-05-20 10:08:39 +02:00
|
|
|
regexp = new RegExp(rgxEsc(term), modifier);
|
2015-12-05 14:37:59 +01:00
|
|
|
}
|
|
|
|
return regexp.test(data);
|
2014-11-16 01:29:07 +01:00
|
|
|
}
|