@pawel-up/jexl
Version:
Javascript Expression Language: Powerful context-based expression parser and evaluator
158 lines • 5.14 kB
JavaScript
export const UPPER = (val) => val.toUpperCase();
export const LOWER = (val) => val.toLowerCase();
export const SUBSTR = (val, start, end) => val.substring(start, end);
export const SPLIT = (val, separator) => val.split(separator);
export const REPLACE = (val, oldStr, newStr) => val.replace(oldStr, newStr);
export const CAPITALIZE = (val) => {
if (!val)
return val;
return val.charAt(0).toUpperCase() + val.slice(1).toLowerCase();
};
export const TITLE_CASE = (val) => {
return val.replace(/\b\w/g, (char) => char.toUpperCase());
};
export const CAMEL_CASE = (val) => {
let trimmed = val.trim();
trimmed = trimmed
.replace(/[A-Z]{2,}/g, (match) => match.charAt(0) + match.slice(1).toLowerCase())
.replace(/[^a-zA-Z0-9]+(.)/g, (_, character) => character.toUpperCase())
.replace(/[^a-zA-Z0-9]/g, '');
trimmed = trimmed[0].toLowerCase() + trimmed.substring(1);
return trimmed;
};
export const PASCAL_CASE = (val) => {
let trimmed = val.trim();
trimmed = trimmed
.replace(/[A-Z]{2,}/g, (match) => match.charAt(0) + match.slice(1).toLowerCase())
.replace(/[^a-zA-Z0-9]+(.)/g, (_, character) => character.toUpperCase())
.replace(/[^a-zA-Z0-9]/g, '');
if (!trimmed) {
return trimmed;
}
trimmed = trimmed[0].toUpperCase() + trimmed.substring(1);
return trimmed;
};
export const SNAKE_CASE = (val) => {
let trimmed = val.trim();
trimmed =
trimmed
.match(/[A-Z]{2,}(?=[ A-Z][a-z]+[0-9]*|\b)|[A-Z]?[a-z]+[0-9]*|[A-Z]|[0-9]+/g)
?.map((x) => x.toLowerCase())
.join('_') || '';
return trimmed;
};
export const KEBAB_CASE = (val) => {
let trimmed = val.trim();
trimmed =
trimmed
.match(/[A-Z]{2,}(?=[ A-Z][a-z]+[0-9]*|\b)|[A-Z]?[a-z]+[0-9]*|[A-Z]|[0-9]+/g)
?.map((x) => x.toLowerCase())
.join('-') || '';
return trimmed;
};
export const PAD = (val, targetLength, padString = ' ') => {
return val.padStart(targetLength, padString);
};
export const PAD_START = (val, targetLength, padString = ' ') => {
return val.padStart(targetLength, padString);
};
export const PAD_END = (val, targetLength, padString = ' ') => {
return val.padEnd(targetLength, padString);
};
export const REPEAT = (val, count) => {
return val.repeat(count);
};
export const TRUNCATE = (val, maxLength, suffix = '...') => {
if (val.length <= maxLength)
return val;
return val.substring(0, maxLength - suffix.length) + suffix;
};
export const STARTS_WITH = (val, searchString) => {
return val.startsWith(searchString);
};
export const ENDS_WITH = (val, searchString) => {
return val.endsWith(searchString);
};
export const REPLACE_ALL = (val, searchValue, replaceValue) => {
return val.replaceAll(searchValue, replaceValue);
};
export const TRIM = (val) => val.trim();
export const TRIM_START = (val) => {
return val.trimStart();
};
export const TRIM_END = (val) => {
return val.trimEnd();
};
export const SLUG = (val) => {
return val
.toLowerCase()
.trim()
.replace(/[^\w\s-]/g, '')
.replace(/[\s_-]+/g, '-')
.replace(/^-+|-+$/g, '');
};
export const ESCAPE_HTML = (val) => {
const htmlEscapes = {
'&': '&',
'<': '<',
'>': '>',
'"': '"',
"'": ''',
};
return val.replace(/[&<>"']/g, (match) => htmlEscapes[match]);
};
export const UNESCAPE_HTML = (val) => {
const htmlUnescapes = {
'&': '&',
'<': '<',
'>': '>',
'"': '"',
''': "'",
};
return val.replace(/&(?:amp|lt|gt|quot|#39);/g, (match) => htmlUnescapes[match]);
};
export const WORD_COUNT = (val) => {
if (!val || val.trim().length === 0)
return 0;
return val
.trim()
.split(/\s+/)
.filter((word) => word.length > 0).length;
};
export const CHAR_COUNT = (val) => {
return val.length;
};
export const INITIALS = (val) => {
if (!val || val.trim().length === 0)
return '';
return val
.trim()
.split(/\s+/)
.filter((word) => word.length > 0)
.map((word) => word.charAt(0).toUpperCase())
.join('');
};
export const LINES = (val) => {
return val.split(/\r?\n/);
};
export const NORMALIZE_SPACE = (val) => {
return val.replace(/\s+/g, ' ').trim();
};
export const MASK = (val, maskChar = '*', visibleStart = 0, visibleEnd = 0) => {
if (val.length <= visibleStart + visibleEnd)
return val;
const start = val.substring(0, visibleStart);
const end = val.substring(val.length - visibleEnd);
const middle = maskChar.repeat(val.length - visibleStart - visibleEnd);
return start + middle + end;
};
export const BETWEEN = (val, startDelim, endDelim) => {
const start = val.indexOf(startDelim);
if (start === -1)
return '';
const end = val.indexOf(endDelim, start + startDelim.length);
if (end === -1)
return '';
return val.substring(start + startDelim.length, end);
};
//# sourceMappingURL=string.js.map