Skip to content
Myr

tmpl.js

This file

Path

test/support/tmpl.js

35 lines · 681 bytes · JavaScript

Last changed by

Merge tag '3.18.6'Douglas Christopher Wilsonbefore adoption4aa2801

History of this file

Take it

Merge tag '3.18.6'Douglas Christopher Wilsonbefore adoption4aa2801

lines 1–35why, one step back
1var fs = require('fs');
23var variableRegExp = /\$([0-9a-zA-Z\.]+)/g;45module.exports = function renderFile(fileName, options, callback) {6 function onReadFile(err, str) {7 if (err) {8 callback(err);9 return;10 }1112 try {13 str = str.replace(variableRegExp, generateVariableLookup(options));14 } catch (e) {15 err = e;16 }1718 callback(err, str);19 }2021 fs.readFile(fileName, 'utf8', onReadFile);22};2324function generateVariableLookup(data) {25 return function variableLookup(str, path) {26 var parts = path.split('.');27 var value = data;2829 for (var i = 0; i < parts.length; i++) {30 value = value[parts[i]];31 }3233 return value;34 };35}