Skip to content

Commit 9656d61

Browse files
docs: ✏️ JSDoc
1 parent 0d52cad commit 9656d61

File tree

2 files changed

+37
-30
lines changed

2 files changed

+37
-30
lines changed

packages/interpolate/README.md

Lines changed: 0 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -12,36 +12,6 @@ Super simple microtemplating.
1212
yarn add @pacote/interpolate
1313
```
1414

15-
## Usage
16-
17-
```typescript
18-
import { interpolate } from '@pacote/interpolate'
19-
20-
const render = interpolate('Hello, {{ name }}!')
21-
22-
render({ name: 'world' }) // => "Hello, world!"
23-
```
24-
25-
### `interpolate(template: string, pattern?: RegExp) => (data?: { [key: string]: string } | string[]) => string`
26-
27-
`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.
28-
29-
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:
30-
31-
```typescript
32-
const render = interpolate('Hello, {{0}} and {{1}}!')
33-
34-
render(['Alice', 'Bob']) // => "Hello, Alice and Bob!"
35-
```
36-
37-
`interpolate()` optionally receives a pattern expression in order to customise the placeholder delimiters. This pattern _must_ have a single capture group. For example:
38-
39-
```typescript
40-
const render = interpolate('Hello, %{name}!', /%{([\s\S]+?)}/)
41-
42-
render({ name: 'world' }) // => "Hello, world!"
43-
```
44-
4515
## License
4616

4717
MIT © [Luís Rodrigues](https://goblindegook.com).

packages/interpolate/src/index.ts

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,42 @@
11
type ReplaceMap<T> = { [placeholder: string]: T } | T[]
22

3+
/**
4+
* Creates a renderer that substitutes placeholders in a template with values
5+
* provided at call time.
6+
*
7+
* The returned function accepts either an object keyed by placeholder names or
8+
* an array of values, in which case placeholders are numeric indices. Only raw
9+
* substitution is performed—no escaping, looping, or conditional logic.
10+
*
11+
* @param template Template containing placeholders to replace.
12+
* @param pattern Pattern describing the placeholder delimiters. Defaults to
13+
* `/{{([\s\S]+?)}}/`. May be a string or `RegExp` and must
14+
* contain exactly one capture group for the placeholder key.
15+
*
16+
* @returns Function that receives replacement data and yields the interpolated
17+
* string.
18+
*
19+
* @example
20+
* ```typescript
21+
* const render = interpolate('Hello, {{ name }}!')
22+
*
23+
* render({ name: 'world' }) // => 'Hello, world!'
24+
* ```
25+
*
26+
* @example
27+
* ```typescript
28+
* const render = interpolate('Hello, {{0}} and {{1}}!')
29+
*
30+
* render(['Alice', 'Bob']) // => 'Hello, Alice and Bob!'
31+
* ```
32+
*
33+
* @example
34+
* ```typescript
35+
* const render = interpolate('Hello, %{name}!', /%{([\s\S]+?)}/)
36+
*
37+
* render({ name: 'world' }) // => 'Hello, world!'
38+
* ```
39+
*/
340
export function interpolate(
441
template: string,
542
pattern: RegExp | string = /{{([\s\S]+?)}}/,

0 commit comments

Comments
 (0)