-
-
Save toretto460/ea36d26d376aa2573747 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Hello {{firstName|upper}} {{lastName|lower}} your encoded password is {{password|encode}}! |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var fs = require('fs'); | |
var functions = { | |
upper: function (str) { return str.toUpperCase(); }, | |
lower: function (str) { return str.toLowerCase(); }, | |
encode: function (str) { return str.replace(/(.)/g, '*'); } | |
}; | |
var extractFunctions = function (group) { | |
var filters = group.split("|"); | |
if (filters.length == 1) { | |
return filters; | |
} | |
var value = filters.splice(0, 1); | |
return filters.reduce(function(fnx, f) { | |
functions[f] !== undefined ? fnx.push(functions[f]) : void 0; | |
return fnx; | |
}, [value]); | |
}; | |
module.exports = { | |
render: function(templateFile, data) { | |
return fs.readFileSync(templateFile, 'utf8').replace( | |
/\{\{([^{}]*)\}\}/g, | |
function (match, group) { | |
var expandedGroup = extractFunctions(group), | |
group = expandedGroup.splice(0, 1); | |
if (typeof data[group] !== 'undefined') { | |
return expandedGroup.reduce(function(calculatedValue, f) { | |
calculatedValue = f(calculatedValue); | |
return calculatedValue; | |
}, data[group]); | |
}; | |
return ''; | |
} | |
); | |
} | |
} | |
console.log( | |
module.exports.render("template.js.hb", {firstName: 'Simone', lastName: 'Di Maulo', password: 'password'}) | |
); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment