Live data from Hacker News

JavaScript: Search and Don’t Replace (2008)

johnresig.com

11–20 of 46 posts

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

#11
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("&");

Things sure have changed in the last 12 years! The only method you're using that existed back then is join. Wild.

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

#13
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("&");

If you read the article he makes a point about not using intermediate arrays and .join()

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

#15

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

the song of re̸gular exp ression parsing will exti nguish the voices of mor tal man from the sp here

https://stackoverflow.com/questions/1732348/regex-match-open...

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

#16

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

> He wrapped himself in quotations - as a beggar would enfold himself in the purple of Emperors.

Kipling

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

#17
post #12

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

This was a time when there was widespread belief that iterating over an array was best done with a library.

I know it's weird, but that's what $.each existed for. for ... of did not exist.

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

#18
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.

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

#19
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("&");

If you read the article he makes a point about not using intermediate arrays and .join()

That's a good point, but my post didn't have any grand point :)

I was hoping other people to come up modern solutions to this same original problem.

Even my solution is questionable, because it relies on generating new URLSearchParams with strings, if one wants to be secure the reduce should take URLSearchParams as accumulator and add the items there.

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

#20
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("&");

If you read the article he makes a point about not using intermediate arrays and .join()

Is creating an array really more costly than altering a string? I think it may depend on how much condensing of params you expect compared to singular params. The things being optimized for also may have changed over the intervening decade. That's the problem with optimizing something without respect for it's intended common workload, or expecting those justifications to hold over time in all cases.

Edit: Woah, made a hash of that before going in and fixing all the typos from my phone keyboard.

Post reply on HN