discourse/app/assets/javascripts/ember-addons/fmt.js.es6

25 lines
731 B
Plaintext
Raw Normal View History

2018-06-15 11:03:24 -04:00
import Ember from "ember";
2016-11-02 15:37:26 -04:00
const inspect = Ember.inspect;
const isArray = Ember.isArray;
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.
2018-06-15 11:03:24 -04:00
let idx = 0; // the current index for non-numerical replacements
2016-11-02 15:37:26 -04:00
return str.replace(/%@([0-9]+)?/g, function(s, argIndex) {
2018-06-15 11:03:24 -04:00
argIndex = argIndex ? parseInt(argIndex, 10) - 1 : idx++;
2016-11-02 15:37:26 -04:00
s = cachedFormats[argIndex];
2018-06-15 11:03:24 -04:00
return s === null ? "(null)" : s === undefined ? "" : inspect(s);
2016-11-02 15:37:26 -04:00
});
}