Live data from Hacker News

JavaScript: Search and Don’t Replace (2008)

johnresig.com

21–30 of 46 posts

Re: JavaScript: Search and Don’t Replace (2008)

#21
post #9

I would have come up with something like this: Object.entries(Array.from(new URLSearchParams("foo=1&foo=2&foo=3&blah=a&blah=b").entries()).reduce((a,[k,v]) => ({ ...a, [k]: [...a[k] ?? [], v] }), {})).map(([key, values]) => `${key}=${values.join(",")}`).join("&");

I don't think this was posted as "I would submit a PR with this code for prod" but more of a "here's a cool one-liner for fun" approach.

I enjoy reading people's off-the-cuff code golf stuff and usually learn something.

Re: JavaScript: Search and Don’t Replace (2008)

#22

Earlier quoted context omitted.

This is a funny quote. I really hope it doesn't keep people from learning regular expressions, which are actually not as hard as they are often portrayed and extremely powerful and efficient, as demonstrated in this post.

Sure it's powerful and a lot of them are easy. But there's a lot of indecipherable regexes in the wild. I guess you need to learn them anyway, but I'm very strict about using them in production

This is probably an ideal case though. Regular expressions are a part of the JavaScript standard, so there's no excuse for someone that programs in JavaScript to not know at least the basics, and this is almost as basic as you can get.

The alternative is what, a simple tokenizing parser? I think that's actually a step squarely into territory of making it more complex and less readable than a simple regular expression is.

Re: JavaScript: Search and Don’t Replace (2008)

#23
post #12

Just use match instead? Why use replace and hack into an array? https://developer.mozilla.org/en-US/docs/Web/JavaScript/Refe...

To not create a intermediate array I think

This works fine:

[...paragraph.matchAll(regex)].reduce((q,[kv, key, value]) => { q[key] = (q[key] ? q[key] + `,`: ``) + value return q }, {})

https://pastebin.com/yb5QBCm6

Re: JavaScript: Search and Don’t Replace (2008)

#24
We probably should be cautious reading anything about JS optimization that's 12 years old. It might have been true at the time, but now with the highly-optimized V8 engine, and a set of native methods that didn't exist back then, there's a pretty good chance there's another way to do this that's even faster.

Re: JavaScript: Search and Don’t Replace (2008)

#25

> Some people, when confronted with a problem, think "I know, I'll use regular expressions." Now they have two problems. Jamie Zawinski

I'd normally bristle against RegExp hate (unless the RegExp is complex), but in this case, please, just use `URLSearchParams`.

It's well-adopted (unless you have to support IE: https://caniuse.com/?search=URLSearchParams>)

Re: JavaScript: Search and Don’t Replace (2008)

#26
post #12

Just use match instead? Why use replace and hack into an array? https://developer.mozilla.org/en-US/docs/Web/JavaScript/Refe...

.match() only returns the result of one match, rather than all the matches in the string. To do that, you'd need to use .matchAll() (which didn't exist in 2008), or write an awkward do…while loop. Using .replace() looks neater.

Re: JavaScript: Search and Don’t Replace (2008)

#27
post #23

Earlier quoted context omitted.

To not create a intermediate array I think

This works fine: [...paragraph.matchAll(regex)].reduce((q,[kv, key, value]) => { q[key] = (q[key] ? q[key] + `,`: ``) + value return q }, {}) https://pastebin.com/yb5QBCm6

It does, but it still creates an intermediate array which is what the parent comment was suggesting the use of `replace` worked around.

With that said, there could easily be an array being iterated under the hood with the `replace` method anyway.

Re: JavaScript: Search and Don’t Replace (2008)

#28
post #2

Please give me an 80 kloc project full of this kind of smartness, preferably minefied.

look at jQuery... if you have never read the source for it, as a developer, you are doing yourself an injustice. not only has john resig worked on it, but also yehuda katz, so there are some really smart people who coded it to learn from.

In my opinion, having read parts of jQuery's source code on various occasions… You should only read it for entertainment value, it makes for fascinating brain teasers. Here's one snippet I had the opportunity to read recently:

  contains = hasCompare || rnative.test( docElem.contains ) ?
    function( a, b ) {
      var adown = a.nodeType === 9 ? a.documentElement : a,
        bup = b && b.parentNode;
      return a === bup || !!( bup && bup.nodeType === 1 && (
        adown.contains ?
          adown.contains( bup ) :
          a.compareDocumentPosition && a.compareDocumentPosition( bup ) & 16
      ));
    } :
    function( a, b ) {
      if ( b ) {
        while ( (b = b.parentNode) ) {
          if ( b === a ) {
            return true;
          }
        }
      }
      return false;
    };

Re: JavaScript: Search and Don’t Replace (2008)

#30

Earlier quoted context omitted.

look at jQuery... if you have never read the source for it, as a developer, you are doing yourself an injustice. not only has john resig worked on it, but also yehuda katz, so there are some really smart people who coded it to learn from.

In my opinion, having read parts of jQuery's source code on various occasions… You should only read it for entertainment value, it makes for fascinating brain teasers. Here's one snippet I had the opportunity to read recently: contains = hasCompare || rnative.test( docElem.contains ) ? function( a, b ) { var adown = a.nodeType === 9 ? a.documentElement : a, bup = b && b.parentNode; return a === bup || !!( bup && bup.…

i have no doubts. i haven't read through the source in years, but i'm sure there are tons of wtfs in there. those dudes had to do some pretty effed up stuff in order to make it work, and keep working, with the slew of browsers and incompatibilities back then.
Post reply on HN