Skip to content

Instantly share code, notes, and snippets.

@kawanet
Created June 2, 2016 19:02
Show Gist options
  • Select an option

  • Save kawanet/e3a19451b24142a70d0e4eaef5a6eb00 to your computer and use it in GitHub Desktop.

Select an option

Save kawanet/e3a19451b24142a70d0e4eaef5a6eb00 to your computer and use it in GitHub Desktop.
sprintf("%08d", x) == pad("0")(8)(x)
/**
* sprintf("%08d", x) == pad("0")(8)(x)
*
* @param char {String}
* @returns {Function}
* @license MIT
* @see https://gist.github.com/kawanet/e3a19451b24142a70d0e4eaef5a6eb00
*/
function pad(char) {
return function(len) {
var prefix = padder(len);
return function(str) {
return (str == null) ? prefix : (prefix + str).substr(-len);
};
};
function padder(len) {
var array = [];
for (var i = 0; i < len; i++) {
array[i] = char;
}
return array.join("");
}
}
if ("undefined" !== typeof module) module.exports = pad;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment