Created
March 6, 2014 13:21
-
-
Save boxmein/9389559 to your computer and use it in GitHub Desktop.
A small Javascript function that extrapolates in-string code like Ruby does.
This file contains hidden or 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
/* | |
extrapolate.js | |
============== | |
A small utility function that allows Ruby-like string extrapolation | |
via #{expr}. Relies on ES5's Array#forEach but is relatively easy | |
to convert not to use it. | |
...that's it, folks! | |
*/ | |
String.prototype.extrapolate = function() { | |
var str = this; | |
this.match(/#\{(.+?)\}/g).forEach(function(each) { | |
str = str.replace(each, eval(each.slice(2, -1))); | |
}); | |
return str.valueOf(); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment