News for hackers. Stuff that matters.
Re:TC39: Add Object.groupBy and Map.groupBy by aragonite on Tuesday December 19, 2023 @05:13PM (#38702543)
What's the thinking behind attaching Object.groupBy to the Object constructor? With the other Object.X() functions I can easily see why it makes sense they exist on the Object constructor, because they generally follow the pattern of taking an object as argument, and say, freezes that object, returns that object's [[Prototype]], returns that object's keys, check if that object is sealed, etc. By contrast, Object.groupBy is a utility function that takes an iterable and a callback, and doesn't seem to have anything distinctively to do with "Object" per se.

(I guess one exception is Object.is, which takes two values as arguments. But even Object.is makes a lot of sense existing on the Object constructor because it exposes an important abstract operation (SameValue). Object.groupBy is only a utility function)

Re:TC39: Add Object.groupBy and Map.groupBy by bakkoting on Tuesday December 19, 2023 @05:25PM (#38702734)
We originally tried to put it on Array.prototype, under multiple different names, and that broke various pages in various ways. So we gave up and had to put it somewhere else. And it's conceptually similar to `fromEntries` - they're both ways of making an object. So the Object constructor was the obvious choice.
Re:TC39: Add Object.groupBy and Map.groupBy by no_wizard on Tuesday December 19, 2023 @05:51PM (#38703045)
This is why we need a global Iterator / Iterable type. These are iterators at the end of the day after all. That would also signal the fact they could be used for more than one data structure (Array, Map, POJOs)
Re:TC39: Add Object.groupBy and Map.groupBy by bakkoting on Tuesday December 19, 2023 @05:58PM (#38703126)
Global iterator type is coming: https://github.com/tc39/proposal-iterator-helpers

But a method named `groupBy` on iterators traditionally means a different thing: https://github.com/tc39/proposal-array-grouping/issues/51#is...

Global iterable type it's too late for, since there's many extant iterables in the language and on the web which don't have it in their prototype chain and can't reasonably be changed.

Re:TC39: Add Object.groupBy and Map.groupBy by pharmakom on Wednesday December 20, 2023 @03:09AM (#38706433)
Stop it please! Just add the pipleline proposal and we can use a library or define our own function.
Re:TC39: Add Object.groupBy and Map.groupBy by replygirl on Tuesday December 19, 2023 @05:44PM (#38702951)
it's a noble approach, but feels like it's at its limits if no one could find a reasonable name that works with Array. there's gotta be a point at which it makes sense to EOL third-party stuff like that: set a date far in the future, and any pages where document.lastModified is greater get Array.groupBy as well
Re:TC39: Add Object.groupBy and Map.groupBy by bakkoting on Tuesday December 19, 2023 @05:49PM (#38703025)
Not gonna happen: https://github.com/tc39/faq?tab=readme-ov-file#why-dont-we-j...

Browsers aren't going to break old pages.

Re:TC39: Add Object.groupBy and Map.groupBy by diegof79 on Tuesday December 19, 2023 @08:03PM (#38704256)
Well there is a standard way to support multiple versions of a JS library: imports.

I wonder is there is any proposal for a standard library with a well known URI.

A browser with ESM support should be able to provide its internal version or fallback with a polyfill using import maps. Something like: import {groupBy} from “std:arrays”

That can be also useful to standardize libs between Node,Deno,Bun without polluting the global namespace… and it’s backwards compatible

Re:TC39: Add Object.groupBy and Map.groupBy by Silhouette on Tuesday December 19, 2023 @06:54PM (#38703656)
Browsers have broken old pages on a massive scale on several occasions. Numerous useful pages were effectively lost when they decided to shut down widely used plugins like Flash and Java applets that were mainstays of the interactive web for years. Google/Chrome in particular are notorious for pushing features before they're properly standardised, allowing developers to start relying on them, and then pulling them because they are no longer convenient.

A better answer here might be to give the language a version declaration feature so browsers could reliably provide a backward-compatible interpretation of old scripts if necessary. If breaking changes are only made when there is great value in the change and no reasonable alternative then they shouldn't happen often enough for this kind of versioning to become an unmanageable burden.

Re:TC39: Add Object.groupBy and Map.groupBy by bakkoting on Tuesday December 19, 2023 @07:19PM (#38703882)
There's a big difference between breaking things because they're relying on impossible-to-secure plugins and breaking things because JavaScript developers want to invoke an API in a slightly different way.

Regarding versioning, see the immediately preceding question in the FAQ.

Re:TC39: Add Object.groupBy and Map.groupBy by Silhouette on Tuesday December 19, 2023 @07:44PM (#38704089)
I read both FAQ answers. I just - with greatest respect - don't find them convincing.

The arguments about completely removing the plugins were always questionable. Yes they had security problems. Now the attack surface they represented has largely been moved to the browsers themselves, which also have security problems, often of a similar nature and for similar reasons. Plugins had stopped being a vehicle for drive-by downloads and the like thanks to better browser safeguards like click-to-play long before they were decisively removed.

Some of the most popular programming languages in the world behave significantly differently between major versions and have developed effective solutions to manage those differences. Sometimes those are as simple as a command line compiler flag or config file setting to specify a target language version. And of course the libraries used with many languages also frequently have to deal with complex dependency resolutions these days. I do not understand why JavaScript is special and could not adopt a similar model if the will was there.

Re:TC39: Add Object.groupBy and Map.groupBy by bakkoting on Tuesday December 19, 2023 @08:44PM (#38704504)
Browsers are much, much better at addressing security issues than Flash or Java applets ever were. There's no comparison. And even if you think that they ought not to have been removed, surely it's clear that the concerns about security for users are a fundamentally different thing than JS developers wanting a different API shape.

As to versioning the language: languages like Rust and Go use editions, which allow them to making breaking changes to syntax but not to the standard library, which is what's being discussed here. Indeed Rust has several deprecated-but-unremovable things in their standard library. Python makes breaking changes to their standard library, and then people's code breaks. C++ requires you to specify the version for the entire program, which isn't viable for JS because pages mix scripts from dozens of authors, which all need to interact and to have a coherent view of the world. Not sure what other language you're thinking of.

The relatively unique object model in JS, and the fact that the standard library consists of ambient properties, also makes it special; it's harder in JS than it would be in a language like Rust to detect use of a particular feature of the standard library.

Re:TC39: Add Object.groupBy and Map.groupBy by replygirl on Wednesday December 20, 2023 @07:48AM (#38708022)
yes, what my comment proposes is a solution that avoids breaking the web, by referring to document.lastModified. in this scenario there's a transition period where only Object.groupBy is available, then after a few years Array.groupBy is also available for any pages modified after the cutoff date. static pages are spared, and servers + library devs have plenty of time to react

there is definitely an approach out there that breaks fewer pages than things like the flash EOL broke

Re:TC39: Add Object.groupBy and Map.groupBy by bakkoting on Wednesday December 20, 2023 @02:18PM (#38712459)
A lot of shipped code is effectively unmaintained even on pages which are otherwise being modified, in many cases by relatively non-technical people (a small business owner contracts a webdev to build a page and show them how to update the content, for example).

And in a lot of cases it's not obvious that your code is going to be incompatible. For example the most recent case there was code which was like `function foo(x) { if (x.group) x = x.group; /.../}` where that function was being called with `foo({ group: [...] })` or `foo([...])`. That code breaks if `Array.prototype.group` is added. No one is going to figure that out themselves unless they test on a version of a browser which ships `Array.prototype.group`, and empirically no one does that.

Yes, we could find a way to break fewer pages than flash EOL broke. But even just a handful of pages people actually use is too much. No browser wants to ship an update which will break (to pick a relevant example) the website of the government of Brazil, even if they somehow "ought to have" updated their code before that update. Developers are empirically not going to do that update and browsers are not going to punish the users of the pages of those developers by breaking them.

Re:TC39: Add Object.groupBy and Map.groupBy by LegionMammal978 on Tuesday December 19, 2023 @05:22PM (#38702692)
Map.groupBy() returns a Map with the keys mapped to array values. In parallel, Object.groupBy() returns an Object with the keys cast into property names. Thus, I'd say it fits the pattern of Object-related static methods, since it's using the Object mechanism as a map.
Re:TC39: Add Object.groupBy and Map.groupBy by zeroimpl on Tuesday December 19, 2023 @06:22PM (#38703358)
The part I’m confused on is the documentation saying the returned object from Object.groupBy doesn’t extend from Object.prototype. Is it still an object?

    The return value of groupBy is an object that does not inherit from %Object.prototype%.
Re:TC39: Add Object.groupBy and Map.groupBy by edflsafoiewq on Tuesday December 19, 2023 @06:28PM (#38703420)
It's called a null-prototype object: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Refe...

They're "usually used as a cheap substitute for maps": the lack of prototype prevents the object from being polluted by keys that morally shouldn't be there, like toString, etc.

Re:TC39: Add Object.groupBy and Map.groupBy by aragonite on Tuesday December 19, 2023 @06:44PM (#38703570)
It's a value that belongs as a member of the Object data type (Object(it)===it), but it's not (in addition) an instance of the Object constructor (!(it instanceof Object)). I guess the former, wider sense of 'an object' is more often intended.
Re:TC39: Add Object.groupBy and Map.groupBy by danvk on Tuesday December 19, 2023 @05:15PM (#38702580)
What about Object.fromEntries?

See https://github.com/tc39/proposal-array-grouping for why this isn’t a method on Array.prototype.

Re:TC39: Add Object.groupBy and Map.groupBy by flqn on Tuesday December 19, 2023 @05:18PM (#38702625)
Constructs an object from kv pairs, definitely assoctiated with the "Object" prototype. Groupby is more dubious