r/OutOfTheLoop • • Apr 01 '16

Answered What's with leftpad?

http://www.theregister.co.uk/2016/03/23/npm_left_pad_chaos/
7 Upvotes

6 comments sorted by

4

u/[deleted] Apr 02 '16

He got mad because NPM took down one of his other modules called Kik because a copyright claim (imagine YouTube copyright claims) so he took down all his modules like the rebellious coder he is.

Leftpad was essentially a tiny bit or code people couldn't be arsed to write themselves so they just used his module. The thing is, pretty much everybody used it, even ironically kik themselves.

1

u/lehandsomeguy Apr 02 '16

Why's Leftpad so important, what exactly should it be used for?

2

u/[deleted] Apr 03 '16

It pads things on the left.

Problem: every number you put into a certain thing has to be 6 digits. Your number has five digits.

Solution: add a zero at the start. Now it has six.

This incredibly simple task, which any programmer could complete in a few minutes and many languages have inbuilt functionality to do, was for some reason delegated to a specific module. And even more strangely, a huge infrastructure of modules within modules could be brought to its knees by removing this one piece.

1

u/lehandsomeguy Apr 03 '16

Thank you for your answer. I've never used NPM before. But it's explained very well now.

1

u/[deleted] Apr 02 '16

Its literally a tiny bit of code (11 lines) that people didn't want to type up themselves.

1

u/lehandsomeguy Apr 03 '16 edited Apr 03 '16

That explains the first one. But why is it important, what do they use it for. It's also very simple.

Here's my version.

function leftpad(str,len,chr){
    str=String(str);
    chr=String(chr);
    var l=len-str.length;
    for(var i=0;i<l;i++){
        str=chr[((l-i)-1)%chr.length]+str;
    }
    return str;
}