1
0
Fork 0
mirror of https://github.com/koalyptus/TableFilter.git synced 2024-04-28 12:52:49 +02:00
TableFilter/src/string.js

113 lines
3.2 KiB
JavaScript
Raw Normal View History

2016-10-22 04:07:57 +02:00
import {remove as removeDiacritics} from 'diacritics';
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-10-22 04:07:57 +02:00
export const trim = (text) => {
2016-05-20 10:08:39 +02:00
if (text.trim) {
return text.trim();
}
return text.replace(/^\s*|\s*$/g, '');
2017-05-25 05:51:44 +02:00
};
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-10-22 04:07:57 +02:00
export const rgxEsc = (text) => {
2016-05-20 10:08:39 +02:00
let chars = /[-\/\\^$*+?.()|[\]{}]/g;
let escMatch = '\\$&';
return String(text).replace(chars, escMatch);
2017-05-25 05:51:44 +02:00
};
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
*/
export const matchCase = (text, caseSensitive = false) => {
2016-05-20 10:08:39 +02:00
if (!caseSensitive) {
return text.toLowerCase();
}
return text;
2017-05-25 05:51:44 +02:00
};
2015-12-05 14:37:59 +01:00
2016-05-20 10:08:39 +02:00
/**
* Checks if passed data contains the searched term
2016-10-22 04:07:57 +02:00
* @param {String} term Searched term
* @param {String} data Data string
* @param {Boolean} exactMatch Exact match
* @param {Boolean} caseSensitive Case sensitive
* @param {Boolean} ignoreDiacritics Ignore diacritics
2016-05-20 10:08:39 +02:00
* @return {Boolean}
*/
2016-10-22 04:07:57 +02:00
export const contains = (term, data, exactMatch = false, caseSensitive = false,
ignoreDiacritics = false) => {
// Improved by Cedric Wartel (cwl) automatic exact match for selects and
// special characters are now filtered
let regexp;
let modifier = caseSensitive ? 'g' : 'gi';
if (ignoreDiacritics) {
term = removeDiacritics(term);
data = removeDiacritics(data);
}
if (exactMatch) {
regexp = new RegExp('(^\\s*)' + rgxEsc(term) + '(\\s*$)',
modifier);
} else {
regexp = new RegExp(rgxEsc(term), modifier);
2014-11-16 01:29:07 +01:00
}
2016-10-22 04:07:57 +02:00
return regexp.test(data);
2017-05-25 05:51:44 +02:00
};
2019-01-15 13:29:13 +01:00
/**
* Camelize a string, cutting the string by multiple separators like
* hyphens, underscores and spaces.
* @param {String} text text to camelize
* @return {String} camelized text
*/
export const toCamelCase = (text = '') => {
return text.replace(/^([A-Z])|[\s-_]+(\w)/g, (match, p1, p2) => {
if (p2) {
return p2.toUpperCase();
}
return p1.toLowerCase();
});
};
/**
* Generate a string in the format of a UUID (Universally Unique IDentifier).
* NOTE: This format of 8 chars, followed by 3 groups of 4 chars, followed by 12
* chars is known as a UUID and is defined in RFC4122 and is a standard for
* generating unique IDs. This function DOES NOT implement this standard.
* It simply outputs a string that looks similar. The standard is found here:
* https://www.ietf.org/rfc/rfc4122.txt
* source: https://gist.github.com/gordonbrander/2230317
* @return {String}
*/
export const uuid = () => {
const chr4 = () => Math.random().toString(16).slice(-4);
return chr4() + chr4()
+ '-' + chr4()
+ '-' + chr4()
+ '-' + chr4()
+ '-' + chr4()
+ chr4() + chr4();
};