Skip to content

Instantly share code, notes, and snippets.

@mamoo
Created December 16, 2013 12:45
Show Gist options
  • Select an option

  • Save mamoo/7986432 to your computer and use it in GitHub Desktop.

Select an option

Save mamoo/7986432 to your computer and use it in GitHub Desktop.
Simple string padding method using Array.
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