@uifabric/utilities
Version:
Fluent UI React utilities for building components.
38 lines (37 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) {
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);
}
//# sourceMappingURL=string.js.map