Skip to content

Commit

Permalink
Merge pull request #31 from davidchambers/create
Browse files Browse the repository at this point in the history
readme: document format.create
  • Loading branch information
davidchambers authored Mar 26, 2018
2 parents ad5ec1b + 4df3f18 commit 5fd35a8
Showing 1 changed file with 21 additions and 0 deletions.
21 changes: 21 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,27 @@ const sheldon = {
// => "I've always wanted to go to a goth club. BAZINGA!"
```

### `format.create(transformers)`

This function takes an object mapping names to transformers and returns a
formatting function. A transformer is applied if its name appears, prefixed
with `!`, after a field name in a template string.

```javascript
const fmt = format.create({
escape: s => s.replace(/[&<>"'`]/g, c => '&#' + c.charCodeAt(0) + ';'),
upper: s => s.toUpperCase(),
})

fmt('Hello, {!upper}!', 'Alice')
// => 'Hello, ALICE!'

const restaurant = {name: 'Anchor & Hope', url: 'http://anchorandhopesf.com/'}

fmt('<a href="{url!escape}">{name!escape}</a>', restaurant)
// => '<a href="http://anchorandhopesf.com/">Anchor &#38; Hope</a>'
```

### `format.extend(prototype, transformers)`

This function takes a prototype (presumably `String.prototype`) and an object
Expand Down

0 comments on commit 5fd35a8

Please sign in to comment.