Skip to content

Refactor? #1

Open
Open
@pdehaan

Description

@pdehaan

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;

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions