can-key/replace-with/replace-with
Replace the templated parts of a string with values from an object.
replaceWith(str, data, replacer, remove)
import replaceWith from "can-key/replace-with/replace-with";
replaceWith("foo_{bar}", {bar: "baz"}); // -> "foo_baz"
Parameters
- str
{String}
:String with {curly brace} delimited property names.
- data
{Object}
:Object from which to read properties.
- replacer
{function(key, value)}
:Function which returns string replacements. Optional.
replaceWith("foo_{bar}", {bar: "baz"}, (key, value) => { return value.toUpperCase(); }); // -> "foo_BAZ"
- shouldRemoveMatchedPaths
{Boolean}
:Whether to remove properties found in delimiters in
str
fromdata
.
Returns
{String}
:
the supplied string with delimited properties replaced with their values.
var replaceWith = require("can-key/replace-with/replace-with");
var answer = replaceWith(
'{.}{.}{.}{.}{.} Batman!',
{},
() => 'Na'
);
// => 'NaNaNaNaNa Batman!'