@egodigital/egoose
Version:
Helper classes and functions for Node.js 10 or later.
145 lines • 4.94 kB
JavaScript
;
/**
* This file is part of the @egodigital/egoose distribution.
* Copyright (c) e.GO Digital GmbH, Aachen, Germany (https://www.e-go-digital.com/)
*
* @egodigital/egoose is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation, version 3.
*
* @egodigital/egoose is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
Object.defineProperty(exports, "__esModule", { value: true });
const _ = require("lodash");
const Enumerable = require("node-enumerable");
const index_1 = require("../index");
let knownFormatProviders;
/**
* Formats a string.
*
* @param {any} formatStr The value that represents the format string.
* @param {any[]} [args] The arguments for 'formatStr'.
*
* @return {string} The formated string.
*/
function format(formatStr, ...args) {
return formatArray(formatStr, args);
}
exports.format = format;
/**
* Formats a string.
*
* @param {any} formatStr The value that represents the format string.
* @param {Enumerable.Sequence<any>} [args] The arguments for 'formatStr'.
*
* @return {string} The formated string.
*/
function formatArray(formatStr, args) {
formatStr = index_1.toStringSafe(formatStr);
if (!_.isArrayLike(args)) {
args = Enumerable.from(args)
.toArray();
}
// apply arguments in
// placeholders
return formatStr.replace(/{(\d+)(\:)?([^}]*)}/g, (match, index, separator, providerName) => {
index = parseInt(index_1.toStringSafe(index));
let resultValue = args[index];
if (':' === separator) {
// collect "format providers"
const FORMAT_PROVIDERS = index_1.toStringSafe(providerName).split(',')
.map(x => index_1.normalizeString(x))
.filter(x => '' !== x);
// transform argument by
// format providers
FORMAT_PROVIDERS.forEach(fp => {
let provider = knownFormatProviders[fp];
if (_.isNil(provider)) {
// try default
provider = knownFormatProviders[''];
}
if (provider) {
resultValue = provider(resultValue, match);
}
});
}
if (_.isUndefined(resultValue)) {
return match;
}
return index_1.toStringSafe(resultValue);
});
}
exports.formatArray = formatArray;
/**
* Returns a (new) list of default string format providers, grouped by name.
*
* @return {StringFormatProviderList} The new list.
*/
function getDefaultStringFormatProviders() {
return {
'ending_space': (val) => {
val = index_1.toStringSafe(val);
if ('' !== val) {
val = val + ' ';
}
return val;
},
'leading_space': (val) => {
val = index_1.toStringSafe(val);
if ('' !== val) {
val = ' ' + val;
}
return val;
},
'lower': (val) => {
return index_1.toStringSafe(val).toLowerCase();
},
'surround': (val) => {
val = index_1.toStringSafe(val);
if ('' !== val) {
val = "'" + index_1.toStringSafe(val) + "'";
}
return val;
},
'trim': (val) => {
return index_1.toStringSafe(val).trim();
},
'upper': (val) => {
return index_1.toStringSafe(val).toUpperCase();
},
};
}
exports.getDefaultStringFormatProviders = getDefaultStringFormatProviders;
/**
* Registers a new list of global string format providers.
*
* @param {StringFormatProviderList} [providers] The new list.
* @param {boolean} [withDefaults] Also register default providers first. Default: (true)
*/
function registerStringFormatProviders(providers, withDefaults) {
withDefaults = index_1.toBooleanSafe(withDefaults, true);
const NEW_LIST = {};
const APPEND_TO_LIST = (list) => {
if (_.isNil(list)) {
return;
}
for (const PROVIDER_NAME in list) {
NEW_LIST[index_1.normalizeString(PROVIDER_NAME)] = list[PROVIDER_NAME];
}
};
if (withDefaults) {
APPEND_TO_LIST(getDefaultStringFormatProviders());
}
APPEND_TO_LIST(providers);
knownFormatProviders = NEW_LIST;
}
exports.registerStringFormatProviders = registerStringFormatProviders;
// register defaults
registerStringFormatProviders();
//# sourceMappingURL=index.js.map