sindresorhus / normalize-url

Normalize a URL
MIT License
840 stars 122 forks source link

`removeQueryParameters` fails in Firefox content scripts: Error `URLSearchParams.keys()` is not a method #167

Closed akindyakov closed 2 years ago

akindyakov commented 2 years ago

The option removeQueryParameters of normalizeUrl with a list of RegExps raises an error of "Error: o.searchParams.keys() is not iterable" only in Firefox content scripts.

I compile code from TypeScript, the code snipet with usage of normalizeUrl is following:

normalizeUrl('https://github.com/sindresorhus/normalize-url/issues/', {
   forceHttps: true,
   normalizeProtocol: true,
   removeTrailingSlash: true,
   removeQueryParameters: [/^utm_\w+/i, /^itm_\w+/i],
   sortQueryParameters: true,
   stripAuthentication: true,
   stripHash: true,
   stripProtocol: false,
   stripTextFragment: true,
   stripWWW: true,
})
sindresorhus commented 2 years ago

This has nothing to do specifically with this package. It's either a TS config issue or a Firefox issue.

akindyakov commented 2 years ago

This has nothing to do specifically with this package. It's either a TS config issue or a Firefox issue

Thanks for the update!

Do you think it doesn't make sense to use .forEach instead? It would be more transferable and even cheaper for URLs with many query params -

I can make a PR to fix it. It can be something like this:

  if (Array.isArray(removeQueryParameters)) {
    const keysToDelete: string[] = []
    urlObject.searchParams.forEach((_value: string, key: string) => {
      if (testParameter(key, removeQueryParameters)) {
        keysToDelete.push(key)
      }
    })
    keysToDelete.forEach((key: string) => urlObject.searchParams.delete(key))
  }

What do you think @sindresorhus ?

sindresorhus commented 2 years ago

Do you think it doesn't make sense to use .forEach instead?

I'm not interested in using .forEach. I prefer for-of.

akindyakov commented 2 years ago

Sure, thanks for the information then!