Skip to content
This repository has been archived by the owner on Apr 22, 2023. It is now read-only.

path.split() needed, as counterpart to path.join() #1224

Closed
aseemk opened this issue Jun 24, 2011 · 14 comments
Closed

path.split() needed, as counterpart to path.join() #1224

aseemk opened this issue Jun 24, 2011 · 14 comments

Comments

@aseemk
Copy link

aseemk commented Jun 24, 2011

I have a path like "/Users/aseemk/Projects/Foo", and I'd like to break it apart into components. There needs to be a standard path.split() for me to do so; splitting on '/' won't be portable to Windows.

@isaacs
Copy link

isaacs commented Jun 24, 2011

The tricky bit is that windows paths are more like urls than posix paths. For instance, C:foo\bar is valid, and different from C:\foo\bar. (The first is relative to the current working directory on the C: device. Each drive has its own cwd. It's insane.)

@piscisaureus We'd talked way back when about having some kind of path.parse() function similar to url.parse which would return the device, path, basename, etc., and whether or not the path is absolute. Do you still think that's a good idea? It seems like most of the functionality is there already. It could return something like { drive: "C", directories:["foo", "bar"], basename:"baz.ext", extension: ".ext", absolute: true }.

@aseemk Would that meet your needs? You could also just split on /[\/\\]/ to get the parts if process.platform === "win32".

@aseemk
Copy link
Author

aseemk commented Jun 24, 2011

Man, a path.parse() would be so useful. Given how common it is to parse URLs, this would be a welcome way of dealing with path problems too.

That said, even after parsing a (possibly Windows) filepath to split the device (drive) and the rest of the path, it's still common to need to split the path portion into directories, so a path.split() would still be needed.

Yep, I can hardcode the slashes myself and check for Windows; just commenting though that, IMHO, Node as a framework should abstract that away so that every developer doesn't have to do that themselves.

@aseemk
Copy link
Author

aseemk commented Jun 24, 2011

Btw, I was also thinking of filing a feature request for some method that tells me whether a given path is absolute or relative. On UNIX, checking it is easy -- just see if it begins with '/' -- but again, it seems like Node should abstract that away both for robustness (cross-platform) and convenience. It'd be great if a hypothetical path.parse() returned absolute: true/false like you suggested.

@piscisaureus
Copy link

I agree that functionality like path.parse/path.split can be useful. However there is some danger that people will abuse it to implement their own path.resolve in a crappy way. Also, don't be tempted to think that the concept of "absolute" and "relative" is somehow portable.

@piscisaureus
Copy link

@isaacs: is this still on the map? The functionality is mostly there in lib/path.js, it's just not exported.

https://github.com/joyent/node/blob/master/lib/path.js#L66-79
https://github.com/joyent/node/blob/master/lib/path.js#L261-267

@isaacs
Copy link

isaacs commented Sep 8, 2011

@piscisaureus Yeah, we should just export it and add some tests. It would mean that I don't have to implement it in npm in a crappy way :)

@gero3
Copy link

gero3 commented Feb 15, 2012

Is this still okay to get this exported??

I can try to get a pull request together and some tests.

@ghost
Copy link

ghost commented Nov 6, 2013

just googled for path.split assuming it would be part of node. it isnt :(

@OrangeDog
Copy link

pathString.split(path.sep)

@ghost
Copy link

ghost commented Nov 6, 2013

@OrangeDog thanks!

@chrisdickinson
Copy link

This has been implemented by #7429.

@chrisdickinson
Copy link

Fixed in 2d17193.

@callumlocke
Copy link

I know this is old, but... that change implements path.parse(), which does not cover the feature being requested here.

@andyburke
Copy link

Landed here looking for a path.split as well. I agree with @callumlocke, this probably shouldn't have been closed with path.parse().

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

No branches or pull requests

8 participants