@adaptabletools/adaptable
Version:
Powerful AG Grid extension which provides advanced, cutting-edge functionality to meet all DataGrid requirements
149 lines (148 loc) • 4.73 kB
JavaScript
export function IsNull(stringToCheck) {
return stringToCheck == null || stringToCheck == undefined;
}
export function IsNotNull(stringToCheck) {
return !IsNull(stringToCheck);
}
export function IsEmpty(stringToCheck) {
return stringToCheck === '';
}
export function IsNotEmpty(stringToCheck) {
return !IsEmpty(stringToCheck);
}
export function IsNullOrEmpty(stringToCheck) {
return IsNull(stringToCheck) || IsEmpty(stringToCheck);
}
export function IsNotNullOrEmpty(stringToCheck) {
return !IsNullOrEmpty(stringToCheck);
}
export function IsNullOrEmptyOrWhiteSpace(stringToCheck) {
return IsNullOrEmpty(stringToCheck) || IsEmpty(stringToCheck?.trim());
}
export function IsNotNullOrEmptyOrWhiteSpace(stringToCheck) {
return !IsNullOrEmptyOrWhiteSpace(stringToCheck);
}
export function CamelCaseToHumanText(camelCase) {
if (!camelCase || camelCase == null) {
return null;
}
const rex = /([a-z])([A-Z])/g;
const rexCaps = /([A-Z]+)([A-Z])([a-z])/g;
const words = camelCase
.replace(rex, '$1 $2')
.replace(rexCaps, '$1 $2$3')
.replace(/\./g, ' ')
.split(' ');
return words
.map((word) => word.substring(0, 1).toUpperCase() + (word.length > 1 ? word.substring(1, word.length) : ''))
.join(' ');
}
export function RemoveTrailingComma(stringToCheck) {
return stringToCheck.replace(/,\s*$/, '');
}
export function ToLowerCase(stringToCheck) {
return IsNullOrEmpty(stringToCheck) ? stringToCheck : stringToCheck.toLowerCase();
}
export function Includes(stringToCheck, valueToCheck) {
return stringToCheck.includes(valueToCheck);
}
export function NotIncludes(stringToCheck, valueToCheck) {
return !Includes(stringToCheck, valueToCheck);
}
export function AbbreviateString(stringToAbbreviate, maxLength) {
return stringToAbbreviate.length < maxLength
? stringToAbbreviate
: stringToAbbreviate.substr(0, maxLength) + '...';
}
export function CapitaliseFirstLetter(str) {
return `${str.charAt(0).toUpperCase()}${str.substring(1)}`;
}
export function Humanize(str) {
return startCase(str);
}
export function ReplaceEmptySpacesWithUnderscore(str = '') {
return str.replace(/ /g, '_');
}
export function IsNumeric(str) {
if (typeof str !== 'string') {
return false;
}
return !isNaN(parseFloat(str));
}
export function UnescapeHtmlEntities(str) {
if (!str || typeof str !== 'string') {
return str;
}
let preparedStr = str;
const map = [
{ target: /'|'/gi, replacement: "'" },
{ target: /"|"/gi, replacement: '"' },
{ target: /&|&/gi, replacement: '&' },
{ target: /<|</gi, replacement: '<' },
{ target: />|>/gi, replacement: '>' },
{ target: / | /gi, replacement: ' ' },
];
for (const transform of map) {
preparedStr = preparedStr.replace(transform.target, transform.replacement);
}
return preparedStr;
}
export function ReplaceAllOccurencesExceptLastOne(str, searchTerm, replacement) {
const lastIndex = str.lastIndexOf(searchTerm);
if (lastIndex === -1) {
return str;
}
const partBeforeLast = str.slice(0, lastIndex);
const partAfterLast = str.slice(lastIndex);
const replacedPart = partBeforeLast.replaceAll(searchTerm, replacement);
return replacedPart + partAfterLast;
}
export function words(str) {
const matches = str.match(/[A-Z]?[a-z]+|[A-Z]+(?=[A-Z][a-z]|\d|$|[^a-zA-Z0-9])|[A-Z]|[0-9]+/g);
return matches || [];
}
function _capitalizeWord(word) {
return word.charAt(0).toUpperCase() + word.slice(1).toLowerCase();
}
export function kebabCase(string) {
return words(string).map((w) => w.toLowerCase()).join('-');
}
export function startCase(string) {
return words(string).map(_capitalizeWord).join(' ');
}
export function sentenceCase(input) {
const parts = words(input);
if (parts.length === 0) {
return '';
}
return parts
.map((w, i) => (i === 0 ? w.charAt(0).toUpperCase() + w.slice(1).toLowerCase() : w.toLowerCase()))
.join(' ');
}
export const StringExtensions = {
IsNull,
IsNotNull,
IsEmpty,
IsNotEmpty,
IsNullOrEmpty,
IsNotNullOrEmpty,
IsNullOrEmptyOrWhiteSpace,
IsNotNullOrEmptyOrWhiteSpace,
CamelCaseToHumanText,
RemoveTrailingComma,
ToLowerCase,
Includes,
NotIncludes,
AbbreviateString,
CapitaliseFirstLetter,
Humanize,
ReplaceEmptySpacesWithUnderscore,
IsNumeric,
UnescapeHtmlEntities,
ReplaceAllOccurencesExceptLastOne,
words,
kebabCase,
startCase,
sentenceCase,
};
export default StringExtensions;