@itsmworkbench/utils
Version:
The usual utility functions
94 lines (93 loc) • 3.65 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
exports.withoutFirstSegment = exports.simpleTemplate = exports.fullExtension = exports.splitAndCapitalize = exports.extractSqlString = exports.escapeSqlParameters = exports.escapeForSql = exports.toCamelCase = exports.uppercaseFirstLetter = exports.lowercaseFirstLetter = void 0;
function lowercaseFirstLetter(str) {
return str.charAt(0).toLowerCase() + str.slice(1);
}
exports.lowercaseFirstLetter = lowercaseFirstLetter;
function uppercaseFirstLetter(str) {
return str.charAt(0).toUpperCase() + str.slice(1);
}
exports.uppercaseFirstLetter = uppercaseFirstLetter;
function toCamelCase(str) {
return str
// First, handle the start of the string separately if it's uppercase
.replace(/^([A-Z])/, (firstChar) => firstChar.toLowerCase())
// Then, transform the rest of the string
.replace(/[^a-zA-Z0-9]+(.)/g, (match, chr) => chr.toUpperCase());
}
exports.toCamelCase = toCamelCase;
function escapeForSql(str) {
// Replace single quotes with two single quotes for SQL escaping
return str.replace(/'/g, "''");
}
exports.escapeForSql = escapeForSql;
function escapeSqlParameters(sql, lookupMap) {
// Regular expression to find all instances of `:parameterName`
const parameterRegex = /:([\w.]+)/g;
if (lookupMap === undefined)
throw new Error('lookupMap is undefined');
// Replace function to handle each match
const replacer = (match, paramName) => {
// Check if the parameter needs quotes
if (lookupMap[paramName]?.escape) {
// Add quotes around the parameter placeholder
return `'${match}'`;
}
else {
// Leave the parameter placeholder as is
return match;
}
};
// Replace each parameter in the SQL string according to the lookup map
return sql?.replace(parameterRegex, replacer);
}
exports.escapeSqlParameters = escapeSqlParameters;
function extractSqlString(query) {
if (!query)
return [];
// Regular expression to match parameters (including those with dots)
const parameterRegex = /:([a-zA-Z0-9._]+)/g;
let match;
const parameters = [];
// Use a loop to find all matches in the query string
while ((match = parameterRegex.exec(query)) !== null) {
// Add the matched parameter name to the list, excluding the leading colon
parameters.push(match[1]);
}
return parameters;
}
exports.extractSqlString = extractSqlString;
function splitAndCapitalize(input) {
if (input === undefined)
return '';
const withSpaces = input.replace(/([A-Z])/g, ' $1').trim();
const result = withSpaces.charAt(0).toUpperCase() + withSpaces.slice(1);
return result;
}
exports.splitAndCapitalize = splitAndCapitalize;
function fullExtension(path) {
const index = path.indexOf('.');
return index === -1 ? '' : path.slice(index + 1);
}
exports.fullExtension = fullExtension;
function simpleTemplate(template, data) {
if (template === undefined)
return undefined;
if (data === undefined)
return template;
return template.replace(/\{(\w+)\}/g, (match, key) => {
if (key in data) {
return String(data[key]);
}
return match; // Return the original match if no corresponding key is found
});
}
exports.simpleTemplate = simpleTemplate;
function withoutFirstSegment(path) {
if (path === undefined)
return '';
const index = path.indexOf('/');
return index === -1 ? '' : path.slice(index + 1);
}
exports.withoutFirstSegment = withoutFirstSegment;
;