UNPKG

ravendb

Version:
118 lines 4.15 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.StringUtil = void 0; const TypeUtil_js_1 = require("./TypeUtil.js"); const index_js_1 = require("../Exceptions/index.js"); class StringUtil { static letterRe = /^\p{L}/u; static digitRe = /\d/; static leftPad(s, length, char) { const inputLength = s ? s.length : 0; if (inputLength === length) { // no need for padding 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_js_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[Number.parseInt(placeholder, 10)]; return (TypeUtil_js_1.TypeUtil.isNullOrUndefined(value) ? "" : value).toString(); }); } static validateDBName(dbName) { if (TypeUtil_js_1.TypeUtil.isNullOrUndefined(dbName) || !dbName) { (0, index_js_1.throwError)("InvalidOperationException", "Empty name is not valid"); } if (!/^[A-Za-z0-9_\-.]+$/.test(dbName)) { (0, index_js_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) && !["_", "@"].includes(c)) { escape = true; break; } continue; } if (!this.isLetterOrDigit(c) && !["_", "@", ".", "[", "]"].includes(c)) { escape = true; break; } } if (escape) { return `'${field}'`; } return field; } static capitalize(s) { return s.charAt(0).toUpperCase() + s.slice(1); } static uncapitalize(s) { return s.charAt(0).toLowerCase() + s.slice(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 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, String.raw `\'`); 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; //# sourceMappingURL=StringUtil.js.map