From e82b7320abcb655a133f1ae627770cbe7c98d18d Mon Sep 17 00:00:00 2001 From: Nathan Woltman Date: Sun, 16 Aug 2015 23:26:46 -0400 Subject: [PATCH] path: make format() consistent and more functional MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 Reviewed-By: James M Snell PR-URL: https://github.com/nodejs/node/pull/2408 --- doc/api/path.markdown | 10 +++++++ lib/path.js | 36 +++++++++---------------- test/parallel/test-path-parse-format.js | 30 ++++++++++++--------- 3 files changed, 41 insertions(+), 35 deletions(-) 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);