23 lines
485 B
JavaScript
23 lines
485 B
JavaScript
'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 || '';
|
|
});
|
|
};
|