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

Commit

Permalink
address @misterdjules's comments
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisdickinson committed Nov 20, 2014
1 parent 0857342 commit c917c04
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 24 deletions.
4 changes: 2 additions & 2 deletions doc/api/path.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ An example on Windows:
// returns
['C:\Windows\system32', 'C:\Windows', 'C:\Program Files\nodejs\']

## path.parse
## path.parse(pathString)

Returns an object from a path string.

Expand Down Expand Up @@ -230,7 +230,7 @@ An example on Windows:
name : "index"
}

## path.format
## path.format(pathObject)

Returns a path string from an object, the opposite of `path.parse` above.

Expand Down
58 changes: 46 additions & 12 deletions lib/path.js
Original file line number Diff line number Diff line change
Expand Up @@ -363,10 +363,19 @@ win32.extname = function(path) {
};


win32.format = function(object) {
var root = object.root || '';
var dir = object.dir;
var base = object.base || '';
win32.format = function(pathObject) {
if (!util.isObject(pathObject)) {
throw new TypeError("Parameter 'pathObject' must be an object, not " + typeof pathObject);
}

var root = pathObject.root || '';

if (!util.isString(root)) {
throw new TypeError("'pathObject.root' must be a string or undefined, not " + typeof pathObject.root);
}

var dir = pathObject.dir;
var base = pathObject.base || '';
if (dir.slice(dir.length - 1, dir.length) === win32.sep) {
return dir + base;
} else {
Expand All @@ -375,8 +384,14 @@ win32.format = function(object) {
};


win32.parse = function(string) {
var allParts = win32SplitPath(string);
win32.parse = function(pathString) {
if (!util.isString(pathString)) {
throw new TypeError("Parameter 'pathString' must be a string, not " + typeof pathString);
}
var allParts = win32SplitPath(pathString);
if (!allParts || allParts.length !== 4) {
throw new TypeError("Invalid path '" + pathString + "'");
}
return {
root: allParts[0],
dir: allParts[0] + allParts[1].slice(0, allParts[1].length - 1),
Expand Down Expand Up @@ -568,20 +583,39 @@ posix.extname = function(path) {
};


posix.format = function(object) {
var root = object.root || '';
posix.format = function(pathObject) {
if (!util.isObject(pathObject)) {
throw new TypeError("Parameter 'pathObject' must be an object, not " + typeof pathObject);
}

var root = pathObject.root || '';

if (!util.isString(root)) {
throw new TypeError("'pathObject.root' must be a string or undefined, not " + typeof pathObject.root);
}

var sep = posix.sep;
if (root.indexOf(':') > -1) {
sep = '\\';
}
var dir = object.dir + sep;
var base = object.base || '';
var dir = pathObject.dir + sep;
var base = pathObject.base || '';
return dir + base;
};


posix.parse = function(string) {
var allParts = posixSplitPath(string);
posix.parse = function(pathString) {
if (!util.isString(pathString)) {
throw new TypeError("Parameter 'pathString' must be a string, not " + typeof pathString);
}
var allParts = posixSplitPath(pathString);
if (!allParts || allParts.length !== 4) {
throw new TypeError("Invalid path '" + pathString + "'");
}
allParts[1] = allParts[1] || '';
allParts[2] = allParts[2] || '';
allParts[3] = allParts[3] || '';

return {
root: allParts[0],
dir: allParts[0] + allParts[1].slice(0, allParts[1].length - 1),
Expand Down
48 changes: 38 additions & 10 deletions test/simple/test-path-parse-format.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ var winPaths = [
// unc
'\\\\server\\share\\file_path',
'\\\\server two\\shared folder\\file path.zip',
'\\\\teela\\admin$\\system32'
'\\\\teela\\admin$\\system32',
'\\\\?\\UNC\\server\\share'

];

Expand All @@ -42,23 +43,50 @@ var unixPaths = [
'user/dir/another File.zip'
];

var errors = [
{method: 'parse', input: [null], message: /Parameter 'pathString' must be a string, not/},
{method: 'parse', input: [{}], message: /Parameter 'pathString' must be a string, not object/},
{method: 'parse', input: [true], message: /Parameter 'pathString' must be a string, not boolean/},
{method: 'parse', input: [1], message: /Parameter 'pathString' must be a string, not number/},
{method: 'parse', input: [], message: /Parameter 'pathString' must be a string, not undefined/},
// {method: 'parse', input: [''], message: /Invalid path/}, // omitted because it's hard to trigger!
{method: 'format', input: [null], message: /Parameter 'pathObject' must be an object, not/},
{method: 'format', input: [''], message: /Parameter 'pathObject' must be an object, not string/},
{method: 'format', input: [true], message: /Parameter 'pathObject' must be an object, not boolean/},
{method: 'format', input: [1], message: /Parameter 'pathObject' must be an object, not number/},
{method: 'format', input: [{root: true}], message: /'pathObject.root' must be a string or undefined, not boolean/},
{method: 'format', input: [{root: 12}], message: /'pathObject.root' must be a string or undefined, not number/},
];

check(path.win32, winPaths);
check(path.posix, unixPaths);
checkErrors(path.win32);
checkErrors(path.posix);

function checkErrors(path) {
errors.forEach(function(errorCase) {
try {
path[errorCase.method].apply(path, errorCase.input);
} catch(err) {
assert.ok(err instanceof TypeError);
assert.ok(
errorCase.message.test(err.message),
'expected ' + errorCase.message + ' to match ' + err.message
);
return;
}

assert.fail('should have thrown');
});
}


function check(path, paths) {
paths.forEach(function(element, index, array) {
var count = index + 1;
console.log(count + ': `' + element + '`');
var output = path.parse(element);
var keys = Object.keys(output);
var values = [];
for (var i = 0; i < Object.keys(output).length; i++) {
values.push(output[keys[i]]);
}

assert.strictEqual(path.format(path.parse(element)), element);
assert.strictEqual(path.parse(element).dir, path.dirname(element));
assert.strictEqual(path.parse(element).base, path.basename(element));
assert.strictEqual(path.parse(element).ext, path.extname(element));
})
});
}

0 comments on commit c917c04

Please sign in to comment.