SymphonyElectron/js/utils/stringFormat.js
2018-08-29 10:39:16 +05:30

22 lines
604 B
JavaScript

/**
* Injects content into string
* @param string {String}
* @param data {Object} - content to replace
* @return {*}
*/
function stringFormat(string, data) {
for (let key in data) {
if (Object.prototype.hasOwnProperty.call(data, key)) {
return string.replace(/({([^}]+)})/g, function (i) {
let replacedKey = i.replace(/{/, '').replace(/}/, '');
if (!data[replacedKey]) {
return i;
}
return data[replacedKey];
});
}
}
return null;
}
module.exports = stringFormat;