Created
March 13, 2015 13:03
-
-
Save callerobertsson/65b0eaefa1c728a1641f to your computer and use it in GitHub Desktop.
Javascript: Pad string but don't truncate if too long
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
// Javascript Pad String | |
// Inspired by http://dev.enekoalonso.com/2010/07/20/little-tricks-string-padding-in-javascript/ | |
function pad(filling, value) { | |
if (('' + value).length > filling.length) { | |
return value; | |
} | |
return (filling + value).slice(-1 * filling.length); | |
} | |
console.log(pad('0000000000', 123456789)); | |
console.log(pad('xx', 'monkey')); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment