url icon indicating copy to clipboard operation
url copied to clipboard

Invoking serializer "exclude fragment" from API

Open marcoscaceres opened this issue 4 years ago • 2 comments

This is a bit of a long shot, but I figured I'd ask...

I found myself needing to serialize a URL via the API, but with "exclude fragment" being true.

I'm wondering, could we overload the serializer to accept {excludeFragment: true}... imagine something like:

const str = url.toString({ excludeFragment: true });
const json = url.toJSON({ excludeFragment: true });

It's a bit unconventional, but taking the optional argument would be nice for completeness.

marcoscaceres avatar Aug 11 '21 01:08 marcoscaceres

This technically isn't supported by Web IDL at the moment. Could you instead do

function toStringWithoutFragment(url) {
  const u2 = new URL(url);
  u2.hash = '';
  return String(u2);
}

TimothyGu avatar Aug 11 '21 16:08 TimothyGu

Yeah, that was literally what I was doing (and why I ended up here). Having to create a new URL is not amazing, but it's not the end of the world either.

marcoscaceres avatar Aug 12 '21 01:08 marcoscaceres