UNPKG

@configurator/ravendb

Version:
118 lines 4.27 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.StringUtil = void 0; const XRegExpAll = require("xregexp"); const XRegExp = XRegExpAll.default || XRegExpAll; const TypeUtil_1 = require("./TypeUtil"); const Exceptions_1 = require("../Exceptions"); const changeCase = require("change-case"); class StringUtil { static leftPad(s, length, char) { const inputLength = s ? s.length : 0; if (inputLength === length) { return s; } return char.repeat(length - inputLength) + s; } static startsWithIgnoreCase(s, prefix) { return s.toLowerCase().startsWith(prefix.toLowerCase()); } static toWebSocketPath(url) { return url.replace('https://', 'wss://').replace('http://', 'ws://'); } static format(s, vars, ...varsArray) { if (TypeUtil_1.TypeUtil.isObject(vars)) { return s.replace(/\{([\w\d-]+)\}/g, (match, placeholder) => (placeholder in vars ? vars[placeholder] : '').toString()); } const inputVars = [vars].concat(varsArray); return s.replace(/\{([\d]+)\}/g, (match, placeholder) => { const value = inputVars[parseInt(placeholder, 10)]; return (TypeUtil_1.TypeUtil.isNullOrUndefined(value) ? '' : value).toString(); }); } static validateDBName(dbName) { if (TypeUtil_1.TypeUtil.isNullOrUndefined(dbName) || !dbName) { (0, Exceptions_1.throwError)('InvalidOperationException', 'Empty name is not valid'); } if (!/^[A-Za-z0-9_\-.]+$/.test(dbName)) { (0, Exceptions_1.throwError)('InvalidOperationException', `Database name can only contain only A-Z, a-z, "_", "." or "-"`); } } static escapeIfNecessary(field) { if (!field) { return field; } let escape = false; for (let i = 0; i < field.length; i++) { const c = field[i]; if (i === 0) { if (!this.isLetter(c) && ['_', '@'].indexOf(c) === -1) { escape = true; break; } continue; } if (!this.isLetterOrDigit(c) && ['_', '@', '.', '[', ']'].indexOf(c) === -1) { escape = true; break; } } if (escape) { return `'${field}'`; } return field; } static capitalize(s) { return s.charAt(0).toUpperCase() + s.substring(1); } static uncapitalize(s) { return s.charAt(0).toLowerCase() + s.substring(1); } static isCharacter(character) { return character && 1 === character.length; } static isDigit(character) { return this.isCharacter(character) && this.digitRe.test(character); } static isLetter(character) { return this.isCharacter(character) && this.letterRe.test(character); } static isLetterOrDigit(character) { return this.isLetter(character) || this.isDigit(character); } static isNullOrEmpty(s) { return !(s || '').length; } static isNullOrWhitespace(s) { return !(s || '').trim().length; } static changeCase(transformName, s) { return changeCase[transformName](s); } static equalsIgnoreCase(s1, s2) { const s1Type = typeof s1; const s2Type = typeof s2; return (s1Type === s2Type && s1Type !== 'undefined' && s2Type !== 'undefined' && s1.toLowerCase() === s2.toLowerCase()); } static escapeString(builder, value) { if (StringUtil.isNullOrWhitespace(value)) { return; } StringUtil._escapeStringInternal(builder, value); } static _escapeStringInternal(builder, value) { let escaped = JSON.stringify(value); escaped = escaped.replace(/'/g, "\\'"); builder.append(escaped.substring(1, escaped.length - 1)); } static splice(input, start, delCount, newSubStr) { return input.slice(0, start) + newSubStr + input.slice(start + Math.abs(delCount)); } } exports.StringUtil = StringUtil; StringUtil.letterRe = XRegExp('^\\p{L}$'); StringUtil.digitRe = /\d/; //# sourceMappingURL=StringUtil.js.map