UNPKG

tsbase

Version:

Base class libraries for TypeScript

62 lines 2.25 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.Strings = void 0; const Regex_1 = require("./Regex"); class Strings { constructor() { } /** * Returns the camel cased version of the given string * NOTE: For multi word strings that are not separated by spaces, this function will merely lowercase the first character * @param string */ static CamelCase(string) { return string.replace(/(?:^\w|[A-Z]|\b\w)/g, (word, index) => { return index === 0 ? word.toLowerCase() : word.toUpperCase(); }).replace(/\s+/g, Strings.Empty); } /** * Returns the pascal cased version of the given string * NOTE: For multi word strings that are not separated by spaces, this function will merely uppercase the first character * @param string */ static PascalCase(string) { return string .split(Strings.Space) .map(s => { var _a; return `${((_a = s[0]) === null || _a === void 0 ? void 0 : _a.toUpperCase()) || this.Empty}${s.slice(1)}`; }) .join(Strings.Empty); } /** * Returns true if the given string is null, empty, or consists purely of whitespace * @param string */ static IsEmptyOrWhiteSpace(string) { return !string || string.trim().length === 0; } /** * Returns a version of the given string minus new line characters and whitespace characters between tags * @param string */ static MinifyXml(string) { const newLines = /\r?\n|\r/g; const spacesBetweenXmlTags = /\r?>(\s+)<|\r/g; return string.replace(newLines, Strings.Empty) .replace(spacesBetweenXmlTags, '><'); } /** * Returns a "slugified" version of the given string * Replaces white space with dashes "-", removes non-alphanumeric characters, and lowercases * @param string * @returns */ static Slugify(string) { return string .replace(Regex_1.Regex.NonAlphaNumeric, '') .trim() .replace(Regex_1.Regex.WhiteSpace, '-') .toLowerCase(); } } exports.Strings = Strings; Strings.Empty = ''; Strings.Space = ' '; //# sourceMappingURL=Strings.js.map