UNPKG

typescriptkit

Version:

Basic functionality for TypeScript projects

117 lines (115 loc) 4.96 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const ObjectExtensions_1 = require("./ObjectExtensions"); /** * Helper Utility for the String class */ class StringExtensions { /** * Check whether a function is null or empty * @param stringObject The string to check. */ static isNullOrEmpty(stringObject) { if (ObjectExtensions_1.ObjectExtensions.isNullOrUndefined(stringObject)) return true; if (stringObject === String()) return true; return false; } /** * Check whether a function is null or consists of whitespace * @param stringObject The string to check. */ static isNullOrWhitespace(stringObject) { if (ObjectExtensions_1.ObjectExtensions.isNullOrUndefined(stringObject)) return true; return StringExtensions.isNullOrEmpty(stringObject .toString() // <- Failsafe incase it's not a string .trim()); } /** * Check whether a string starts with a certain character * @param stringObject The string to check. * @param characters The characters to check. */ static startsWith(stringObject, characters) { if (StringExtensions.isNullOrWhitespace(stringObject)) return false; if (StringExtensions.isNullOrWhitespace(characters)) return false; return stringObject.substr(0, characters.length) === characters; } /** * Check whether a string ends with a certain character * @param stringObject The string to check. * @param characters The characters to check. */ static endsWith(stringObject, characters) { if (StringExtensions.isNullOrWhitespace(stringObject)) return false; if (StringExtensions.isNullOrWhitespace(characters)) return false; return stringObject.substring(stringObject.length - characters.length) === characters; } /** * Trim the given character at the start of the string * @param stringObject The string to trim. * @param characters (optional) The characters to trim. */ static trimStartCharacters(stringObject, characters) { /* istanbul ignore next */ if (StringExtensions.isNullOrWhitespace(stringObject)) throw new ReferenceError(`The parameter stringObject is required`); if (StringExtensions.isNullOrWhitespace(characters)) characters = ' '; return stringObject .replace(new RegExp(`^${characters}*`, 'gmi'), String()); } /** * Trim the given character at the end of the string * @param stringObject The string to trim. * @param characters (optional) The characters to trim. */ static trimEndCharacters(stringObject, characters) { /* istanbul ignore next */ if (StringExtensions.isNullOrWhitespace(stringObject)) throw new ReferenceError(`The parameter stringObject is required`); if (StringExtensions.isNullOrWhitespace(characters)) characters = ' '; return stringObject .replace(new RegExp(`${characters}*$`, 'gmi'), String()); } /** * Trim the given character at the start and end of the string * @param stringObject The string to trim. * @param characters (optional) The characters to trim. */ static trimCharacters(stringObject, characters) { /* istanbul ignore next */ if (StringExtensions.isNullOrWhitespace(stringObject)) throw new ReferenceError(`The parameter stringObject is required`); if (StringExtensions.isNullOrWhitespace(characters)) return stringObject.trim(); stringObject = StringExtensions.trimStartCharacters(stringObject, characters); stringObject = StringExtensions.trimEndCharacters(stringObject, characters); return stringObject; } } exports.StringExtensions = StringExtensions; exports.default = StringExtensions; /* istanbul ignore next */ /** * Apply extensions to the String interface */ // tslint:disable-next-line:no-unused-expression !function applyStringExtensions() { Object.prototype['isNullOrEmpty'] = () => StringExtensions.isNullOrEmpty(this); Object.prototype['isNullOrWhitespace'] = () => StringExtensions.isNullOrWhitespace(this); Object.prototype['startsWith'] = (startChar) => StringExtensions.startsWith(this, startChar); Object.prototype['endsWith'] = (character) => StringExtensions.endsWith(this, character); Object.prototype['trimStartCharacters'] = (character) => StringExtensions.trimStartCharacters(this, character); Object.prototype['trimEndCharacters'] = (character) => StringExtensions.trimEndCharacters(this, character); Object.prototype['trimCharacters'] = (character) => StringExtensions.trimCharacters(this, character); }(); /* istanbul ignore end */ //# sourceMappingURL=StringExtensions.js.map