Yang Guo | 4fd355c | 2019-09-19 10:59:03 +0200 | [diff] [blame] | 1 | /*! |
| 2 | * normalize-path <https://github.com/jonschlinkert/normalize-path> |
| 3 | * |
| 4 | * Copyright (c) 2014-2018, Jon Schlinkert. |
| 5 | * Released under the MIT License. |
| 6 | */ |
| 7 | |
| 8 | module.exports = function(path, stripTrailing) { |
| 9 | if (typeof path !== 'string') { |
| 10 | throw new TypeError('expected path to be a string'); |
| 11 | } |
| 12 | |
| 13 | if (path === '\\' || path === '/') return '/'; |
| 14 | |
| 15 | var len = path.length; |
| 16 | if (len <= 1) return path; |
| 17 | |
| 18 | // ensure that win32 namespaces has two leading slashes, so that the path is |
| 19 | // handled properly by the win32 version of path.parse() after being normalized |
| 20 | // https://msdn.microsoft.com/library/windows/desktop/aa365247(v=vs.85).aspx#namespaces |
| 21 | var prefix = ''; |
| 22 | if (len > 4 && path[3] === '\\') { |
| 23 | var ch = path[2]; |
| 24 | if ((ch === '?' || ch === '.') && path.slice(0, 2) === '\\\\') { |
| 25 | path = path.slice(2); |
| 26 | prefix = '//'; |
| 27 | } |
| 28 | } |
| 29 | |
| 30 | var segs = path.split(/[/\\]+/); |
| 31 | if (stripTrailing !== false && segs[segs.length - 1] === '') { |
| 32 | segs.pop(); |
| 33 | } |
| 34 | return prefix + segs.join('/'); |
| 35 | }; |