UNPKG

1.63 kBJavaScriptView Raw
1define(["require", "exports"], function (require, exports) {
2 "use strict";
3 Object.defineProperty(exports, "__esModule", { value: true });
4 // Regex that finds { and } so they can be removed on a lookup for string format
5 var FORMAT_ARGS_REGEX = /[\{\}]/g;
6 // Regex that finds {#} so it can be replaced by the arguments in string format
7 var FORMAT_REGEX = /\{\d+\}/g;
8 /**
9 * String format method, used for scenarios where at runtime you
10 * need to evaluate a formatted string given a tokenized string. This
11 * usually only is needed in localization scenarios.
12
13 * @example
14 * ```tsx
15 * "I love {0} every {1}".format("CXP")
16 * ```
17 * will result in a Debug Exception.
18 *
19 * @public
20 */
21 // eslint-disable-next-line @typescript-eslint/no-explicit-any
22 function format(s) {
23 var values = [];
24 for (var _i = 1; _i < arguments.length; _i++) {
25 values[_i - 1] = arguments[_i];
26 }
27 var args = values;
28 // Callback match function
29 function replaceFunc(match) {
30 // looks up in the args
31 // eslint-disable-next-line @typescript-eslint/no-explicit-any
32 var replacement = args[match.replace(FORMAT_ARGS_REGEX, '')];
33 // catches undefined in nondebug and null in debug and nondebug
34 if (replacement === null || replacement === undefined) {
35 replacement = '';
36 }
37 return replacement;
38 }
39 return s.replace(FORMAT_REGEX, replaceFunc);
40 }
41 exports.format = format;
42});
43//# sourceMappingURL=string.js.map
\No newline at end of file