@hv-kit/hexpress
Version:
facilitates typescript backend development with express
151 lines • 6.59 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.getKeyword = exports.getAlphanumString = exports.StringReplace = exports.SearchForFind = exports.ArrayToRegExp = exports.Capitalize = exports.Lower = exports.Upper = exports.UcFirst = exports.RandomF = exports.RandomIdentifier = void 0;
const hexpress_config_json_1 = __importDefault(require("../../../../hexpress.config.json"));
const config_1 = require("./config");
function RandomIdentifier(size = 10, type = 'alphanumeric', fixedSize = true, formatting = (data) => data) {
size = (typeof size === 'number'
&& size > 1) ? size : 5;
const endSize = size - 1;
return `${RandomF(1, 'alphabetical', true)}${RandomF(endSize, type, fixedSize, formatting)}`;
}
exports.RandomIdentifier = RandomIdentifier;
function RandomF(size = 10, type = 'alphanumeric', fixedSize = true, formatting = (data) => data) {
const types = ['alphanumeric', 'alphanumeric_s', 'alphabetical', 'alphabetical_s', 'numeric'];
const valuesTypesNumeric = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
const valuesTypesAlphabetical = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'];
const valuesTypesAlphabeticalS = valuesTypesAlphabetical.map((value) => value.toUpperCase()).concat(valuesTypesAlphabetical);
const valuesTypesAlphanumeric = valuesTypesNumeric.map((value) => String(value)).concat(valuesTypesAlphabetical);
const valuesTypesAlphanumericS = valuesTypesNumeric.map((value) => String(value)).concat(valuesTypesAlphabeticalS);
fixedSize = (typeof fixedSize === 'boolean') ? fixedSize : true;
size = (typeof size === 'number' &&
size > 0) ? size : 5;
const initialSize = size;
size = (!!fixedSize) ? size : (Math.floor(Math.random() * (size - 2 + 1)) + 2);
const valuesTypes = {
numeric: valuesTypesNumeric,
alphabetical: valuesTypesAlphabetical,
alphanumeric: valuesTypesAlphanumeric,
alphabetical_s: valuesTypesAlphabeticalS,
alphanumeric_s: valuesTypesAlphanumericS,
};
const min = 1;
const max = valuesTypes[type].length;
let result = [];
for (let index = 0; index < size; index++) {
result.push(valuesTypes[type][Math.floor(Math.random() * (max - min + 1)) + min]);
}
return formatting(result.join('').substring(0, initialSize));
}
exports.RandomF = RandomF;
function UcFirst(value) {
if (!(typeof (value) === 'string'
&& value.length > 0))
return value;
value = String(value);
return value.charAt(0).toUpperCase() + value.slice(1).toLowerCase();
}
exports.UcFirst = UcFirst;
function Upper(value) {
if (!(typeof (value) === 'string'
&& value.length > 0))
return value;
value = String(value);
return value.toUpperCase();
}
exports.Upper = Upper;
function Lower(value) {
if (!(typeof (value) === 'string'
&& value.length > 0))
return value;
value = String(value);
return value.toLowerCase();
}
exports.Lower = Lower;
function Capitalize(value) {
const sep = ' ';
if (!(typeof (value) === 'string'
&& value.length > 0))
return value;
const array_res = String(value).split(sep).map((val) => val.charAt(0).toUpperCase() + val.toLowerCase().slice(1));
const res = array_res.join(sep);
return res;
}
exports.Capitalize = Capitalize;
function ArrayToRegExp(values) {
return new RegExp(`(${values.join('|')})`);
}
exports.ArrayToRegExp = ArrayToRegExp;
function SearchForFind(value) {
return (value
&& typeof (value) === 'string'
&& value.length > 0) ? Lower(value).replace(config_1.regExpForAlphanumeric, '').replace(/\s/gi, '+').split('+').filter((value) => (typeof (value) === 'string'
&& value.length > 0)) : [];
}
exports.SearchForFind = SearchForFind;
function StringReplace(content, params) {
let result = content;
if (typeof (params) === 'object'
&& typeof (result) === 'string') {
Object.keys(params).forEach((key, index) => {
const value = (typeof (params[key]) === 'object') ? JSON.stringify(params[key]) : params[key];
const wordSearched = `{{(${key})}}`;
result = result.replace(new RegExp(wordSearched, 'gi'), value);
if (hexpress_config_json_1.default.debug) {
console.log('----> string.ts > StringReplace - content:: ', content);
console.log('----> string.ts > StringReplace - params[', index, ']:: ', params[index]);
console.log('----> string.ts > StringReplace - wordSearched:: ', wordSearched);
console.log('----> string.ts > StringReplace - value:: ', value);
console.log('----> string.ts > StringReplace - result:: ', result);
}
});
}
return result;
}
exports.StringReplace = StringReplace;
function getAlphanumString(val, word = '_') {
word = (typeof word === 'string' &&
word.length > 0) ? word : '_';
val = (typeof val === 'string' &&
val.length > 0) ? val : String(undefined);
return String(val).replace(config_1.regExpForAlphanumeric2, word).split(word).filter((value) => value.length > 0).join(word);
}
exports.getAlphanumString = getAlphanumString;
function getKeyword(word) {
let result = word;
if (typeof word === 'string' &&
word.length > 0) {
const reg = config_1.regExpForAlphanumeric2;
let res = word.replace(reg, '+').split('+').filter((value) => value.length > 0);
res = res.map((value) => {
let valueRes = value;
if (!!parseFloat(value)) {
valueRes = parseFloat(value);
}
else if (['true', 't', '1', 'false', 'f', '0'].includes(value.toLowerCase())) {
valueRes = (['true', 't', '1'].includes(value.toLowerCase())) ? true : false;
}
return valueRes;
});
result = res;
}
return result;
}
exports.getKeyword = getKeyword;
exports.default = {
RandomIdentifier,
RandomF,
UcFirst,
Upper,
Lower,
Capitalize,
ArrayToRegExp,
SearchForFind,
StringReplace,
getAlphanumString,
getKeyword,
};
//# sourceMappingURL=string.js.map