Created
February 2, 2014 01:16
-
-
Save rmariuzzo/8761698 to your computer and use it in GitHub Desktop.
Simple and minimal `sprintf` function in JavaScript.
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 sprintf(format) { | |
var args = Array.prototype.slice.call(arguments, 1); | |
var i = 0; | |
return format.replace(/%s/g, function() { | |
return args[i++]; | |
}); | |
} |
Nice! Thanks
I love it!
const sprintf = ( format, ...args ) => ( i = 0, format.replace( /%s/g, () => args[i++] ) );
My version in arrow function.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks for sharing, smartly done! I es6-ified it into: