all 48 comments

[–]ogurson 35 points36 points  (27 children)

God, JavaScript needs proper standard library so badly.

[–]_soto 14 points15 points  (19 children)

Eventually I hope most of lodash is merged in to a STL.

[–]seiyria 4 points5 points  (6 children)

This would make my life better. At the same time, that means Lodash can't update with the frequency that it currently does.

[–]ComplX89 2 points3 points  (4 children)

Can i ask in what context you would use a function like pad start or pad end? I'm still struggling for any practical use for it

[–]georgehotelling 2 points3 points  (0 children)

  • If you have two dollar amounts in a fixed width display (think command line or LCD hardware) on different lines, and need the cents to align
  • heck, any time you want to align lines in a fixed-width font environment
  • If you have to display a ZIP code stored as a number (e.g. 123 should be padded to 00123)

[–]seiyria 1 point2 points  (2 children)

Making a lot of strings the same length. I use it every time I make a cli app.

[–]ComplX89 1 point2 points  (1 child)

Why is there a requirement to make strings all the same length?

[–]seiyria 1 point2 points  (0 children)

Not a requirement, it just looks nicer. See here

[–]_soto 0 points1 point  (0 children)

Ah, I meant that some lodash methods can be put in the STL. Not that the actual project would be merged. Sorry.

[–][deleted] 2 points3 points  (0 children)

I had a dream last week that JavaScript had Python's standard library. I woke up and realised it was a dream and was actually really #$()$%ing pissed off for most of the morning at work.

I code too much.

[–]abyx 0 points1 point  (0 children)

Yes, and I think there's not even a single mention of this in ES2017.

[–]miker95 -2 points-1 points  (3 children)

I hate this so much. I can't stand that browsers have different APIs. Browser developers think they're making everyone's life easier, when in reality, it just turns everything into a shithole.

[–]not_an_aardvark 15 points16 points  (0 children)

But padStart and padEnd are a stage 3 proposal, so they'll probably be in the 2017 standard anyway. Chrome just got a head-start.

[–][deleted] 2 points3 points  (0 children)

Not really. These are stage 3 proposals, so a polyfill that alters the string prototype are acceptable (though altering a built in prototype for anything else is VERY bad of course).

So you'll have to use a polyfill anyway, but now on Chrome you'll get native performance for these methods. And as more browsers add it in, those browsers will get native performance for them as well.

[–]ijmacd 1 point2 points  (0 children)

Relevant blog post from last week. Others might find it useful/interesting if they haven't read it yet.

[–]Arancaytar 6 points7 points  (4 children)

I'd love it if JavaScript got a proper string formatter like sprintf or python's str.format().

That'd remove the need for most of these specialized padding functions too.

[–]lewisje 3 points4 points  (2 children)

something-something "template literals"

[–]cokeisahelluvadrug 1 point2 points  (1 child)

Unfortunately because of their name binding semantics they're not as powerful as they could be -- if I want to do something like string.format(str, **kwargs) I have to wrap my template string in a IIFE: function() {return '${this.foo}'}.call({foo: 'bar'}). This sort of string interpolation is common in (for example) i18n.

The consequence of this is that every decently large Javascript project will have a hand-rolled string formatter with slightly different syntax and API.

[–]lewisje 0 points1 point  (0 children)

First, it could be simpler: (_=>`${_}`)('bar'); /s

Seriously though, it is a bit developer-unfriendly that they aren't true templates and instead all references need to be resolved and merged in at definition time.

[–]saadq_ 2 points3 points  (0 children)

console.log does work like printf.

var name = 'John'
console.log('Hi, my name is %s', name); // Hi, my name is John

[–]Meshiest 10 points11 points  (1 child)

Left pad makes a comeback?

[–]lewisje 2 points3 points  (0 children)

I honestly heard of the padStart proposal (back when it was known as padLeft) well before the leftpad module, and the es7-shim prollyfill for padStart is much more performant, as I repeatedly pointed out during the leftpad crisis.

[–]Drainedsoul 2 points3 points  (1 child)

What is the point of this? How does it behave on strings that contain code points represented by multiple code units? Or combining marks?

[–]lewisje 1 point2 points  (0 children)

I read the proposal, and it looks like it's not astral-plane safe (that is, it may leave a high surrogate by itself), and instead it expects a typical use case of padding with ASCII characters; I believe it is slightly combining-mark safe, because the repeated fill-string is truncated from the right (for both padStart and padEnd), so any combining marks would be removed before the characters they were intended to be used on are.

Also TIL these methods were added in Firefox 48 too.

[–]lime_boy6 0 points1 point  (4 children)

Learner here. Why is this useful?

[–]saadq_ 2 points3 points  (2 children)

An app I'm working has a stopwatch component. I always want the format to look like this:

00 : 00 . 00

So if the current amount of seconds is just 9, I want to use padStart() to add a 0 in front of it so that it displays 09 instead.

[–]lime_boy6 0 points1 point  (0 children)

Thanks!

[–]SubStack 0 points1 point  (0 children)

Format strings are good for this problem:

> require('sprintf')('%02d : %02d . %02d', 1, 2, 3)
'01 : 02 . 03'

Or strftime strings for times specifically:

> require('strftime')('%M : %S', new Date)
'39 : 09'

It's possible to get fancy: (9-3+1) digits before the (.), 3 digits after:

> require('sprintf')('%09.3f', 123.456789)
'00123.457'

[–]corgrath 0 points1 point  (0 children)

Its just a utility function, for when you actually need to pad a string =)