Skip to content

Commit

Permalink
path: make format() consistent and more functional
Browse files Browse the repository at this point in the history
Make the win32 and posix versions of path.format() consistent in when
they add a directory separator between the dir and base parts of the
path (always add it unless the dir part is the same as the root).

Also, path.format() is now more functional in that it uses the name
and ext parts of the path if the base part is left out and it uses
the root part if the dir part is left out.

Reviewed-By: João Reis <reis@janeasystems.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
PR-URL: #2408
  • Loading branch information
nwoltman authored and joaocgreis committed Nov 27, 2015
1 parent e25f868 commit d1000b4
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 35 deletions.
10 changes: 10 additions & 0 deletions doc/api/path.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,16 @@ Returns a path string from an object, the opposite of `path.parse` above.
// returns
'/home/user/dir/file.txt'

// `root` will be used if `dir` is not specified and `name` + `ext` will be used
// if `base` is not specified
path.format({
root : "/",
ext : ".txt",
name : "file"
})
// returns
'/file.txt'

## path.isAbsolute(path)

Determines whether `path` is an absolute path. An absolute path will always
Expand Down
36 changes: 13 additions & 23 deletions lib/path.js
Original file line number Diff line number Diff line change
Expand Up @@ -361,21 +361,13 @@ win32.format = function(pathObject) {
);
}

var root = pathObject.root || '';

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

var dir = pathObject.dir;
var base = pathObject.base || '';
var dir = pathObject.dir || pathObject.root;
var base = pathObject.base ||
((pathObject.name || '') + (pathObject.ext || ''));
if (!dir) {
return base;
}
if (dir[dir.length - 1] === win32.sep) {
if (dir === pathObject.root) {
return dir + base;
}
return dir + win32.sep + base;
Expand Down Expand Up @@ -570,18 +562,16 @@ posix.format = function(pathObject) {
);
}

var root = pathObject.root || '';

if (typeof root !== 'string') {
throw new TypeError(
'"pathObject.root" must be a string or undefined, not ' +
typeof pathObject.root
);
var dir = pathObject.dir || pathObject.root;
var base = pathObject.base ||
((pathObject.name || '') + (pathObject.ext || ''));
if (!dir) {
return base;
}

var dir = pathObject.dir ? pathObject.dir + posix.sep : '';
var base = pathObject.base || '';
return dir + base;
if (dir === pathObject.root) {
return dir + base;
}
return dir + posix.sep + base;
};


Expand Down
30 changes: 18 additions & 12 deletions test/parallel/test-path-parse-format.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
'use strict';
require('../common');
var assert = require('assert');
var path = require('path');
const assert = require('assert');
const path = require('path');

var winPaths = [
const winPaths = [
'C:\\path\\dir\\index.html',
'C:\\another_path\\DIR\\1\\2\\33\\index',
'C:\\another_path\\DIR\\1\\2\\33\\\\index',
'another_path\\DIR with spaces\\1\\2\\33\\index',
'\\foo\\C:',
'file',
'.\\file',
'C:\\',
'',

// unc
Expand All @@ -19,13 +20,17 @@ var winPaths = [
'\\\\?\\UNC\\server\\share'
];

var winSpecialCaseFormatTests = [
const winSpecialCaseFormatTests = [
[{dir: 'some\\dir'}, 'some\\dir\\'],
[{base: 'index.html'}, 'index.html'],
[{root: 'C:\\'}, 'C:\\'],
[{name: 'index', ext: '.html'}, 'index.html'],
[{dir: 'some\\dir', name: 'index', ext: '.html'}, 'some\\dir\\index.html'],
[{root: 'C:\\', name: 'index', ext: '.html'}, 'C:\\index.html'],
[{}, '']
];

var unixPaths = [
const unixPaths = [
'/home/user/dir/file.txt',
'/home/user/a dir/another File.zip',
'/home/user/a dir//another&File.',
Expand All @@ -35,16 +40,21 @@ var unixPaths = [
'.\\file',
'./file',
'C:\\foo',
'/',
''
];

var unixSpecialCaseFormatTests = [
const unixSpecialCaseFormatTests = [
[{dir: 'some/dir'}, 'some/dir/'],
[{base: 'index.html'}, 'index.html'],
[{root: '/'}, '/'],
[{name: 'index', ext: '.html'}, 'index.html'],
[{dir: 'some/dir', name: 'index', ext: '.html'}, 'some/dir/index.html'],
[{root: '/', name: 'index', ext: '.html'}, '/index.html'],
[{}, '']
];

var errors = [
const errors = [
{method: 'parse', input: [null],
message: /Path must be a string. Received null/},
{method: 'parse', input: [{}],
Expand All @@ -63,10 +73,6 @@ var errors = [
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/},
];

checkParseFormat(path.win32, winPaths);
Expand Down

0 comments on commit d1000b4

Please sign in to comment.