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

feat(path/unstable): support URL input in dirname() #5747

Merged
merged 4 commits into from
Aug 21, 2024

Conversation

kt3k
Copy link
Member

@kt3k kt3k commented Aug 20, 2024

part of #5537

This PR adds dirname(url: URL): string overload to dirname function.

If a user have a file URL string or object, currently they need to convert it to a path string first to perform path operation on it:

import { dirname, fromFileUrl } from "@std/path";

dirname(fromFileUrl(new URL(import.meta.url)));

This change makes it simpler by automatically interpret the file URL as path

import { dirname } from "@std/path";

dirname(new URL(import.meta.url));

@github-actions github-actions bot added the path label Aug 20, 2024
Copy link

codecov bot commented Aug 20, 2024

Codecov Report

Attention: Patch coverage is 92.30769% with 1 line in your changes missing coverage. Please review.

Project coverage is 96.27%. Comparing base (99200dc) to head (11813f9).
Report is 4 commits behind head on main.

Files Patch % Lines
path/dirname.ts 66.66% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #5747      +/-   ##
==========================================
- Coverage   96.28%   96.27%   -0.01%     
==========================================
  Files         474      474              
  Lines       38383    38391       +8     
  Branches     5578     5584       +6     
==========================================
+ Hits        36957    36962       +5     
- Misses       1384     1387       +3     
  Partials       42       42              

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@iuioiua iuioiua changed the title feat(path/unstable): add dirname(url) feat(path/unstable): support URL input in dirname() Aug 21, 2024
Copy link
Contributor

@iuioiua iuioiua left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM once assertThrows() comments are addressed.

This PR does make me think; what if someone does want to get the parent path of a HTTP URL? E.g. I want to get /documents from https://example.com/documents/image.png. How do I do that? Is that regarded as the directory in a HTTP URL? I think it's a valid use case and we should cater to it.

path/dirname_test.ts Outdated Show resolved Hide resolved
path/dirname_test.ts Outdated Show resolved Hide resolved
@kt3k
Copy link
Member Author

kt3k commented Aug 21, 2024

pathname part of URL object is usually a posix path. I think we can recommend using posix.dirname to it:

> posix.dirname(new URL("https://example.com/documents/image.png").pathname)
"/documents"

@kt3k kt3k merged commit 8e96c19 into denoland:main Aug 21, 2024
16 checks passed
@kt3k kt3k deleted the path-dirname-url branch August 21, 2024 05:29
@iuioiua
Copy link
Contributor

iuioiua commented Aug 21, 2024

Sorry, I want to understand; is there a reason for only allowing dirname() to only work on file URLs?

pathname part of URL object is usually a posix path. I think we can recommend using posix.dirname to it:

> posix.dirname(new URL("https://example.com/documents/image.png").pathname)
"/documents"

I think your example should work without having to call .pathname.

@kt3k
Copy link
Member Author

kt3k commented Aug 21, 2024

If we simply allow http urls, what happens with the call like posix.dirname(new URL("https://example.com"))?

@iuioiua
Copy link
Contributor

iuioiua commented Aug 21, 2024

We could throw, return null or return /.

@kt3k
Copy link
Member Author

kt3k commented Aug 21, 2024

http URLs are not paths while file URLs are equivalent of paths. I think that kind of handling is out of scope of @std/path

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants