Re:I've Just Liberated My Modules
by chvid on Tuesday March 22, 2016 @09:28PM
(#11341428)
You need to go something like:
Unfortunately we need to wrap str twice so maybe a one-liner is not quite in place.
[ Parent
| on HN ]
The array + join is slower
http://jsperf.com/leftpadtesting
Repeat + slice is too
So this I guess
module.exports = function leftpad (str, len, ch) { str = String(str); if (ch === 0) { ch = '0'; } return Array(Math.max(0, len - str.length)).join(ch || ' ') + str; };
Cool, I like the Math.max. Two liner then
str = String(str);\n...
And I did not notice the 0 check in the original code either :-D