Created
February 25, 2010 21:46
-
-
Save flesch/315070 to your computer and use it in GitHub Desktop.
Very simple sprintf() implementation
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
function sprintf(){ | |
var args = Array.prototype.slice.call(arguments), f:Array = [], str = args.shift().split("%s"); | |
while (str.length) { | |
f.push(str.shift(), args.shift() || ""); | |
} | |
return f.join(""); | |
} | |
trace(sprintf("Hello %s, how ya %s?", "motherf*cker", "durin")); |
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
function sprintf(){ | |
var args = Array.prototype.slice.call(arguments); | |
return args.shift().replace(/%s/g, function(){ | |
return args.shift(); | |
}); | |
} | |
alert(sprintf("Hello %s, how ya %s?", "motherf*cker", "durin")); |
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
// http://mir.aculo.us/2011/03/09/little-helpers-a-tweet-sized-javascript-templating-engine/ | |
function t(s,d){ | |
for(var p in d) | |
s = s.replace(new RegExp('{'+p+'}','g'), d[p]); | |
return s; | |
} | |
// t("Hello {who}!", { who: "JavaScript" }); -> "Hello JavaScript!" |
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
function x(s, d) { | |
return s.replace(/\{([\w]+)\}/ig, function(a, b){ | |
return d[b]; | |
}); | |
} | |
// x("Hello {who}!", { who: "JavaScript" }); -> "Hello JavaScript!" | |
// http://jsperf.com/tweet-sized-template-engines |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment