UNPKG

764 BJavaScriptView Raw
1/**
2 * @fileoverview Interpolate keys from an object into a string with {{ }} markers.
3 * @author Jed Fox
4 */
5
6"use strict";
7
8//------------------------------------------------------------------------------
9// Public Interface
10//------------------------------------------------------------------------------
11
12module.exports = (text, data) => {
13 if (!data) {
14 return text;
15 }
16
17 // Substitution content for any {{ }} markers.
18 return text.replace(/\{\{([^{}]+?)\}\}/gu, (fullMatch, termWithWhitespace) => {
19 const term = termWithWhitespace.trim();
20
21 if (term in data) {
22 return data[term];
23 }
24
25 // Preserve old behavior: If parameter name not provided, don't replace it.
26 return fullMatch;
27 });
28};