interpolate.js 764 B

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