UNPKG

587 BJavaScriptView Raw
1module.exports = (template, data) => {
2 if (typeof template !== 'string') {
3 throw new TypeError(
4 `Expected a string in the first argument, got ${typeof template}`
5 )
6 }
7
8 if (typeof data !== 'object') {
9 throw new TypeError(
10 `Expected an Object/Array in the second argument, got ${typeof data}`
11 )
12 }
13
14 const regex = /(\\)?{(.*?)}/g
15
16 return template.replace(regex, (_, disabled, key) => {
17 if (disabled) return `{${key}}`
18
19 let ret = data
20
21 for (const prop of key.split('.')) {
22 ret = ret ? ret[prop] : ''
23 }
24
25 return ret || ''
26 })
27}