UNPKG

654 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 return text.replace(/\{\{\s*([^{}]+?)\s*\}\}/g, (fullMatch, term) => {
17 if (term in data) {
18 return data[term];
19 }
20
21 // Preserve old behavior: If parameter name not provided, don't replace it.
22 return fullMatch;
23 });
24};