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