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): stringParameters
| Name | Type | Description |
|---|---|---|
str | string | String containing placeholders in the format \{\{key\}\}. |
data | Record<PropertyKey, any> | Data dictionary for interpolation. |
pattern | RegExp (optional) | Custom pattern for placeholders (default: /\{\{(.*?)\}\}/g). |
Return
| Type | Description |
|---|---|
string | Returns 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.
