Skip to content

Instantly share code, notes, and snippets.

@phlik
Created May 25, 2012 17:43
Show Gist options
  • Save phlik/2789429 to your computer and use it in GitHub Desktop.
Save phlik/2789429 to your computer and use it in GitHub Desktop.
finding a good way to replace values in a string.
/**
* User: ehouston
*/
var _ = require('underscore');
var localSub = function (text, data) {
// TODO: Implement more efficient replace.
// Loop through all keys and replace the data.
var result = text;
_.each(_.keys(data), function (key) {
var pattern = eval('/{' + key + '}/g');
result = result.replace(pattern, data[key]);
});
return result;
};
var stampValues = function (template, replacements) {
for (var tag in replacements) {
var reg = new RegExp('\{' + tag + '\}', 'g');
template = template.replace(reg, replacements[tag]);
}
return template;
};
var stampValuesEx = function (template, replacements) {
_(_(replacements).keys()).each(function (tag) {
var reg = new RegExp('\{' + tag + '\}', 'g');
template = template.replace(reg, replacements[tag]);
});
return template;
};
var stampValuesEx0 = function (template, replacements) {
_(replacements).each(function (value, tag) {
var reg = new RegExp('\{' + tag + '\}', 'g');
template = template.replace(reg, value);
});
return template;
};
var stampValuesEx2 = function (template, replacements) {
var setValues = (function () {
var translateReg = /{\w+}/g;
var cleanup = /[{}]/g;
return function (s) {
return (s.replace(translateReg, function (match) {
return replacements[match.replace(cleanup, '')];
}));
};
})();
return setValues(template);
}
var stampValuesEx3 = (function () {
var translateReg = /{\w+}/g;
var cleanup = /[{}]/g;
return function (template, replacements) {
return (template.replace(translateReg, function (match) {
return replacements[match.replace(cleanup, '')];
}));
}
})();
var calcInterval = function (test, start, end) {
console.log("interval for " + test + " was " + (end - start) + " milliseconds");
};
var evaluateStampFunction = function (name, testCall, iteractions) {
console.log("");
console.log("====" + name + '====');
var testString = "{name} worked with {name2} and {name2} was impressed with {name}.";
console.log(testCall(testString, {name: "bob", name2: "tom"}));
var i = 0;
var start = +new Date();
for (i = 0; i < iteractions; i++) {
testCall(testString, {name: "bob", name2: "tom"});
}
var end = +new Date();
calcInterval(name, start, end);
};
var count = 10000;
evaluateStampFunction("localSub", localSub, count);
evaluateStampFunction("stampValues", stampValues, count);
evaluateStampFunction("stampValuesEx0", stampValuesEx0, count);
evaluateStampFunction("stampValuesEx", stampValuesEx, count);
evaluateStampFunction("stampValuesEx2", stampValuesEx2, count);
evaluateStampFunction("stampValuesEx3", stampValuesEx3, count);
evaluateStampFunction("localSub", localSub, count);
evaluateStampFunction("stampValues", stampValues, count);
evaluateStampFunction("stampValuesEx", stampValuesEx, count);
evaluateStampFunction("stampValuesEx0", stampValuesEx0, count);
evaluateStampFunction("stampValuesEx2", stampValuesEx2, count);
evaluateStampFunction("stampValuesEx3", stampValuesEx3, count);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment