|
|
|
|
|
by DelaneyM
3258 days ago
|
|
You 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. |
|
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.