Skip to content

interpolate

The interpolate function replaces placeholders in a string with values from a data dictionary.

Syntax

typescript
interpolate(str: string, data: Record<PropertyKey, any>, pattern?: RegExp): string

Parameters

NameTypeDescription
strstringString containing placeholders in the format \{\{key\}\}.
dataRecord<PropertyKey, any>Data dictionary for interpolation.
patternRegExp (optional)Custom pattern for placeholders (default: /\{\{(.*?)\}\}/g).

Return

TypeDescription
stringReturns a new string with placeholders replaced by corresponding values from data.

Example

typescript
const template = "Hello, \{\{name\}\}!";
const data = { name: "Maria" };
interpolate(template, data); // "Hello, Maria!"

Notes

  • Placeholders must be in the format \{\{key\}\} by default.
  • If the value does not exist in data, the placeholder is kept.
  • The backslashes () before the curly braces ( ) are escape characters used only to prevent conflicts with the documentation compiler. When using the function in practice, use simple braces without escaping.

References

Released under the MIT License.