2016-11-03 03:37:26 +08:00
|
|
|
export default function(str, formats) {
|
|
|
|
let cachedFormats = formats;
|
|
|
|
|
2020-02-21 22:56:49 +08:00
|
|
|
if (!_.isArray(cachedFormats) || arguments.length > 2) {
|
2016-11-03 03:37:26 +08:00
|
|
|
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 23:03:24 +08:00
|
|
|
let idx = 0; // the current index for non-numerical replacements
|
2016-11-03 03:37:26 +08:00
|
|
|
return str.replace(/%@([0-9]+)?/g, function(s, argIndex) {
|
2018-06-15 23:03:24 +08:00
|
|
|
argIndex = argIndex ? parseInt(argIndex, 10) - 1 : idx++;
|
2016-11-03 03:37:26 +08:00
|
|
|
s = cachedFormats[argIndex];
|
2019-01-10 18:06:01 +08:00
|
|
|
return typeof s === "string"
|
|
|
|
? s
|
|
|
|
: s === null
|
2019-01-18 02:05:39 +08:00
|
|
|
? "(null)"
|
|
|
|
: s === undefined
|
|
|
|
? ""
|
|
|
|
: "" + s;
|
2016-11-03 03:37:26 +08:00
|
|
|
});
|
|
|
|
}
|