aboutsummaryrefslogtreecommitdiffhomepage
path: root/interpolate.js
blob: 70ac4eb106aa9992549e663e6212955c1a98697e (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
const regex = /\${[^{]+}/g;

export default function interpolate(template, variables) {
    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);
}