UNPKG

@adaptabletools/adaptable

Version:

Powerful data-agnostic HTML5 AG Grid extension which provides advanced, cutting-edge functionality to meet all DataGrid requirements

117 lines (116 loc) 3.96 kB
import startCase from 'lodash/startCase'; 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); } // clone of A Grid's implementation // https://github.com/ag-grid/ag-grid/blob/a5c3d9f56350271ab7e8d42d72aaf5314672719a/packages/ag-grid-community/src/columns/columnNameService.ts#L14 export function CamelCaseToHumanText(camelCase) { if (!camelCase || camelCase == null) { return null; } // either split on a lowercase followed by uppercase ie asHereTo -> as Here To const rex = /([a-z])([A-Z])/g; // or starts with uppercase and we take all expect the last which is assumed to be part of next word if followed by lowercase HEREToThere -> HERE To There 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: /&apos;|&#39;/gi, replacement: "'" }, { target: /&quot;|&#34;/gi, replacement: '"' }, { target: /&amp;|&#38;/gi, replacement: '&' }, { target: /&lt;|&#60;/gi, replacement: '<' }, { target: /&gt;|&#62;/gi, replacement: '>' }, { target: /&nbsp;|&#160;/gi, replacement: ' ' }, ]; for (const transform of map) { preparedStr = preparedStr.replace(transform.target, transform.replacement); } return preparedStr; } export const StringExtensions = { IsNull, IsNotNull, IsEmpty, IsNotEmpty, IsNullOrEmpty, IsNotNullOrEmpty, IsNullOrEmptyOrWhiteSpace, IsNotNullOrEmptyOrWhiteSpace, CamelCaseToHumanText, RemoveTrailingComma, ToLowerCase, Includes, NotIncludes, AbbreviateString, CapitaliseFirstLetter, Humanize, ReplaceEmptySpacesWithUnderscore, IsNumeric, UnescapeHtmlEntities, }; export default StringExtensions;