Skip to content

mapTemplate

Replace template placeholders with values.

Usage

js
import { mapTemplate } from 'js-cool'

Signature

typescript
function mapTemplate(
  template: string,
  data: Record<string, any> | ((key: string) => any)
): string

Parameters

ParameterTypeDescription
templatestringTemplate with placeholders
dataRecord<string, any> | FunctionValues object or function

Returns

string - The template with placeholders replaced.

Examples

js
// Using ${} syntax
mapTemplate('My name is ${name}', { name: 'John' })
// 'My name is John'

// Using {{}} syntax
mapTemplate('Hello {{name}}', { name: 'World' })
// 'Hello World'

// Using function
mapTemplate('Value: ${key}', (key) => key.toUpperCase())
// 'Value: KEY'

Released under the MIT License.