@pacote/interpolate

Super simple microtemplating.

Usage no npm install needed!

<script type="module">
  import pacoteInterpolate from 'https://cdn.skypack.dev/@pacote/interpolate';
</script>

README

@pacote/interpolate

version minified minified + gzip

Super simple microtemplating.

Installation

yarn add @pacote/interpolate

Usage

import { interpolate } from '@pacote/interpolate'

const render = interpolate('Hello, {{ name }}!')

render({ name: 'world' }) // => "Hello, world!"

interpolate(template: string, pattern?: RegExp) => (data?: { [key: string]: string } | string[]) => string

interpolate() takes a string template and returns a function that accepts an object or array containing the placeholders as properties and the strings to replace them with as values. Note that all the function does is substitute placeholders, it does not handle loops, conditionals or perform character escaping.

The returned function also accepts an array of values. In this case, the placeholder is expected to be the numerical index of the array value to use:

const render = interpolate('Hello, {{0}} and {{1}}!')

render(['Alice', 'Bob']) // => "Hello, Alice and Bob!"

interpolate() optionally receives a pattern expression in order to customise the placeholder delimiters. This pattern must have a single capture group. For example:

const render = interpolate('Hello, %{name}!', /%{([\s\S]+?)}/)

render({ name: 'world' }) // => "Hello, world!"

License

MIT © Luís Rodrigues.