Created
June 2, 2016 19:02
-
-
Save kawanet/e3a19451b24142a70d0e4eaef5a6eb00 to your computer and use it in GitHub Desktop.
sprintf("%08d", x) == pad("0")(8)(x)
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
| /** | |
| * 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