aboutsummaryrefslogtreecommitdiffhomepage
path: root/interpolate.js
diff options
context:
space:
mode:
authorAmine El Baghdadi <75886237+NoobJsPerson@users.noreply.github.com> 2021-05-15 17:56:31 +0000
committerAmine El Baghdadi <75886237+NoobJsPerson@users.noreply.github.com> 2021-05-15 17:56:31 +0000
commit381a3ab65b507ca18c2ad4c75fb7ad5897bd4dd5 (patch)
tree7f4a575cc679b2dbe6677f5c6f6d2fcc1839d146 /interpolate.js
parent9a5b4f8f56593b56564865af854558818eab3a87 (diff)
update interpolate.js
Diffstat (limited to 'interpolate.js')
-rw-r--r--interpolate.js13
1 files changed, 13 insertions, 0 deletions
diff --git a/interpolate.js b/interpolate.js
index e69de29..36cf2f8 100644
--- a/interpolate.js
+++ b/interpolate.js
@@ -0,0 +1,13 @@
+const regex = /\${[^{]+}/g;
+
+export default function interpolate(template, variables, fallback) {
+ return template.replace(regex, (match) => {
+ const path = match.slice(2, -1).trim();
+ return getObjPath(path, variables, fallback);
+ });
+}
+
+//get the specified property or nested property of an object
+function getObjPath(path, obj, fallback = '') {
+ return path.split('.').reduce((res, key) => res[key] || fallback, obj);
+} \ No newline at end of file