blob: 0c20589cf2bcda8bafe6070714f653b7105c0aa1 (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
|
export default function interpolate(template, variables) {
return template.replace(/\${[^{]+}/g, (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);
}
|