get=(path,obj,fallback)=>{
const paths=path.trim().split('.')
let result=obj
for(const p of paths){
result=result[p]
if(result===undefined){
return fallback
}
}
return result
}
compile=(tpl,obj)=>{
return tpl.replace(/{(.+)}/g,matched=>{
const value=get(matched.slice(1,-1).replace(/obj\./g,''),obj,matched)
if(value instanceof Object)return JSON.stringify(value)
return value
})
}
var obj = {
a: 1,
b: {c: {d:2}},
c: false
}
var tmpl = `
a 阿斯顿发送到发 {obj.a}
b{obj.b.c}
c{ obj.c.d}
`
console.log(compile(tmpl,obj))