Yang Guo | 4fd355c | 2019-09-19 10:59:03 +0200 | [diff] [blame] | 1 | 'use strict'; |
| 2 | |
| 3 | var isWindows = process.platform === 'win32'; |
| 4 | |
| 5 | // Regex to split a windows path into three parts: [*, device, slash, |
| 6 | // tail] windows-only |
| 7 | var splitDeviceRe = |
| 8 | /^([a-zA-Z]:|[\\\/]{2}[^\\\/]+[\\\/]+[^\\\/]+)?([\\\/])?([\s\S]*?)$/; |
| 9 | |
| 10 | // Regex to split the tail part of the above into [*, dir, basename, ext] |
| 11 | var splitTailRe = |
| 12 | /^([\s\S]*?)((?:\.{1,2}|[^\\\/]+?|)(\.[^.\/\\]*|))(?:[\\\/]*)$/; |
| 13 | |
| 14 | var win32 = {}; |
| 15 | |
| 16 | // Function to split a filename into [root, dir, basename, ext] |
| 17 | function win32SplitPath(filename) { |
| 18 | // Separate device+slash from tail |
| 19 | var result = splitDeviceRe.exec(filename), |
| 20 | device = (result[1] || '') + (result[2] || ''), |
| 21 | tail = result[3] || ''; |
| 22 | // Split the tail into dir, basename and extension |
| 23 | var result2 = splitTailRe.exec(tail), |
| 24 | dir = result2[1], |
| 25 | basename = result2[2], |
| 26 | ext = result2[3]; |
| 27 | return [device, dir, basename, ext]; |
| 28 | } |
| 29 | |
| 30 | win32.parse = function(pathString) { |
| 31 | if (typeof pathString !== 'string') { |
| 32 | throw new TypeError( |
| 33 | "Parameter 'pathString' must be a string, not " + typeof pathString |
| 34 | ); |
| 35 | } |
| 36 | var allParts = win32SplitPath(pathString); |
| 37 | if (!allParts || allParts.length !== 4) { |
| 38 | throw new TypeError("Invalid path '" + pathString + "'"); |
| 39 | } |
| 40 | return { |
| 41 | root: allParts[0], |
| 42 | dir: allParts[0] + allParts[1].slice(0, -1), |
| 43 | base: allParts[2], |
| 44 | ext: allParts[3], |
| 45 | name: allParts[2].slice(0, allParts[2].length - allParts[3].length) |
| 46 | }; |
| 47 | }; |
| 48 | |
| 49 | |
| 50 | |
| 51 | // Split a filename into [root, dir, basename, ext], unix version |
| 52 | // 'root' is just a slash, or nothing. |
| 53 | var splitPathRe = |
| 54 | /^(\/?|)([\s\S]*?)((?:\.{1,2}|[^\/]+?|)(\.[^.\/]*|))(?:[\/]*)$/; |
| 55 | var posix = {}; |
| 56 | |
| 57 | |
| 58 | function posixSplitPath(filename) { |
| 59 | return splitPathRe.exec(filename).slice(1); |
| 60 | } |
| 61 | |
| 62 | |
| 63 | posix.parse = function(pathString) { |
| 64 | if (typeof pathString !== 'string') { |
| 65 | throw new TypeError( |
| 66 | "Parameter 'pathString' must be a string, not " + typeof pathString |
| 67 | ); |
| 68 | } |
| 69 | var allParts = posixSplitPath(pathString); |
| 70 | if (!allParts || allParts.length !== 4) { |
| 71 | throw new TypeError("Invalid path '" + pathString + "'"); |
| 72 | } |
| 73 | allParts[1] = allParts[1] || ''; |
| 74 | allParts[2] = allParts[2] || ''; |
| 75 | allParts[3] = allParts[3] || ''; |
| 76 | |
| 77 | return { |
| 78 | root: allParts[0], |
| 79 | dir: allParts[0] + allParts[1].slice(0, -1), |
| 80 | base: allParts[2], |
| 81 | ext: allParts[3], |
| 82 | name: allParts[2].slice(0, allParts[2].length - allParts[3].length) |
| 83 | }; |
| 84 | }; |
| 85 | |
| 86 | |
| 87 | if (isWindows) |
| 88 | module.exports = win32.parse; |
| 89 | else /* posix */ |
| 90 | module.exports = posix.parse; |
| 91 | |
| 92 | module.exports.posix = posix.parse; |
| 93 | module.exports.win32 = win32.parse; |