Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Lazy Templating #26

Open
CMCDragonkai opened this issue Dec 28, 2022 · 1 comment
Open

Lazy Templating #26

CMCDragonkai opened this issue Dec 28, 2022 · 1 comment
Labels
enhancement New feature or request r&d:polykey:supporting activity Supporting core activity

Comments

@CMCDragonkai
Copy link
Member

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).

@CMCDragonkai CMCDragonkai added the enhancement New feature or request label Dec 28, 2022
@CMCDragonkai CMCDragonkai added the r&d:polykey:supporting activity Supporting core activity label Jul 9, 2023
@linear linear bot closed this as not planned Won't fix, can't repro, duplicate, stale Nov 18, 2024
@tegefaulkes
Copy link
Contributor

Linear marked this as stale so I'm reopening.

@tegefaulkes tegefaulkes reopened this Nov 19, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request r&d:polykey:supporting activity Supporting core activity
Development

No branches or pull requests

2 participants