diff --git a/doc/api/path.markdown b/doc/api/path.markdown index 454c79353f7b3c..96f4888fb330e5 100644 --- a/doc/api/path.markdown +++ b/doc/api/path.markdown @@ -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 diff --git a/lib/path.js b/lib/path.js index 694cd38425a719..0a2c0d6bceb0ec 100644 --- a/lib/path.js +++ b/lib/path.js @@ -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; @@ -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; }; diff --git a/test/parallel/test-path-parse-format.js b/test/parallel/test-path-parse-format.js index 0d5502a1dfe852..af1d993ffd3a9f 100644 --- a/test/parallel/test-path-parse-format.js +++ b/test/parallel/test-path-parse-format.js @@ -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 @@ -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.', @@ -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: [{}], @@ -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);