Replace key/value pairs in a string.
JavaScript
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
src
test
.gitignore
.travis.yml
LICENSE
README.md
package.json

README.md

key-value-replace

Travis Build Status Coverage Status Dependencies

Replace key/value pairs in a string.

Install

npm install key-value-replace

Usage

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.