Skip to content

Instantly share code, notes, and snippets.

View catpea's full-sized avatar
😺
Writing and Recording Poetry and Philosophy

Dr. Thunder Cat Pea catpea

😺
Writing and Recording Poetry and Philosophy
View GitHub Profile
@catpea
catpea / parse-es6-template.js
Created October 26, 2024 03:15 — forked from smeijer/parse-es6-template.js
ES6 template string parser
function get(path, obj, fb = `$\{${path}}`) {
return path.split('.').reduce((res, key) => res[key] || fb, obj);
}
function parseTpl(template, map, fallback) {
return template.replace(/\$\{.+?}/g, (match) => {
const path = match.substr(2, match.length - 3).trim();
return get(path, map, fallback);
});
}