thedesk/app/node_modules/pupa/index.js
2018-01-28 21:27:11 +09:00

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 || '';
});
};