2021-06-08 18:46:20 +03:00
|
|
|
const template = require('lodash.template');
|
|
|
|
const interpolate = /(?<!{){([^{]+?)}/g;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Template function
|
|
|
|
* Takes strings like 'Your site is now available on {url}' and interpolates them with passed in data
|
|
|
|
* Will ignore double or triple braces like {{get}} or {{{helpername}}}
|
|
|
|
* Can handle escaped braces e.g. \\{\\{{helpername}\\}\\}
|
|
|
|
* But there's a simple bare minimum escaping needed to make {{{helpername}}} work e.g. {\\{{helpername}}}
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* @param {String} string - string with optional {data properties}
|
|
|
|
* @param {Object} [data] - optional data to interpolate
|
2021-06-09 14:25:45 +03:00
|
|
|
* @returns {string} the interpolated string
|
2021-06-08 18:46:20 +03:00
|
|
|
*/
|
|
|
|
module.exports = (string, data) => {
|
|
|
|
if (!data) {
|
|
|
|
return string;
|
|
|
|
}
|
|
|
|
|
|
|
|
// We replace any escaped left braces with the unicode character so we can swap it back later
|
|
|
|
let processedString = string.replace(/\\{/g, '\\U+007B');
|
|
|
|
// Let lodash do its thing
|
|
|
|
processedString = template(processedString, {interpolate})(data);
|
|
|
|
// Replace our swapped out left braces and any escaped right braces
|
|
|
|
return processedString.replace(/\\U\+007B/g, '{').replace(/\\}/g, '}');
|
|
|
|
};
|