UNPKG

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