@fluentui/react-northstar
Version:
A themable React component library.
41 lines (36 loc) • 1.31 kB
JavaScript
// 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
export function format(s) {
for (var _len = arguments.length, values = new Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) {
values[_key - 1] = arguments[_key];
}
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);
}
//# sourceMappingURL=format.js.map