UNPKG

4.94 kBJavaScriptView Raw
1"use strict";
2/**
3 * This file is part of the @egodigital/egoose distribution.
4 * Copyright (c) e.GO Digital GmbH, Aachen, Germany (https://www.e-go-digital.com/)
5 *
6 * @egodigital/egoose is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU Lesser General Public License as
8 * published by the Free Software Foundation, version 3.
9 *
10 * @egodigital/egoose is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18Object.defineProperty(exports, "__esModule", { value: true });
19const _ = require("lodash");
20const Enumerable = require("node-enumerable");
21const index_1 = require("../index");
22let knownFormatProviders;
23/**
24 * Formats a string.
25 *
26 * @param {any} formatStr The value that represents the format string.
27 * @param {any[]} [args] The arguments for 'formatStr'.
28 *
29 * @return {string} The formated string.
30 */
31function format(formatStr, ...args) {
32 return formatArray(formatStr, args);
33}
34exports.format = format;
35/**
36 * Formats a string.
37 *
38 * @param {any} formatStr The value that represents the format string.
39 * @param {Enumerable.Sequence<any>} [args] The arguments for 'formatStr'.
40 *
41 * @return {string} The formated string.
42 */
43function formatArray(formatStr, args) {
44 formatStr = index_1.toStringSafe(formatStr);
45 if (!_.isArrayLike(args)) {
46 args = Enumerable.from(args)
47 .toArray();
48 }
49 // apply arguments in
50 // placeholders
51 return formatStr.replace(/{(\d+)(\:)?([^}]*)}/g, (match, index, separator, providerName) => {
52 index = parseInt(index_1.toStringSafe(index));
53 let resultValue = args[index];
54 if (':' === separator) {
55 // collect "format providers"
56 const FORMAT_PROVIDERS = index_1.toStringSafe(providerName).split(',')
57 .map(x => index_1.normalizeString(x))
58 .filter(x => '' !== x);
59 // transform argument by
60 // format providers
61 FORMAT_PROVIDERS.forEach(fp => {
62 let provider = knownFormatProviders[fp];
63 if (_.isNil(provider)) {
64 // try default
65 provider = knownFormatProviders[''];
66 }
67 if (provider) {
68 resultValue = provider(resultValue, match);
69 }
70 });
71 }
72 if (_.isUndefined(resultValue)) {
73 return match;
74 }
75 return index_1.toStringSafe(resultValue);
76 });
77}
78exports.formatArray = formatArray;
79/**
80 * Returns a (new) list of default string format providers, grouped by name.
81 *
82 * @return {StringFormatProviderList} The new list.
83 */
84function getDefaultStringFormatProviders() {
85 return {
86 'ending_space': (val) => {
87 val = index_1.toStringSafe(val);
88 if ('' !== val) {
89 val = val + ' ';
90 }
91 return val;
92 },
93 'leading_space': (val) => {
94 val = index_1.toStringSafe(val);
95 if ('' !== val) {
96 val = ' ' + val;
97 }
98 return val;
99 },
100 'lower': (val) => {
101 return index_1.toStringSafe(val).toLowerCase();
102 },
103 'surround': (val) => {
104 val = index_1.toStringSafe(val);
105 if ('' !== val) {
106 val = "'" + index_1.toStringSafe(val) + "'";
107 }
108 return val;
109 },
110 'trim': (val) => {
111 return index_1.toStringSafe(val).trim();
112 },
113 'upper': (val) => {
114 return index_1.toStringSafe(val).toUpperCase();
115 },
116 };
117}
118exports.getDefaultStringFormatProviders = getDefaultStringFormatProviders;
119/**
120 * Registers a new list of global string format providers.
121 *
122 * @param {StringFormatProviderList} [providers] The new list.
123 * @param {boolean} [withDefaults] Also register default providers first. Default: (true)
124 */
125function registerStringFormatProviders(providers, withDefaults) {
126 withDefaults = index_1.toBooleanSafe(withDefaults, true);
127 const NEW_LIST = {};
128 const APPEND_TO_LIST = (list) => {
129 if (_.isNil(list)) {
130 return;
131 }
132 for (const PROVIDER_NAME in list) {
133 NEW_LIST[index_1.normalizeString(PROVIDER_NAME)] = list[PROVIDER_NAME];
134 }
135 };
136 if (withDefaults) {
137 APPEND_TO_LIST(getDefaultStringFormatProviders());
138 }
139 APPEND_TO_LIST(providers);
140 knownFormatProviders = NEW_LIST;
141}
142exports.registerStringFormatProviders = registerStringFormatProviders;
143// register defaults
144registerStringFormatProviders();
145//# sourceMappingURL=index.js.map
\No newline at end of file