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

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

8 participants

@aseemk

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

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

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

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
Node.js Foundation member

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
Node.js Foundation member

@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

@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

Is this still okay to get this exported??

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

@ghost

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

@OrangeDog

pathString.split(path.sep)

@ghost

@OrangeDog thanks!

@chrisdickinson

This has been implemented by #7429.

@callumlocke

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

@andyburke

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 join this conversation on GitHub. Already have an account? Sign in to comment