Last active
October 14, 2018 13:08
-
-
Save element6/61f9ce16f5d3ce20bc635181c882cac9 to your computer and use it in GitHub Desktop.
padding function
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 padZeros(str, pad = '00') { | |
const padding = typeof pad === 'number' ? Array(pad).fill('0').join('') : pad // '0'.repeat(pad) is faster but need polyfill for IE | |
return (padding + str).slice(-padding.length) | |
} | |
padZeros(2) // '02' | |
padZeros(2, '000') // '002' | |
padZeros(234) // '34' | |
padZeros(1, 4) // '0001' second argument can be a number |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment