42 lines
1.4 KiB
JavaScript
42 lines
1.4 KiB
JavaScript
|
"use strict";
|
||
|
Object.defineProperty(exports, "__esModule", { value: true });
|
||
|
exports.format = void 0;
|
||
|
// Regex that finds { and } so they can be removed on a lookup for string format
|
||
|
var FORMAT_ARGS_REGEX = /[\{\}]/g;
|
||
|
// Regex that finds {#} so it can be replaced by the arguments in string format
|
||
|
var FORMAT_REGEX = /\{\d+\}/g;
|
||
|
/**
|
||
|
* String format method, used for scenarios where at runtime you
|
||
|
* need to evaluate a formatted string given a tokenized string. This
|
||
|
* usually only is needed in localization scenarios.
|
||
|
|
||
|
* @example
|
||
|
* ```tsx
|
||
|
* "I love {0} every {1}".format("CXP")
|
||
|
* ```
|
||
|
* will result in a Debug Exception.
|
||
|
*
|
||
|
* @public
|
||
|
*/
|
||
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||
|
function format(s) {
|
||
|
var values = [];
|
||
|
for (var _i = 1; _i < arguments.length; _i++) {
|
||
|
values[_i - 1] = arguments[_i];
|
||
|
}
|
||
|
var args = values;
|
||
|
// Callback match function
|
||
|
function replaceFunc(match) {
|
||
|
// looks up in the args
|
||
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||
|
var replacement = args[match.replace(FORMAT_ARGS_REGEX, '')];
|
||
|
// catches undefined in nondebug and null in debug and nondebug
|
||
|
if (replacement === null || replacement === undefined) {
|
||
|
replacement = '';
|
||
|
}
|
||
|
return replacement;
|
||
|
}
|
||
|
return s.replace(FORMAT_REGEX, replaceFunc);
|
||
|
}
|
||
|
exports.format = format;
|
||
|
//# sourceMappingURL=string.js.map
|