discourse/app/assets/javascripts/ember-addons/fmt.js.es6
Jarek Radosz 6ba326a9f4
DEV: Deprecate ember module imports (#9011)
Removes remaining `ember` module imports.
2020-02-21 15:56:49 +01:00

26 lines
702 B
JavaScript

export default function(str, formats) {
let cachedFormats = formats;
if (!_.isArray(cachedFormats) || arguments.length > 2) {
cachedFormats = new Array(arguments.length - 1);
for (let i = 1, l = arguments.length; i < l; i++) {
cachedFormats[i - 1] = arguments[i];
}
}
// first, replace any ORDERED replacements.
let idx = 0; // the current index for non-numerical replacements
return str.replace(/%@([0-9]+)?/g, function(s, argIndex) {
argIndex = argIndex ? parseInt(argIndex, 10) - 1 : idx++;
s = cachedFormats[argIndex];
return typeof s === "string"
? s
: s === null
? "(null)"
: s === undefined
? ""
: "" + s;
});
}