aboutsummaryrefslogtreecommitdiffhomepage
path: root/interpolate.js
blob: 443450ee0a58e13b2890177835a2c903a484fb18 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
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);
    });
}

//get the specified property or nested property of an object
function getObjPath(path, obj) {
    return path.split('.').reduce((res, key) => res[key], obj);
}