UNPKG

@adaptabletools/adaptable-cjs

Version:

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

140 lines (139 loc) 5.35 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.StringExtensions = exports.UnescapeHtmlEntities = exports.IsNumeric = exports.ReplaceEmptySpacesWithUnderscore = exports.Humanize = exports.CapitaliseFirstLetter = exports.AbbreviateString = exports.NotIncludes = exports.Includes = exports.ToLowerCase = exports.RemoveTrailingComma = exports.CamelCaseToHumanText = exports.IsNotNullOrEmptyOrWhiteSpace = exports.IsNullOrEmptyOrWhiteSpace = exports.IsNotNullOrEmpty = exports.IsNullOrEmpty = exports.IsNotEmpty = exports.IsEmpty = exports.IsNotNull = exports.IsNull = void 0; const tslib_1 = require("tslib"); const startCase_1 = tslib_1.__importDefault(require("lodash/startCase")); function IsNull(stringToCheck) { return stringToCheck == null || stringToCheck == undefined; } exports.IsNull = IsNull; function IsNotNull(stringToCheck) { return !IsNull(stringToCheck); } exports.IsNotNull = IsNotNull; function IsEmpty(stringToCheck) { return stringToCheck == ''; } exports.IsEmpty = IsEmpty; function IsNotEmpty(stringToCheck) { return !IsEmpty(stringToCheck); } exports.IsNotEmpty = IsNotEmpty; function IsNullOrEmpty(stringToCheck) { return IsNull(stringToCheck) || IsEmpty(stringToCheck); } exports.IsNullOrEmpty = IsNullOrEmpty; function IsNotNullOrEmpty(stringToCheck) { return !IsNullOrEmpty(stringToCheck); } exports.IsNotNullOrEmpty = IsNotNullOrEmpty; function IsNullOrEmptyOrWhiteSpace(stringToCheck) { return IsNullOrEmpty(stringToCheck) || IsEmpty(stringToCheck.trim()); } exports.IsNullOrEmptyOrWhiteSpace = IsNullOrEmptyOrWhiteSpace; function IsNotNullOrEmptyOrWhiteSpace(stringToCheck) { return !IsNullOrEmptyOrWhiteSpace(stringToCheck); } exports.IsNotNullOrEmptyOrWhiteSpace = IsNotNullOrEmptyOrWhiteSpace; // 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(' '); } exports.CamelCaseToHumanText = CamelCaseToHumanText; function RemoveTrailingComma(stringToCheck) { return stringToCheck.replace(/,\s*$/, ''); } exports.RemoveTrailingComma = RemoveTrailingComma; function ToLowerCase(stringToCheck) { return IsNullOrEmpty(stringToCheck) ? stringToCheck : stringToCheck.toLowerCase(); } exports.ToLowerCase = ToLowerCase; function Includes(stringToCheck, valueToCheck) { return stringToCheck.includes(valueToCheck); } exports.Includes = Includes; function NotIncludes(stringToCheck, valueToCheck) { return !Includes(stringToCheck, valueToCheck); } exports.NotIncludes = NotIncludes; function AbbreviateString(stringToAbbreviate, maxLength) { return stringToAbbreviate.length < maxLength ? stringToAbbreviate : stringToAbbreviate.substr(0, maxLength) + '...'; } exports.AbbreviateString = AbbreviateString; function CapitaliseFirstLetter(str) { return `${str.charAt(0).toUpperCase()}${str.substring(1)}`; } exports.CapitaliseFirstLetter = CapitaliseFirstLetter; function Humanize(str) { return (0, startCase_1.default)(str); } exports.Humanize = Humanize; function ReplaceEmptySpacesWithUnderscore(str = '') { return str.replace(/ /g, '_'); } exports.ReplaceEmptySpacesWithUnderscore = ReplaceEmptySpacesWithUnderscore; function IsNumeric(str) { if (typeof str !== 'string') { return false; } return !isNaN(parseFloat(str)); } exports.IsNumeric = IsNumeric; 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; } exports.UnescapeHtmlEntities = UnescapeHtmlEntities; exports.StringExtensions = { IsNull, IsNotNull, IsEmpty, IsNotEmpty, IsNullOrEmpty, IsNotNullOrEmpty, IsNullOrEmptyOrWhiteSpace, IsNotNullOrEmptyOrWhiteSpace, CamelCaseToHumanText, RemoveTrailingComma, ToLowerCase, Includes, NotIncludes, AbbreviateString, CapitaliseFirstLetter, Humanize, ReplaceEmptySpacesWithUnderscore, IsNumeric, UnescapeHtmlEntities, }; exports.default = exports.StringExtensions;