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

Add std/url with URL utilities #3515

Closed
ry opened this issue Aug 3, 2023 · 12 comments · Fixed by #3527
Closed

Add std/url with URL utilities #3515

ry opened this issue Aug 3, 2023 · 12 comments · Fixed by #3527

Comments

@ry
Copy link
Member

ry commented Aug 3, 2023

Should have similar functionality to path: parent, basename, ext

but operate on JS native URL objects

@lino-levan
Copy link
Contributor

Good to know that my analysis was not completely off-base 😅

@tr1ckydev
Copy link
Contributor

tr1ckydev commented Aug 7, 2023

<URL>.toString() allows itself to be used in std/path.

index.ts

import { parse } from "https://deno.land/std/path/mod.ts";

const url = new URL("https://deno.land/std@0.197.0/console/mod.ts");

console.log(parse(url.toString()));

console output

$ deno run index.ts
{
  root: "",
  dir: "https://deno.land/std@0.197.0/console",
  base: "mod.ts",
  ext: ".ts",
  name: "mod"
}

@kt3k
Copy link
Member

kt3k commented Aug 8, 2023

@tr1ckydev If the platform is windows, std/path doesn't work for urls.

import { join } from "https://deno.land/std/path/win32.ts";
const url = new URL("https://deno.land/std@0.197.0/console/mod.ts");
console.log(join(url.toString(), "foo")); // => https:\deno.land\std@0.197.0\console\mod.ts\foo

@tr1ckydev
Copy link
Contributor

tr1ckydev commented Aug 8, 2023

Ahh, I see. Is std/url being currently worked upon or I can work on it and send a pr? ( I have already started working on it)

@kt3k
Copy link
Member

kt3k commented Aug 8, 2023

I don't think anybody started working on it. Which APIs are you planning to implement? Could you share the API designs first?

@tr1ckydev
Copy link
Contributor

tr1ckydev commented Aug 8, 2023

I have tried to keep the API design similar to std/path with only difference being the input for the functions are string | URL and functions will always return as an instance of URL object wherever possible.
Currently I have implemented basename, dirname, extname, join, normalize, parse, format, sep.
Only relative and resolve is pending.

@tr1ckydev
Copy link
Contributor

relative has been implemented but I am skeptical if resolve should be implemented in case of urls?

Also, I have started writing the tests for the functions, and they are working very promising so far.

@lino-levan
Copy link
Contributor

You should be able to copy most if not all of the code from the posixX family of std/path. I would make sure to test edgecases though. Does implementing sep make any sense for URLs? What about format?

IMO, the only useful utilities would be xname, resolve, and maybe normalize. I'm not sure about parse. I think we should definitely NOT implement relative as we should be encouraging people to use the base URL class:

const url = new URL("../fr", "https://test.com/en-us")

As for join, as far as I can tell, the only difference between join and resolve is whether the path has to be absolute or not. For URLs, the path has to be absolute, so I would recommend either changing the behavior of join to resolve (to keep the name) or potentially just not including join at all in favor of resolve.

@tr1ckydev
Copy link
Contributor

You should be able to copy most if not all of the code from the posixX family of std/path. I would make sure to test edgecases though.

Yes, I am using posix functions from std/path.

Does implementing sep make any sense for URLs?

Here's something to say. I have implemented sep as a function that can break https://github.com/denoland/deno_std to [ "https://github.com", "denoland", "deno_std" ] and SEP is a const of value "/"

What about format?

format just takes Partial<ParsedPath> as input and joins them.
parse parses the url into ParsedPath.

I think we should definitely NOT implement relative as we should be encouraging people to use the base URL class:

Agreed.

so I would recommend either changing the behavior of join to resolve (to keep the name) or potentially just not including join at all in favor of resolve.

How about resolve being an alias for join or vice-versa?

@lino-levan
Copy link
Contributor

Here's something to say. I have implemented sep as a function that can break https://github.com/denoland/deno_std to [ "https://github.com", "denoland", "deno_std" ] and SEP is a const of value "/"

Is there a usecase for this? I feel like we should be pretty conservative about what we include in std.

format just takes Partial as input and joins them.
parse parses the url into ParsedPath.

Again, see above.

How about resolve being an alias for join or vice-versa?

Pretty strongly against this. I think resolve is unclear enough already. I think join should be fine.

@tr1ckydev
Copy link
Contributor

Is there a usecase for this? I feel like we should be pretty conservative about what we include in std.

Okay, I get it. Then they can be omitted.

Pretty strongly against this. I think resolve is unclear enough already. I think join should be fine.

Sounds great. I will be keeping join only then.

@jollytoad
Copy link
Contributor

I really like the idea of this module, as I often use the posix path fns for this, but having these use the existing path modules (v0.198.0) introduces unnecessary overhead from the windows implementations.

I've proposed a modification of the path modules structure that could alleviate this... https://github.com/denoland/deno_std/discussions/3553

@kt3k kt3k closed this as completed in #3527 Sep 1, 2023
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

Successfully merging a pull request may close this issue.

5 participants