Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor? #1

Open
pdehaan opened this issue Jul 3, 2019 · 0 comments
Open

Refactor? #1

pdehaan opened this issue Jul 3, 2019 · 0 comments

Comments

@pdehaan
Copy link
Owner

pdehaan commented Jul 3, 2019

Looks like you can probably just do something like this:

const foo =
  "https://prettier.io/docs/en/options.html?foo=true&utm_foo=true&utm_bar=false&bar=false&x=7";

console.log(deUtm(foo));

function deUtm(href) {
  const url = new URL(href);
  for (const key of [...url.searchParams.keys()]) {
    key.startsWith("utm_") && url.searchParams.delete(key);
  }
  return url.href;
}
$ node de-utm.js
# INPUT:  https://prettier.io/docs/en/options.html?foo=true&utm_foo=true&utm_bar=false&bar=false&x=7
# OUTPUT: https://prettier.io/docs/en/options.html?foo=true&bar=false&x=7

Probably easiest/fastest to do in a single for..of loop vs trying to do a less efficient .filter().forEach() double loop, a la:

// WARNING: SUBOPTIMAL DOUBLE LOOP
const url = new URL(href);
[...url.searchParams.keys()]
	.filter(key => key.startsWith("utm_"))
    .forEach(key => url.searchParams.delete(key));
return url.href;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant