Is your feature request related to a problem? Please describe.
Sometimes while logging, you want to use logger.debug, but you end up formatting a message even if the message won't be logged. Would be nice to make the formatting of the message content itself lazy.
Describe the solution you'd like
Use something like:
function template(strings, ...keys) {
return (...values) => {
const dict = values[values.length - 1] || {};
const result = [strings[0]];
keys.forEach((key, i) => {
const value = Number.isInteger(key) ? values[key] : dict[key];
result.push(value, strings[i + 1]);
});
return result.join("");
};
}
const t1Closure = template`${0}${1}${0}!`;
// const t1Closure = template(["","","","!"],0,1,0);
t1Closure("Y", "A"); // "YAY!"
const t2Closure = template`${0} ${"foo"}!`;
// const t2Closure = template([""," ","!"],0,"foo");
t2Closure("Hello", { foo: "World" }); // "Hello World!"
const t3Closure = template`I'm ${"name"}. I'm almost ${"age"} years old.`;
// const t3Closure = template(["I'm ", ". I'm almost ", " years old."], "name", "age");
t3Closure("foo", { name: "MDN", age: 30 }); // "I'm MDN. I'm almost 30 years old."
t3Closure({ name: "MDN", age: 30 }); // "I'm MDN. I'm almost 30 years old."
Describe alternatives you've considered
Could also do logger.debug(() => ''), just a plain old function. But the templating idea seems pretty useful, at the end of the day it does in fact return a function too. So if you're going to template something, returning a function to be called might be faster if there are lots of logger.debug calls going around.
Additional context
Should benchmark if this really improves performance, but it also provides some usability... we might be more free to sprinkle logger.debug calls if they don't slow things down (except a function call).
Is your feature request related to a problem? Please describe.
Sometimes while logging, you want to use
logger.debug, but you end up formatting a message even if the message won't be logged. Would be nice to make the formatting of the message content itself lazy.Describe the solution you'd like
Use something like:
Describe alternatives you've considered
Could also do
logger.debug(() => ''), just a plain old function. But the templating idea seems pretty useful, at the end of the day it does in fact return a function too. So if you're going to template something, returning a function to be called might be faster if there are lots oflogger.debugcalls going around.Additional context
Should benchmark if this really improves performance, but it also provides some usability... we might be more free to sprinkle
logger.debugcalls if they don't slow things down (except a function call).