key-value-replace

Replace key/value pairs in a string

Usage no npm install needed!

<script type="module">
  import keyValueReplace from 'https://cdn.skypack.dev/key-value-replace';
</script>

README

key-value-replace

Build Coverage Status

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.