generated from dr-btd-student/p3
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtemplate-processor.js
More file actions
29 lines (27 loc) · 1007 Bytes
/
template-processor.js
File metadata and controls
29 lines (27 loc) · 1007 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
'use strict';
class TemplateProcessor {
/**
* Constructor for TemplateProcessor class.
* @param {string} template The template string to process.
*/
constructor(template) {
this.template = template;
}
/**
* Fills in placeholders in the template with values from the dictionary.
* @param {object} dictionary Key-value pairs for replacing placeholders.
* @returns {string} The template with placeholders replaced.
*/
fillIn(dictionary) {
let returnString = this.template;
for (const property in dictionary) {
if (Object.prototype.hasOwnProperty.call(dictionary, property)) {
const regex = new RegExp("{{" + property + "}}", "g");
returnString = returnString.replace(regex, dictionary[property]);
}
}
const regex = /{{.*?}}/g; // Updated regex to match non-greedy
returnString = returnString.replace(regex, "");
return returnString;
}
}