Built-in String.{format,template}

improve string class add format and template method
provide built-in format string function

const userDefined = "foo${bar}" // the syntax is consistent with `template string`

const formatted = String.format(userDefined, { bar: "123" })
formatted // returns "foo123"

const tpl = String.template(userDefined)
tpl({ bar: "123" }) // returns "foo123"

Template literals already solve that use case:

const userDefined = ({bar}) => `foo${bar}`;

const formatted = userDefined({bar: "123"});

Sure, userDefined doesn't come from a string, and cannot be transported as data, but there are many templating libraries with different features available in userland already to solve this with either eval (very powerful, but also a security issue) or fixed pattern grammars (more secure, but more restricted, and very heterogenous). I do not believe there is one golden solution that would fit all requirements and be useful as a builtin.

1 Like