tsbase
Version:
Base class libraries for TypeScript
65 lines • 2.44 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.Strings = void 0;
var Regex_1 = require("./Regex");
var Strings = /** @class */ (function () {
function Strings() {
}
/**
* 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
*/
Strings.CamelCase = function (string) {
return string.replace(/(?:^\w|[A-Z]|\b\w)/g, function (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
*/
Strings.PascalCase = function (string) {
var _this = this;
return string
.split(Strings.Space)
.map(function (s) { var _a; return "".concat(((_a = s[0]) === null || _a === void 0 ? void 0 : _a.toUpperCase()) || _this.Empty).concat(s.slice(1)); })
.join(Strings.Empty);
};
/**
* Returns true if the given string is null, empty, or consists purely of whitespace
* @param string
*/
Strings.IsEmptyOrWhiteSpace = function (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
*/
Strings.MinifyXml = function (string) {
var newLines = /\r?\n|\r/g;
var 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
*/
Strings.Slugify = function (string) {
return string
.replace(Regex_1.Regex.NonAlphaNumeric, '')
.trim()
.replace(Regex_1.Regex.WhiteSpace, '-')
.toLowerCase();
};
Strings.Empty = '';
Strings.Space = ' ';
return Strings;
}());
exports.Strings = Strings;
//# sourceMappingURL=Strings.js.map