Created
August 31, 2012 14:23
-
-
Save joras/3553424 to your computer and use it in GitHub Desktop.
Fast, compiled(using eval :) named parameter formatter for JS.
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
function formatter(format) { | |
var findpatt = /:(\w+?):/g | |
var format = format.replace(findpatt, '"+data["$1"]+"'); | |
eval('var nfun = function(data) {'+ | |
'return "'+format+'";'+ | |
"};") | |
return nfun; | |
} | |
var test = formatter(":1:, blaah :2:"); | |
var test2 = formatter(":1:, :2:"); | |
console.log(test({1: "one", 2: "two"})); | |
console.log(test2({1: "one", 2: "two"})); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment