Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

path: make format() consistent and more functional #2408

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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