7 ms·
Interesting that String padding made it in -- sort of jumps out as the simplest of these additions. I wonder how much of that had to do with negative PR for JS-
by yahelc 9y ago
Interesting that String padding made it in -- sort of jumps out as the simplest of these additions. I wonder how much of that had to do with negative PR for JS-land due to left-pad-gate.
- DelaneyM 9y agoYou can read that disaster in two ways... I consider the fact that a stupid-simple package was depended-upon by so many mature libraries as an indication it should be a language feature. Working with network protocols I find myself needing padding functions all the time, and there isn't really an elegant way to do so inline, so I welcome this addition.
- andersonk 9y agoHonestly, I feel most of the blame falls to NPM for allowing publishers to delete packages. This doesn't happen in other ecosystems (e.g. Java).
- steveklabnik 9y agoAfterward, they changed their policies so that this can't happen again.
- pluma 9y agoMost of the blame falls to npm Inc for bending over backwards to a corporation for a bogus trademark claim they weren't even involved in. Sure, left-pad being deleted was what resulted in most people's problems but this was just the fallout from npm Inc forcibly reassigning an actively used package name from a major open source contributor to appease a company that didn't even threaten them directly.
- mbell 9y ago> I consider the fact that a stupid-simple package was depended-upon by so many mature libraries as an indication it should be a language feature. Unfortunately neither the npm module nor the browser version really do what most people want and string handling in javascript is still a minefield. '\u{1F4A9}'.padStart(5, '1') => "111" // oops (\u{1F4A9} is at the end of this, HN filter) '\u{1F4A9}'.length => 2 [...'\u{1F4A9}'].length => 1 //WTF? 'mañana'.padStart(7, '1') => "1mañana" // ok 'man\u0303ana'.padStart(7, '1') => "mañana" // oops 'man\u0303ana'.length => 7 [...'man\u0303ana'].length => 7 // WTF? Why doesn't this match the behavior of [...'\u{1F4A9}'].length ? 'man\u0303ana'.normalize('NFC').padStart(7, '1') => "1mañana" // OK I do understand the unicode issues here, but the inconsistency in the APIs from a user perspective and lack of any fully cross browser support for sane string processing in Javascript means we still have only a few options: 1) Don't do string processing in javascript at all. 2) Include a library to make it sane, these are usually huge as they usually need large lookup tables. 3) Accept that things won't always be correct. This is one example but the lack of a sane standard library in Javascript is one of the biggest problems the web has right now. I'd be curious to know how many bytes of JS are loaded on the average website just to work around the lack of standard library support for basic functionality, I'd bet it's a very large number. Another fun one: Try to parse a URL and append extra query params to it, correctly.
- javajosh 9y agoIt seems like certain code points (like '\u{1F4A9}' aka the poop emoji) are a single character but the string report a length of 2. That is the root of all of those problems. One of your "problems", the length of an array with a single string element, isn't a problem.
- recursive 9y ago> One of your "problems", the length of an array with a single string element That's not what it is. Look again, and pay attention to the ... part.
- javajosh 9y agoThat's really helpful, thanks.
- mbell 9y ago> One of your "problems", the length of an array with a single string element, isn't a problem. You're misunderstand the code, it's using the spread operator on a string: [...'word'] => ["w", "o", "r", "d"] What is being demonstrated is that under the hood, javascript stores astral plane codepoints as surrogate pairs and strings operate on 'characters' which is why '\u{1F4A9}'.length => 2. But, when the spread operator is applied to a string, it breaks up the string into codepoints, not characters. This is also why [...'man\u0303ana'].length => 7, the combining tilde is a separate codepoint. This is an example of how wonky string processing in JS is, [...string].length is actually the most straightforward way to get a count of codepoints in a string.
- colejohnson66 9y agoAny Unicode codepoint above U+FFFF will require 2 UTF-16 characters
- jcranmer 9y ago> [...'man\u0303ana'].length => 7 // WTF? Why doesn't this match the behavior of [...'\u{1F4A9}'].length ? The key thing to remember is that iteration over Unicode strings only makes sense as iteration over code points, not UCS-2 characters, not bytes, not grapheme clusters. The JS String iterator was very deliberately made to iterate over code points. That length reports UCS-2 characters is a historical mistake. That padding is operating on UCS-2 characters is probably a reflection of the fact that the operation isn't well-defined beyond ASCII.
- hajile 9y agoI believe the string padding proposal predated the "leftpad" incident by a year or so.