mirror of
https://github.com/nodejs/node.git
synced 2025-05-22 15:55:37 +00:00

PR-URL: https://github.com/nodejs/node/pull/27670 Reviewed-By: Michaël Zasso <targos@protonmail.com> Reviewed-By: Richard Lau <riclau@uk.ibm.com> Reviewed-By: Refael Ackermann (רפאל פלחי) <refack@gmail.com> Reviewed-By: Daijiro Wachi <daijiro.wachi@gmail.com> Reviewed-By: Ujjwal Sharma <usharma1998@gmail.com>
29 lines
764 B
JavaScript
29 lines
764 B
JavaScript
/**
|
|
* @fileoverview Interpolate keys from an object into a string with {{ }} markers.
|
|
* @author Jed Fox
|
|
*/
|
|
|
|
"use strict";
|
|
|
|
//------------------------------------------------------------------------------
|
|
// Public Interface
|
|
//------------------------------------------------------------------------------
|
|
|
|
module.exports = (text, data) => {
|
|
if (!data) {
|
|
return text;
|
|
}
|
|
|
|
// Substitution content for any {{ }} markers.
|
|
return text.replace(/\{\{([^{}]+?)\}\}/gu, (fullMatch, termWithWhitespace) => {
|
|
const term = termWithWhitespace.trim();
|
|
|
|
if (term in data) {
|
|
return data[term];
|
|
}
|
|
|
|
// Preserve old behavior: If parameter name not provided, don't replace it.
|
|
return fullMatch;
|
|
});
|
|
};
|