News for hackers. Stuff that matters.
Re:Node.js packages don't deserve trust by stank345 on Monday April 11, 2022 @10:30AM (#30989261)
Part of it is that JS's standard library is terrible (eg. no built-in union/intersection when working with sets, bizarre default sorting behavior see (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Refe...), groupBy (see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Refe...) is new and not well-supported, etc). You almost have to reach for some utility library or build your own ad-hoc one just to use the language.
Re:Node.js packages don't deserve trust by yardstick on Monday April 11, 2022 @10:42AM (#30989462)
> You almost have to reach for some utility library or build your own ad-hoc one just to use the language.

Sure. But are hundreds of dependencies really required for this?

In Java you would use tools like Guava or Spring for general quality of life improvements, and there would be a few deps for them (under a dozen iirc).

The solution is for the “top tier” libraries and frameworks in the JS world to be designed with minimal dependencies. And where they do have a need for a dependency, they undertake serious consideration of the best option that minimises dependency hell.

Re:Node.js packages don't deserve trust by ragnese on Monday April 11, 2022 @11:04AM (#30989788)
And those "top tier" libraries do exist for some stuff- especially if we're talking about the anemic standard library. The famous lodash library doesn't have any (non-lodash-umbrella) dependencies AFAIK.
Re:Node.js packages don't deserve trust by stank345 on Monday April 11, 2022 @11:03AM (#30989775)
Yeah, there's definitely a separate cultural/inexperience problem at play as well.
Re:Node.js packages don't deserve trust by zkldi on Monday April 11, 2022 @10:41AM (#30989436)
> Bizarre default sorting behaviour

You are meant to pass an argument to [].sort; If you don't, it falls back to the most generic thing that makes sense -- which is turning everything into a string and sorting lexicographically.

Arrays in JS can have any type in them, such as [number, string, function]. That's obviously very unlikely, but the only thing in common between all types in JS is that they can all be explicitly turned into strings.

And yes, I agree that throwing an error here for no argument would be better here (a linter WILL enforce this), but this is hardly a critical shortcoming of JS's standard library, and you DEFINITELY do not need a utility library just to use the language (especially for your examples).

Re:Node.js packages don't deserve trust by stank345 on Monday April 11, 2022 @10:58AM (#30989708)
I hear what you're saying, but it still seems pretty bonkers to me that if you try to sort an array of numbers it will cast them to strings and sort alphabetically (!):

  > [1, 2, 10, 3].sort()
  [ 1, 10, 2, 3 ]
> And yes, I agree that throwing an error here for no argument would be better here (a linter WILL enforce this), but this is hardly a critical shortcoming of JS's standard library

I suppose "critical" is debatable but this seems very fundamental and very unexpected to me.

> you DEFINITELY do not need a utility library just to use the language (especially for your examples)

I hadn't actually considered using a linter to avoid these types of standard library footguns... that's actually a pretty great idea!

Re:Node.js packages don't deserve trust by hombre_fatal on Monday April 11, 2022 @11:24AM (#30990089)
Since arrays can contain a mix of anything, I don't think it's that bonkers that the default impl chose to canonicalize values into consistent comparables with String(). It's just not useful for numbers.

But for example, they probably decided that it was more useful defaulting to having a sort order for things that otherwise aren't comparable:

    a = [undefined, null, undefined, null]
    a.sort()
    // [ null, null, undefined, undefined ]
Since null < undefined and undefined < null are both false, then a simple `a < b` comparator wouldn't sort them at all. Same for objects.

If you have an array that's a huge mix of random values, from null to undefined to [] to {} and you sort() it, all of those values will now be grouped together by type.

This doesn't seem like a bonkers trade-off.

Re:Node.js packages don't deserve trust by zkldi on Monday April 11, 2022 @03:01PM (#30993171)
There would be a significant performance impact if `.sort()` argumentless had to traverse the list and checked all inputs were numbers. It's better to pass a sort function -- that is how the API is meant to be used.

I agree that without an argument it should just be a fatal error, but if it were to have any sort of functionality, it should convert all to strings.

This is consistent among JS's apis, and pretty much the origin of all the 'js wut' moments on the internet. Everything in JS can be converted into a string. If you do something stupid with disparate types, it will likely turn operands into strings and compare them that way.

Re:Node.js packages don't deserve trust by protonfish on Monday April 11, 2022 @10:40AM (#30989412)
I don't see that as the case. Modern JS can do a lot out of the box but the culture looks down on "vanilla" JavaScript. I've seen way too many libraries that are nothing but a thin wrapper around native functionality. When your first (and only) technique is to look for a library, this is where you end up.
Re:Node.js packages don't deserve trust by stank345 on Monday April 11, 2022 @11:02AM (#30989770)
I agree and am very pro vanilla JS FWIW. I just find myself reaching for something like lodash's `intersection`/`difference` functions when working with sets, `sortBy` to get more normal (and not in-place) sorting behavior, and `groupBy` to do group by.