Skip to content

Commit d1000b4

Browse files
nwoltmanjoaocgreis
authored andcommitted
path: make format() consistent and more functional
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 <[email protected]> Reviewed-By: James M Snell <[email protected]> PR-URL: #2408
1 parent e25f868 commit d1000b4

File tree

3 files changed

+41
-35
lines changed

3 files changed

+41
-35
lines changed

doc/api/path.markdown

+10
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,16 @@ Returns a path string from an object, the opposite of `path.parse` above.
9595
// returns
9696
'/home/user/dir/file.txt'
9797

98+
// `root` will be used if `dir` is not specified and `name` + `ext` will be used
99+
// if `base` is not specified
100+
path.format({
101+
root : "/",
102+
ext : ".txt",
103+
name : "file"
104+
})
105+
// returns
106+
'/file.txt'
107+
98108
## path.isAbsolute(path)
99109

100110
Determines whether `path` is an absolute path. An absolute path will always

lib/path.js

+13-23
Original file line numberDiff line numberDiff line change
@@ -361,21 +361,13 @@ win32.format = function(pathObject) {
361361
);
362362
}
363363

364-
var root = pathObject.root || '';
365-
366-
if (typeof root !== 'string') {
367-
throw new TypeError(
368-
'"pathObject.root" must be a string or undefined, not ' +
369-
typeof pathObject.root
370-
);
371-
}
372-
373-
var dir = pathObject.dir;
374-
var base = pathObject.base || '';
364+
var dir = pathObject.dir || pathObject.root;
365+
var base = pathObject.base ||
366+
((pathObject.name || '') + (pathObject.ext || ''));
375367
if (!dir) {
376368
return base;
377369
}
378-
if (dir[dir.length - 1] === win32.sep) {
370+
if (dir === pathObject.root) {
379371
return dir + base;
380372
}
381373
return dir + win32.sep + base;
@@ -570,18 +562,16 @@ posix.format = function(pathObject) {
570562
);
571563
}
572564

573-
var root = pathObject.root || '';
574-
575-
if (typeof root !== 'string') {
576-
throw new TypeError(
577-
'"pathObject.root" must be a string or undefined, not ' +
578-
typeof pathObject.root
579-
);
565+
var dir = pathObject.dir || pathObject.root;
566+
var base = pathObject.base ||
567+
((pathObject.name || '') + (pathObject.ext || ''));
568+
if (!dir) {
569+
return base;
580570
}
581-
582-
var dir = pathObject.dir ? pathObject.dir + posix.sep : '';
583-
var base = pathObject.base || '';
584-
return dir + base;
571+
if (dir === pathObject.root) {
572+
return dir + base;
573+
}
574+
return dir + posix.sep + base;
585575
};
586576

587577

test/parallel/test-path-parse-format.js

+18-12
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
11
'use strict';
22
require('../common');
3-
var assert = require('assert');
4-
var path = require('path');
3+
const assert = require('assert');
4+
const path = require('path');
55

6-
var winPaths = [
6+
const winPaths = [
77
'C:\\path\\dir\\index.html',
8-
'C:\\another_path\\DIR\\1\\2\\33\\index',
8+
'C:\\another_path\\DIR\\1\\2\\33\\\\index',
99
'another_path\\DIR with spaces\\1\\2\\33\\index',
1010
'\\foo\\C:',
1111
'file',
1212
'.\\file',
13+
'C:\\',
1314
'',
1415

1516
// unc
@@ -19,13 +20,17 @@ var winPaths = [
1920
'\\\\?\\UNC\\server\\share'
2021
];
2122

22-
var winSpecialCaseFormatTests = [
23+
const winSpecialCaseFormatTests = [
2324
[{dir: 'some\\dir'}, 'some\\dir\\'],
2425
[{base: 'index.html'}, 'index.html'],
26+
[{root: 'C:\\'}, 'C:\\'],
27+
[{name: 'index', ext: '.html'}, 'index.html'],
28+
[{dir: 'some\\dir', name: 'index', ext: '.html'}, 'some\\dir\\index.html'],
29+
[{root: 'C:\\', name: 'index', ext: '.html'}, 'C:\\index.html'],
2530
[{}, '']
2631
];
2732

28-
var unixPaths = [
33+
const unixPaths = [
2934
'/home/user/dir/file.txt',
3035
'/home/user/a dir/another File.zip',
3136
'/home/user/a dir//another&File.',
@@ -35,16 +40,21 @@ var unixPaths = [
3540
'.\\file',
3641
'./file',
3742
'C:\\foo',
43+
'/',
3844
''
3945
];
4046

41-
var unixSpecialCaseFormatTests = [
47+
const unixSpecialCaseFormatTests = [
4248
[{dir: 'some/dir'}, 'some/dir/'],
4349
[{base: 'index.html'}, 'index.html'],
50+
[{root: '/'}, '/'],
51+
[{name: 'index', ext: '.html'}, 'index.html'],
52+
[{dir: 'some/dir', name: 'index', ext: '.html'}, 'some/dir/index.html'],
53+
[{root: '/', name: 'index', ext: '.html'}, '/index.html'],
4454
[{}, '']
4555
];
4656

47-
var errors = [
57+
const errors = [
4858
{method: 'parse', input: [null],
4959
message: /Path must be a string. Received null/},
5060
{method: 'parse', input: [{}],
@@ -63,10 +73,6 @@ var errors = [
6373
message: /Parameter "pathObject" must be an object, not boolean/},
6474
{method: 'format', input: [1],
6575
message: /Parameter "pathObject" must be an object, not number/},
66-
{method: 'format', input: [{root: true}],
67-
message: /"pathObject\.root" must be a string or undefined, not boolean/},
68-
{method: 'format', input: [{root: 12}],
69-
message: /"pathObject\.root" must be a string or undefined, not number/},
7076
];
7177

7278
checkParseFormat(path.win32, winPaths);

0 commit comments

Comments
 (0)