https://github.com/electerious/key-value-replace
Replace key/value pairs in a string.
https://github.com/electerious/key-value-replace
key-value replace string template
Last synced: 8 months ago
JSON representation
Replace key/value pairs in a string.
- Host: GitHub
- URL: https://github.com/electerious/key-value-replace
- Owner: electerious
- License: mit
- Created: 2016-12-09T22:44:45.000Z (about 9 years ago)
- Default Branch: master
- Last Pushed: 2022-10-01T13:59:14.000Z (over 3 years ago)
- Last Synced: 2025-04-24T01:07:14.142Z (9 months ago)
- Topics: key-value, replace, string, template
- Language: JavaScript
- Homepage:
- Size: 33.2 KB
- Stars: 6
- Watchers: 3
- Forks: 2
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
# key-value-replace
 [](https://coveralls.io/github/electerious/key-value-replace?branch=master)
Replace key/value pairs in a string.
## Install
```
npm install key-value-replace
```
## Usage
```js
const replace = require('key-value-replace')
replace('hello {{ name }}', { name: 'world' }) // hello world
replace('{{ greeting }} {{ name }}', { greeting: 'hi', name: 'world' }) // hi world
replace('hello {{ name }}', { name: (key, i, data, str) => key }) // hello name
replace('hello !! name !!', { name: 'world' }, [ '!! ', ' !!' ]) // hello world
```
## Parameters
- `str` `{String}` - A string to replace the contents of.
- `obj` `{Object}` - Key/value pairs, where value must be a function or a string.
- `delimiter` `{Array}` - Optional array with a start and end delimiter.
- `{String}` Start delimiter. Defaults to `'{{ '`.
- `{String}` End delimiter. Defaults to `' }}'`.
## Returns
- `{String}` - A string with replaced content.