main
Last change
on this file was d565449, checked in by stefan toskovski <stefantoska84@…>, 4 weeks ago |
Update repo after prototype presentation
|
-
Property mode
set to
100644
|
File size:
764 bytes
|
Rev | Line | |
---|
[d565449] | 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 |
|
---|
| 12 | module.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 | };
|
---|
Note:
See
TracBrowser
for help on using the repository browser.