UNPKG

@adaptabletools/adaptable-cjs

Version:

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

157 lines (156 loc) 5.7 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.StringExtensions = void 0; exports.IsNull = IsNull; exports.IsNotNull = IsNotNull; exports.IsEmpty = IsEmpty; exports.IsNotEmpty = IsNotEmpty; exports.IsNullOrEmpty = IsNullOrEmpty; exports.IsNotNullOrEmpty = IsNotNullOrEmpty; exports.IsNullOrEmptyOrWhiteSpace = IsNullOrEmptyOrWhiteSpace; exports.IsNotNullOrEmptyOrWhiteSpace = IsNotNullOrEmptyOrWhiteSpace; exports.CamelCaseToHumanText = CamelCaseToHumanText; exports.RemoveTrailingComma = RemoveTrailingComma; exports.ToLowerCase = ToLowerCase; exports.Includes = Includes; exports.NotIncludes = NotIncludes; exports.AbbreviateString = AbbreviateString; exports.CapitaliseFirstLetter = CapitaliseFirstLetter; exports.Humanize = Humanize; exports.ReplaceEmptySpacesWithUnderscore = ReplaceEmptySpacesWithUnderscore; exports.IsNumeric = IsNumeric; exports.UnescapeHtmlEntities = UnescapeHtmlEntities; exports.ReplaceAllOccurencesExceptLastOne = ReplaceAllOccurencesExceptLastOne; const tslib_1 = require("tslib"); const startCase_1 = tslib_1.__importDefault(require("../utils/startCase")); function IsNull(stringToCheck) { return stringToCheck == null || stringToCheck == undefined; } function IsNotNull(stringToCheck) { return !IsNull(stringToCheck); } function IsEmpty(stringToCheck) { return stringToCheck === ''; } function IsNotEmpty(stringToCheck) { return !IsEmpty(stringToCheck); } function IsNullOrEmpty(stringToCheck) { return IsNull(stringToCheck) || IsEmpty(stringToCheck); } function IsNotNullOrEmpty(stringToCheck) { return !IsNullOrEmpty(stringToCheck); } function IsNullOrEmptyOrWhiteSpace(stringToCheck) { return IsNullOrEmpty(stringToCheck) || IsEmpty(stringToCheck?.trim()); } 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 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(' '); } function RemoveTrailingComma(stringToCheck) { return stringToCheck.replace(/,\s*$/, ''); } function ToLowerCase(stringToCheck) { return IsNullOrEmpty(stringToCheck) ? stringToCheck : stringToCheck.toLowerCase(); } function Includes(stringToCheck, valueToCheck) { return stringToCheck.includes(valueToCheck); } function NotIncludes(stringToCheck, valueToCheck) { return !Includes(stringToCheck, valueToCheck); } function AbbreviateString(stringToAbbreviate, maxLength) { return stringToAbbreviate.length < maxLength ? stringToAbbreviate : stringToAbbreviate.substr(0, maxLength) + '...'; } function CapitaliseFirstLetter(str) { return `${str.charAt(0).toUpperCase()}${str.substring(1)}`; } function Humanize(str) { return (0, startCase_1.default)(str); } function ReplaceEmptySpacesWithUnderscore(str = '') { return str.replace(/ /g, '_'); } function IsNumeric(str) { if (typeof str !== 'string') { return false; } return !isNaN(parseFloat(str)); } 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; } function ReplaceAllOccurencesExceptLastOne(str, searchTerm, replacement) { // Find the index of the last occurrence of the search string const lastIndex = str.lastIndexOf(searchTerm); // If the string is not found, return the original string if (lastIndex === -1) { return str; } // Slice the string into two parts: before and after the last occurrence const partBeforeLast = str.slice(0, lastIndex); const partAfterLast = str.slice(lastIndex); // Replace all occurrences in the first part const replacedPart = partBeforeLast.replaceAll(searchTerm, replacement); // Recombine the modified first part with the untouched second part return replacedPart + partAfterLast; } exports.StringExtensions = { IsNull, IsNotNull, IsEmpty, IsNotEmpty, IsNullOrEmpty, IsNotNullOrEmpty, IsNullOrEmptyOrWhiteSpace, IsNotNullOrEmptyOrWhiteSpace, CamelCaseToHumanText, RemoveTrailingComma, ToLowerCase, Includes, NotIncludes, AbbreviateString, CapitaliseFirstLetter, Humanize, ReplaceEmptySpacesWithUnderscore, IsNumeric, UnescapeHtmlEntities, ReplaceAllOccurencesExceptLastOne, }; exports.default = exports.StringExtensions;