Created
December 16, 2013 12:45
-
-
Save mamoo/7986432 to your computer and use it in GitHub Desktop.
Simple string padding method using Array.
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 pad(string, length, padstring){ | |
| if (!string || !length || !padstring) | |
| throw new Error('all parameters must be defined'); | |
| var outputstr = '' + string, | |
| diff = length - outputstr.length; | |
| if (diff < 1) return string; | |
| var arr = new Array(diff); | |
| arr.push(outputstr); | |
| return (arr.join(padstring)); | |
| } | |
| //TEST | |
| console.log(pad('a', 15, 'b')); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment