@tsdotnet/text-utility
Version:
A set of commonly used functions for manipulating and formatting strings.
105 lines • 3.19 kB
JavaScript
;
/*!
* @author electricessence / https://github.com/electricessence/
* Licensing: MIT
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.EMPTY = void 0;
exports.getHashCode = getHashCode;
exports.repeat = repeat;
exports.fromChars = fromChars;
exports.escapeRegExp = escapeRegExp;
exports.trim = trim;
exports.supplant = supplant;
exports.format = format;
exports.startsWith = startsWith;
exports.endsWith = endsWith;
exports.EMPTY = '';
function getHashCode(source) {
let hash = 0 | 0;
if (source.length === 0)
return hash;
for (let i = 0, l = source.length; i < l; i++) {
const ch = source.charCodeAt(i);
hash = (hash << 5) - hash + ch;
hash |= 0;
}
return hash;
}
function repeat(source, count) {
if (source == null)
throw new Error('Cannot repeat null or undefined.');
if (typeof source !== 'string')
throw new TypeError('Expected \'source\' to be string.');
let result = exports.EMPTY;
if (source !== '' && !isNaN(count)) {
for (let i = 0; i < count; i++) {
result += source;
}
}
return result;
}
function fromChars(chOrChars, count = 1) {
if (chOrChars instanceof Array) {
let result = exports.EMPTY;
for (const char of chOrChars) {
result += String.fromCharCode(char);
}
return result;
}
else {
return repeat(String.fromCharCode(chOrChars), count);
}
}
function escapeRegExp(source) {
return source.replace(/[-[\]/{}()*+?.\\^$|]/g, '\\$&');
}
function trim(source, chars, ignoreCase) {
if (chars === exports.EMPTY)
return source;
if (chars) {
const escaped = escapeRegExp(chars instanceof Array ? chars.join() : chars);
return source.replace(new RegExp('^[' + escaped + ']+|[' + escaped + ']+$', 'g' + (ignoreCase ? 'i' : '')), exports.EMPTY);
}
return source.replace(/^\s+|\s+$/g, exports.EMPTY);
}
function supplant(source, params) {
const oIsArray = params instanceof Array;
return source.replace(/{([^{}]*)}/g, (a, b) => {
let n = b;
if (oIsArray) {
const i = parseInt(b, 10);
if (!isNaN(i))
n = i;
}
const r = params[n];
switch (typeof r) {
case 'string':
case 'number':
case 'boolean':
return r;
default:
return r && typeof r.toString == 'function' ? r.toString() : a;
}
});
}
function format(source, ...args) {
return supplant(source, args);
}
function canMatch(source, match) {
if (typeof source != 'string' || !match)
return false;
if (source === match)
return true;
if (match.length < source.length)
return null;
}
function startsWith(source, pattern) {
const m = canMatch(source, pattern);
return typeof m == 'boolean' ? m : source.indexOf(pattern) === 0;
}
function endsWith(source, pattern) {
const m = canMatch(source, pattern);
return typeof m == 'boolean' ? m : source.lastIndexOf(pattern) === source.length - pattern.length;
}
//# sourceMappingURL=Utility.js.map