Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

path.split implementation #2766

Closed
wants to merge 2 commits into from

9 participants

@gero3

I added an implementation to create a split method in the path module. This is created based on #1224.

I ask for input
@isaacs @bnoordhuis

@isaacs
Owner

I like the basic idea here. However, I think that it needs a bit more refinement.

  1. It should be called 'path.parse' rather than 'path.split', since that's what it's doing. path.split would just be p.split(/\/+/) on unix, and p.split(/[\/\\]+/) on windows. This is turning it into an object, which is very useful, but different.
  2. If there's a path.parse, then there should be a symmetrical path.format that turns the object into a string.
  3. Please make sure that the code lints properly. There's a lot of extra spaces lining things up: don't do that.
  4. The unix result is kind of weird. There's really no "device" in the path on unix, so that should probably just be left out.
  5. It needs tests (a lot, especially really weird chars etc.) and documentation.

@igorzi, @piscisaureus Do you feel that this captures the appropriate information about a windows path?

@Nodejs-Jenkins

Can one of the admins verify this patch?

@ronkorving

What's the status on this? (poke @gero3 )

@isaacs
Owner

@ronkorving @gero3 It's still waiting for someone to get it over the finishline.

Requirements:

  1. Add path.format which does the inverse of path.parse.
  2. A buttload of tests.
  3. Rebase onto current master.
  4. make jslintfix to fix the style issues.
@gero3

I'll have another look at it tonight

@jquense

I'd take a stab at implementing this since @gero3 seems to be out of the game. Tho I am not sure what the "device" for UNC paths should be. \mycomp\a\path\to\file.js is going to be parsed oddly in this commit ("\mycomp\a"), should the device be "\mycomp" or ""?

@indutny
Owner

ping ;)

@litmit

See also #6976

@Mithgol

Pull request #7429 might fix this issue.

@JacksonTian

anyone can close it?

@indutny indutny closed this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Feb 16, 2012
  1. @gero3
Commits on Feb 21, 2012
  1. @gero3

    "add improvements"

    gero3 authored
This page is out of date. Refresh to see the latest.
Showing with 53 additions and 0 deletions.
  1. +53 −0 lib/path.js
View
53 lib/path.js
@@ -78,6 +78,35 @@ if (isWindows) {
ext = result2[3] || '';
return [device, dir, basename, ext];
};
+
+ exports.parse = function(filepath){
+
+ var result = splitDeviceRe.exec(filepath),
+ device = (result[1] || '') + (result[2] || ''),
+ tail = result[3] || '';
+
+ // Split the tail into dir, basename and extension
+ var result2 = splitTailRe.exec(tail),
+ dir = result2[1] || '',
+ basename = result2[2] || '',
+ ext = result2[3] || '';
+
+ var isUnc = device && device.charAt(1) !== ':',
+ isAbsolute = !!result[2] || isUnc,
+ directories = dir.split(/[\\\/]/);
+
+ return {"device": device, //outputs "C:"
+ "directories": directories, //outputs [test1,test2]
+ "baseName":basename, //outputs "test3"
+ "filename" : basename + ext, //outputs "test3.ext"
+ "extension":ext, //outputs ".ext"
+ "isUnc":isUnc, //outputs false
+ "isAbsolute":isAbsolute, //outputs true
+ "filepath":filepath //outputs C:/test1/test2/test3.ext
+ };
+
+
+ };
// path.resolve([from ...], to)
// windows version
@@ -261,6 +290,30 @@ if (isWindows) {
var result = splitPathRe.exec(filename);
return [result[1] || '', result[2] || '', result[3] || '', result[4] || ''];
};
+
+ exports.parse = function(filepath){
+
+ var result = splitPathRe.exec(filepath),
+ isAbsolute = filepath.charAt(0) === '/',
+ device = result[1] || '',
+ dir = result[2] || '',
+ basename = result[3] || '',
+ ext = result[4] || '',
+ directories = dir.split(/[\/]/);
+
+ return {
+ "directories": directories,
+ "baseName":basename,
+ "filename" : basename + ext,
+ "extension":ext,
+ "isUnc":false,
+ "isAbsolute":isAbsolute,
+ "filepath":filepath
+ };
+
+
+ };
+
// path.resolve([from ...], to)
// posix version
Something went wrong with that request. Please try again.