News for hackers. Stuff that matters.
Re:Cash: An absurdly small jQuery alternative for modern bro... by mg on Saturday November 02, 2024 @02:13PM (#42028095)
Browsers have become so nice to work with, that these days, I get away with just the following two lines of code to simplify DOM manipulation:

    dqs  = document.querySelector.bind(document);
    dqsA = document.querySelectorAll.bind(document);
So instead of

    country = document.querySelector('#country');
    cities  = document.querySelectorAll('.city');
I can write

    country = dqs('#country');
    cities  = dqsA('.city');
For everything else, I am fine with just using the native browser functions.

I usually import the two functions from a module like this:

import { dqs, dqsA } from '/lib/js/dqs.js';

This is the module:

https://github.com/no-gravity/dqs.js

Re:Cash: An absurdly small jQuery alternative for modern bro... by wmanley on Saturday November 02, 2024 @04:04PM (#42028888)
> Browsers have become so nice to work with, that these days, I get away with just the following two lines of code to simplify DOM manipulation: > > dqs = document.querySelector.bind(document); > dqsA = document.querySelectorAll.bind(document);

Sounds useful and reasonable.

> I usually import the two functions from a module like this: > > import { dqs, dqsA } from '/lib/js/dqs.js';

Utterly absurd. Just copy and paste. It’s only two simple lines, how could it be worth a dependency?

Re:Cash: An absurdly small jQuery alternative for modern bro... by brightball on Saturday November 02, 2024 @04:20PM (#42028977)
Included in multiple places?
Re:Cash: An absurdly small jQuery alternative for modern bro... by 0xCMP on Saturday November 02, 2024 @04:26PM (#42029011)
modern browsers support the import syntax natively, so it really shouldn't be a lot of overhead to import it.
Re:Cash: An absurdly small jQuery alternative for modern bro... by notpushkin on Sunday November 03, 2024 @12:35AM (#42031194)
The overhead here would be the need to make another request just for these two functions.

On the other hand, with bundling though it’s totally fine to have a module just for these two helpers. (Even better if it can be inlined, but I haven’t seen anything supporting this since Prepack, which is still POC I think.)

Re:Cash: An absurdly small jQuery alternative for modern bro... by mg on Sunday November 03, 2024 @06:03AM (#42032373)
AFAIK modern HTTP versions like HTTP/3 can request multiple files in a single network packet. So it is basically free to do "another request". As the data request goes out and the data comes in in packets with other "requests".
Re:Cash: An absurdly small jQuery alternative for modern bro... by theandrewbailey on Sunday November 03, 2024 @07:51AM (#42032766)
A network request isn't free, only less costly than it used to be. Even with HTTP 3, your JS execution is stalled for however long the RTT is back to the server. That could be 500+ ms if its on the side of the world and doesn't have a CDN.
Re:Cash: An absurdly small jQuery alternative for modern bro... by mg on Sunday November 03, 2024 @10:08AM (#42033463)
Depends on the import tree. The way I understand it, this:

    import { x } from '/a.js';
    import { y } from '/b.js';
Does not take longer than this:

    import { x } from '/a.js';
Because the message to the server "Give me b.js" goes out in the same network packet as "Give me a.js" and the data of b.js comes back in the same packet(s) as the data of a.js.
Re:Cash: An absurdly small jQuery alternative for modern bro... by Cyphase on Saturday November 02, 2024 @05:39PM (#42029403)
It looks like it's intended to be copied and pasted into your codebase, not be an external dependency.
Re:Cash: An absurdly small jQuery alternative for modern bro... by 0x457 on Saturday November 02, 2024 @05:52PM (#42029462)
I guess it's meant to be processed by some bundler later.
Re:Cash: An absurdly small jQuery alternative for modern bro... by kfajdsl on Saturday November 02, 2024 @06:20PM (#42029623)
/lib/js/dq.js is part of their codebase.
Re:Cash: An absurdly small jQuery alternative for modern bro... by Kiro on Saturday November 02, 2024 @07:05PM (#42029891)
Bizarre comment. Why would you copypaste this into every file when you can do it once and import it? What's the problem exactly?
Re:Cash: An absurdly small jQuery alternative for modern bro... by croes on Saturday November 02, 2024 @11:44PM (#42031017)
Left-pad anyone?
Re:Cash: An absurdly small jQuery alternative for modern bro... by sadeshmukh on Sunday November 03, 2024 @01:10AM (#42031426)
It's in their codebase.
Re:Cash: An absurdly small jQuery alternative for modern bro... by Kiro on Sunday November 03, 2024 @03:06AM (#42031778)
That's an external dependency. You don't install anything here. It's no different than making any other module you reuse in multiple places.
Re:Cash: An absurdly small jQuery alternative for modern bro... by eastbound on Sunday November 03, 2024 @06:50AM (#42032529)
Because using a JS module will set back your 30-minutes project by an entire day.

Of course you are supposed to master which of the es/interop/amd/require incantation you are supposed to use. I wish Typescript would have mandated one style and one style of JS module only!!

And I’ve never succeeded to find a good guidelines on which kind of JS module I should use. Any advice on what is a very easy and stable and worth-to-learn technique to master imports in 2024?

Re:Cash: An absurdly small jQuery alternative for modern bro... by genuinelydang on Sunday November 03, 2024 @08:31AM (#42032967)
What? If you don’t have external dependencies, just remove your bundler/transpiler and rely on browsers to import your code.
Re:Cash: An absurdly small jQuery alternative for modern bro... by afavour on Sunday November 03, 2024 @08:37AM (#42032995)
Your rant is several years out of date. You can use ES imports natively in Node and the browser. I have been very happy doing so.

Besides, if you’re working with a codebase of non-zero complexity you need imports/require/whatever anyway.

Re:Cash: An absurdly small jQuery alternative for modern bro... by jazzypants on Sunday November 03, 2024 @09:35AM (#42033276)
Use ESM. It's built into the browser. CJS is legacy. Every major JS runtime has module interop built in now.

Modules aren't hard anymore.

Re:Cash: An absurdly small jQuery alternative for modern bro... by nsonha on Sunday November 03, 2024 @09:13PM (#42037791)
> Because using (a JS) module will set back your 30-minutes project by an entire day

May be learn to do the basic of YOUR JOB for once, some "software engineer".

Re:Cash: An absurdly small jQuery alternative for modern bro... by WorldMaker on Monday November 04, 2024 @02:17PM (#42045063)
In 2024 the one and only answer is finally just ESM.

If you need to support the browser use `<script type="module">`. That works natively in every current browser today. You may need an "importmap" for more easily handling dependencies. You may need to spot build with something like esbuild or rollup or rolldown for some of your dependencies if they were written in CJS or to add missing things a browser needs like ".js" file extensions.

If you need to support Node use the package.json incantation `"type": "module"` and the `"exports"` key instead of `"main"` and get sane modern defaults in all LTS supported versions of Node (and a few past versions now, too). Most imports will "just work", your module files will be sensibly named ".js". If you publish a library, most people can consume it, even if some of them (CJS stalwarts/people trapped in legacy swamps) complain about having to work with Promises some of the time.

If you need to support Deno or Bun, they already have sane defaults and their documentation guides you pretty well, including Typescript setup.

> I wish Typescript would have mandated one style and one style of JS module only!!

The good news it that they kind of did: the import/export syntax that Typescript has made familiar since around TS 1.0/1.5 is "surprise" ESM syntax. Typescript has been preparing developers into using it all along. You can stop cross-compiling the ESM you've already become used to writing to older, worse formats like CJS and AMD, you can let Typescript do a lot less work on your behalf and just "strip types" more than "transpile".

Re:Cash: An absurdly small jQuery alternative for modern bro... by mrweasel on Sunday November 03, 2024 @10:44AM (#42033685)
Every file? You have one .js file per project, if you're like me. So just throwing those two lines in the top and never having to worry about it ever again seems like a nice option.
Re:Cash: An absurdly small jQuery alternative for modern bro... by NoahKAndrews on Sunday November 03, 2024 @06:57PM (#42037155)
How big are your projects? It's very strange to me that you would want to have just a single JS file per project. Even if you want to avoid bundlers, ES modules make it easy to import code from other files.
Re:Cash: An absurdly small jQuery alternative for modern bro... by mrweasel on Monday November 04, 2024 @03:39AM (#42039675)
> How big are your projects?

In terms of Javascript, as little as I can possibly get away with. The web stuff that I do is mostly CRUD type apps, which can be done entirely server side. The Javascript is only where it make the user experience better, so basic form help or to do a modal, things like that.

Re:Cash: An absurdly small jQuery alternative for modern bro... by niutech on Friday November 15, 2024 @04:04AM (#42145129)
Importing an ES module without a bundler involves making a HTTP request, which costs time.

Better KISS and fit all of JS in a single script.

Re:Cash: An absurdly small jQuery alternative for modern bro... by mg on Friday November 15, 2024 @05:23AM (#42145551)

    which costs time
No sure this holds true anymore.

When you go to a site like reddit.com which uses HTTP/2, you see a gazillion of requests for JS files, many starting at the same time and ending at the same time.

I think that is because newer HTTP versions can put multiple requests in the same data packet. And even HTTP/3 is now supported by over 90% of browsers.

Re:Cash: An absurdly small jQuery alternative for modern bro... by fragmede on Saturday November 02, 2024 @08:55PM (#42030415)
Because if it ever needs to change, you're in for a world of hurt. Because useful stuff like that is worth sharing elsewhere. It starts with 2 lines, but then theres another useful function you'd like in another file. So you just copy and paste those two lines. But then you want that in a third file. Pretty soon you have this almost-library you're carrying around you've spread across a bunch of files, and now what started with two simple lines is now a mountain of tech debt.

Maybe you'll never write enough JavaScript to have additional utility functions. You'll probably never need to modify those two lines. But copying and pasting like that makes for quite the code smell. Because if you're copying and pasting that, the question that someone may never actually verbalize to you is what else in the code is copy and pasting instead of being turned into a shared function in a libray?

Re:Cash: An absurdly small jQuery alternative for modern bro... by nashashmi on Saturday November 02, 2024 @02:55PM (#42028369)
I really wish it was a native script to use qs and qsa, rather than something I have to add.

FYI: I know you meant to give an example, but element tags with ID are DOM variables as well.

Re:Cash: An absurdly small jQuery alternative for modern bro... by kccqzy on Saturday November 02, 2024 @06:00PM (#42029503)
I recently attempted to remove React as a dependency just to see what would happen. It turns out different browsers are still incredibly inconsistent when it comes to event handling. For example the select event on an <input> element somehow doesn't fire at all on Safari during my test, and doesn't fire when the caret is merely moved on some browsers. Using just the native browser functions isn't just fine, even if you don't need all the React features like components or state or props. It turns out React DOM is valuable as it papers over browser differences.
Re:Cash: An absurdly small jQuery alternative for modern bro... by divbzero on Saturday November 02, 2024 @06:11PM (#42029567)
I haven’t tested myself, but according to MDN the select event on <input> elements should be supported by Safari?

https://developer.mozilla.org/en-US/docs/Web/API/HTMLInputEl...

Re:Cash: An absurdly small jQuery alternative for modern bro... by kccqzy on Saturday November 02, 2024 @06:21PM (#42029627)
The MDN page you linked to includes a nice selection logger example. It just doesn't work on my Safari (iOS).
Re:Cash: An absurdly small jQuery alternative for modern bro... by chrismorgan on Saturday November 02, 2024 @10:48PM (#42030858)
And does an equivalent in React work? Because I don't believe React does any of the papering-over you describe. My understanding (as a non-user) is that React does, logically, essentially nothing special around event handling.
Re:Cash: An absurdly small jQuery alternative for modern bro... by kccqzy on Saturday November 02, 2024 @11:23PM (#42030954)
I don't remember off the top of my head whether this specific example works in React as I'm not next to a computer. But I remember reading React source code and finding a whole lot of code to handle the select event. (Just found it by doing a GitHub code search on my phone https://github.com/facebook/react/blob/7c8e5e7ab8bb63de91163...)

In general React has its own event handling code. For one in React the user doesn't even deal with the browser native DOM events but React synthetic events. React also readily creates brand new synthetic events from other browser events. React also sometimes gives different names or behaviors to browser events; the most famous example is that the React onChange event is roughly equivalent to the browser onInput event, but absolutely different from the browser onChange event.

Re:Cash: An absurdly small jQuery alternative for modern bro... by chrismorgan on Saturday November 02, 2024 @11:48PM (#42031027)
Good to know, thanks. I knew it made synthetic events, but thought it was all still 1:1. I see I was completely wrong. I gotta say, yuck. Don't like it, wish they'd taken a more polyfill-like approach.
Re:Cash: An absurdly small jQuery alternative for modern bro... by Izkata on Sunday November 03, 2024 @01:37AM (#42031489)
IIRC they do that to deal with browser differences and be consistent with things like event bubbling. Probably other benefits as well but that's the one I'm fairly sure I remember from years ago.
Re:Cash: An absurdly small jQuery alternative for modern bro... by fiddlerwoaroof on Saturday November 02, 2024 @11:25PM (#42030959)
There are a couple of edge cases I forget at the moment where react event handlers intentionally behave differently from the DOM handlers with the same name.
Re:Cash: An absurdly small jQuery alternative for modern bro... by theandrewbailey on Saturday November 02, 2024 @08:41PM (#42030355)
Re:Cash: An absurdly small jQuery alternative for modern bro... by niutech on Friday November 15, 2024 @04:16AM (#42145198)
Bling.js is just $() and .on(), Cash is more than bling.
Re:Cash: An absurdly small jQuery alternative for modern bro... by Cyphase on Monday November 18, 2024 @08:19PM (#42179204)
And you might need Cash before you can have bling.
Re:Cash: An absurdly small jQuery alternative for modern bro... by namanyayg on Sunday November 03, 2024 @12:20AM (#42031137)
This is my favorite trick that I've been using for a long time

...I just checked and it turns out I first blogged[0] about it 12 years ago. Time flies.

[0] https://nmn.gl/blog/javascript-shortcut-for-getelementbyid-a...

Re:Cash: An absurdly small jQuery alternative for modern bro... by cr125rider on Sunday November 03, 2024 @08:19AM (#42032913)
Your “ct” ligatures in your headings have a fun little loop connecting them!
Re:Cash: An absurdly small jQuery alternative for modern bro... by namanyayg on Sunday November 03, 2024 @01:11PM (#42034825)
Haha I'm glad you noticed :) I'm a huge typography nerd.

The css to do that comes from the Normalize-OpenType.css [0] library

[0] https://kennethormandy.com/journal/normalize-opentype-css/

Re:Cash: An absurdly small jQuery alternative for modern bro... by efilife on Monday November 04, 2024 @08:10PM (#42047733)
> Just look at it’s size!

Should have been its. Now you are aware of a 12 year old typo

Re:Cash: An absurdly small jQuery alternative for modern bro... by gardenhedge on Sunday November 03, 2024 @05:20AM (#42032233)
How do people you work with recieve this? I'd imagine it could get messy6if every dev has their own little things like this
Re:Cash: An absurdly small jQuery alternative for modern bro... by nedt on Sunday November 03, 2024 @06:40AM (#42032496)
querySelectorAll() isn't live. So you could do what I very often do and already convert the result to an array, i.e.

  dqsA = s => Array.from(document.querySelectorAll(s));
Reason why I do that very often is because it allows all array methods to be used on the result, like .map() or .filter(), which makes it feel very much like jQuery. YMMV
Re:Cash: An absurdly small jQuery alternative for modern bro... by theandrewbailey on Sunday November 03, 2024 @07:54AM (#42032783)

    NodeList.prototype.__proto__ = Array.prototype;
Problem solved without Array.from().
Re:Cash: An absurdly small jQuery alternative for modern bro... by cr125rider on Sunday November 03, 2024 @08:16AM (#42032898)
Does the underlying data structure work okay with that? I would assume there is some sort of lazy iterator involved that may not work with array methods, or only work once.

This is JavaScript though…

Re:Cash: An absurdly small jQuery alternative for modern bro... by jazzypants on Sunday November 03, 2024 @09:36AM (#42033285)
Prototype pollution is bad. We learned this over a decade ago.
Re:Cash: An absurdly small jQuery alternative for modern bro... by mg on Sunday November 03, 2024 @09:51AM (#42033366)
Good point!

I wonder what I would have to look for in my codebase in terms of what could break when dqsA starts returning an array instead of a NodeList?

Re:Cash: An absurdly small jQuery alternative for modern bro... by WorldMaker on Monday November 04, 2024 @02:35PM (#42045245)
The versions of those methods (map/filter/reduce/etc) that support any iterator (including upgrading NodeList "for free") have passed Stage 4 of the process, which means they will be in the next version of the standard and already starting to show up in some browsers.

https://github.com/tc39/proposal-iterator-helpers

Re:Cash: An absurdly small jQuery alternative for modern bro... by sekLabs on Sunday November 03, 2024 @12:10PM (#42034315)
you can use `$(queryGoesHere)` or `$$(queryGoesHere)` too from the devtool console.
Re:Cash: An absurdly small jQuery alternative for modern bro... by niutech on Friday November 15, 2024 @04:12AM (#42145174)
Not every utility function is a one-liner. For example $.fn.one() or $.fn.on() with multiple events is easier to write with jQuery/Cash. Have a look under the hood: https://github.com/fabiospampinato/cash/blob/master/src/even...