-
Notifications
You must be signed in to change notification settings - Fork 30.6k
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 a utility to convert file URL or path to one of them? #41521
Comments
We have a Lines 1558 to 1562 in 38b7961
I don't think there is any reliable way to check if a string is a path or a file URL... I'm afraid the best option here is to assume that a string is a path and call |
Wouldn't it be more natural to have it in |
To be clear, the Example usage: import path from 'node:path';
// Passthrough
path.from('/Users/sindresorhus/dev');
//=> '/Users/sindresorhus/dev'
path.from(new URL('file:///Users/sindresorhus/dev'));
//=> '/Users/sindresorhus/dev' |
Is this what you had in mind in terms of semantics? benjamingr@0704851#diff-6610c8d452ba38a0db8c591e06af555bc7755ace17065a066c853ffcc5271559R7-R20 |
Yes, that's what I want. |
@aduh95 wdyt about an implementation that does that? ( #41521 (comment) ) |
I don't think we'll get consensus landing in core something that treats differently a string depending on if it starts with The workarounds that come to mind are:
Any combination of the above would address my concern, but I'm open to other ideas if someone thinks of something else. |
@aduh95 I think it's safe to say "if this can be an npm module, sindre has already probably published one" :D Making small useful modules kind of their thing. Edit: lol found it https://www.npmjs.com/package/file-url |
Basically: I think the main issue with this being on npm is that file urls and paths are both very common in modern Node.js and not having a utility to convert them in core is weird. I'm sure we can bikeshed the name and semantics as long as the core use case (convert file url to path) is addressed. Would it be better if the name was explicit |
I have no need for supporting What I would like is: export const from = urlOrPath => urlOrPath instanceof URL ? fileURLToPath(urlOrPath) : urlOrPath; import path from 'path';
path.from('/Users/sindresorhus');
//=> '/Users/sindresorhus'
path.from(new URL('file:///Users/sindresorhus'));
//=> '/Users/sindresorhus' |
No, this does not solve the request. |
So you would expect: path.from('file:///Users/sindresorhus');
//=> file:///Users/sindresorhus ? |
Edit: was already mentioned above
So basically you would like Lines 1555 to 1563 in 9caceb2
|
FYI: I have |
@benjamingr Yes |
Yes |
Would it be fine if the name was more explicit like |
I don't think we can be much more explicit than |
There has been no activity on this feature request for 5 months and it is unlikely to be implemented. It will be closed 6 months after the last non-automated comment. For more information on how the project manages feature requests, please consult the feature request management document. |
Please keep this open. |
@MoLow wanna take a look? |
Sure, once I'm back from vacation |
+1 to this, the semantics seems more concise with the rest of the API |
The pull request for this issue was recently closed for lack of response from one of the reviewers. The main open question is how file URL strings ( I would argue they should be either passed through or even throw a user-friendly error. Node.js APIs do not seem to currently handle file URL strings and I don't think they should either: fs.readFileSync('file:///Users/sindresorhus/x.md');
// Error: ENOENT: no such file or directory, open |
Just to clarify, it does handle it but as a relative path (i.e. in your example it tries to read |
This solution would particularly be useful for converting |
You can already use |
Oh, sorry. Got mislead by reading as this issue is still open. |
There has been no activity on this feature request for 5 months and it is unlikely to be implemented. It will be closed 6 months after the last non-automated comment. For more information on how the project manages feature requests, please consult the feature request management document. |
Please keep this open. |
Is this still open to being implemented? I haven't done open source before in a meaningful way but I want to try this if I still can? |
AFAICT this is implemented as @nodejs/path would it be worth it to make |
I would argue that However, I must say I liked this from @sindresorhus, if path could be smart enough to understand both (and accepting URL would be a bonus) this would be awesome but I also think this "breaks" the current syntax, since we have one function that converts from URL to path, but not one that does the opposite. |
The ability for path to understand URL syntax is #52672, but I'm unsure whether that will land. |
There has been no activity on this feature request for 5 months. To help maintain relevant open issues, please add the
never-stale
|
Please keep this open. |
What is the problem this feature will solve?
It's a nice way to accept both
URL
and path string in API, but we need covert it to either string or URL, it would be nice to have a built-in method to do this.What is the feature you are proposing to solve the problem?
For converting to string:
Maybe add
path.from(URL | string)
? @sindresorhus proposed here.For converting to URL:
url.from(URL | string)
I'm not sure, since
URL
is a standard, I guess adding methodURL
object won't be acceptable, maybe tonode:url
module?What alternatives have you considered?
No response
The text was updated successfully, but these errors were encountered: