diff --git a/index.js b/index.js index 6fac553..a02ed71 100644 --- a/index.js +++ b/index.js @@ -27,6 +27,13 @@ module.exports = function(path, stripTrailing) { } } + // preserve the leading slashes of a UNC path (`\\server\share\...`), + // consistent with the win32 namespace prefixes handled above + if (prefix === '' && len > 2 && path[0] === '\\' && path[1] === '\\' && path[2] !== '\\' && path[2] !== '/') { + path = path.slice(2); + prefix = '//'; + } + var segs = path.split(/[/\\]+/); if (stripTrailing !== false && segs[segs.length - 1] === '') { segs.pop(); diff --git a/test.js b/test.js index 55b3bda..bedac48 100644 --- a/test.js +++ b/test.js @@ -84,4 +84,22 @@ describe('normalize-path', function() { }); }); }); + describe('windows UNC paths', function() { + var units = [ + ['\\\\Server01\\user\\docs\\Letter.txt', '//Server01/user/docs/Letter.txt'], + ['\\\\server\\share\\file.css', '//server/share/file.css'], + ['\\\\localhost\\c$\\temp\\file.txt', '//localhost/c$/temp/file.txt'], + ['\\\\server\\share\\', '//server/share'], + ]; + + units.forEach(function(unit) { + it('should preserve the leading slashes of ' + unit[0], function() { + assert.equal(normalize(unit[0]), unit[1]); + }); + }); + + it('should keep a trailing slash when stripTrailing is false', function() { + assert.equal(normalize('\\\\server\\share\\', false), '//server/share/'); + }); + }); });