blob: 501fca5a5f358211175b6bbb72ed085dc716b431 (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
'use strict';
module.exports = (tpl, data) => {
if (typeof tpl !== 'string') {
throw new TypeError(`Expected a string in the first argument, got ${typeof tpl}`);
}
if (typeof data !== 'object') {
throw new TypeError(`Expected an Object/Array in the second argument, got ${typeof data}`);
}
const re = /{(.*?)}/g;
return tpl.replace(re, (_, key) => {
let ret = data;
for (const prop of key.split('.')) {
ret = ret ? ret[prop] : '';
}
return ret || '';
});
};
|