mirror of
https://github.com/strapi/strapi.git
synced 2025-10-01 03:12:48 +00:00
27 lines
737 B
JavaScript
27 lines
737 B
JavaScript
![]() |
import { isPlainObject, isString } from 'lodash';
|
||
|
|
||
|
const templateObject = function (obj, variables) {
|
||
|
// Allow values which looks like such as
|
||
|
// an ES6 literal string without parenthesis inside (aka function call).
|
||
|
const regex = /\$\{[\S]*\}/g;
|
||
|
const replacer = (match) => {
|
||
|
const key = match.substring(0, match.length - 1).replace('${', '');
|
||
|
|
||
|
return variables[key];
|
||
|
}
|
||
|
|
||
|
return Object.keys(obj).reduce((acc, key) => {
|
||
|
if (isPlainObject(obj[key])) {
|
||
|
acc[key] = templateObject(obj[key]);
|
||
|
} else if (isString(obj[key]) && regex.test(obj[key])) {
|
||
|
acc[key] = obj[key].replace(regex, replacer);
|
||
|
} else {
|
||
|
acc[key] = obj[key];
|
||
|
}
|
||
|
|
||
|
return acc;
|
||
|
}, {});
|
||
|
};
|
||
|
|
||
|
export default templateObject;
|