3 var isWindows = process.platform === 'win32';
5 // Regex to split a windows path into three parts: [*, device, slash,
8 /^([a-zA-Z]:|[\\\/]{2}[^\\\/]+[\\\/]+[^\\\/]+)?([\\\/])?([\s\S]*?)$/;
10 // Regex to split the tail part of the above into [*, dir, basename, ext]
12 /^([\s\S]*?)((?:\.{1,2}|[^\\\/]+?|)(\.[^.\/\\]*|))(?:[\\\/]*)$/;
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),
25 basename = result2[2],
27 return [device, dir, basename, ext];
30 win32.parse = function(pathString) {
31 if (typeof pathString !== 'string') {
33 "Parameter 'pathString' must be a string, not " + typeof pathString
36 var allParts = win32SplitPath(pathString);
37 if (!allParts || allParts.length !== 4) {
38 throw new TypeError("Invalid path '" + pathString + "'");
42 dir: allParts[0] + allParts[1].slice(0, -1),
45 name: allParts[2].slice(0, allParts[2].length - allParts[3].length)
51 // Split a filename into [root, dir, basename, ext], unix version
52 // 'root' is just a slash, or nothing.
54 /^(\/?|)([\s\S]*?)((?:\.{1,2}|[^\/]+?|)(\.[^.\/]*|))(?:[\/]*)$/;
58 function posixSplitPath(filename) {
59 return splitPathRe.exec(filename).slice(1);
63 posix.parse = function(pathString) {
64 if (typeof pathString !== 'string') {
66 "Parameter 'pathString' must be a string, not " + typeof pathString
69 var allParts = posixSplitPath(pathString);
70 if (!allParts || allParts.length !== 4) {
71 throw new TypeError("Invalid path '" + pathString + "'");
73 allParts[1] = allParts[1] || '';
74 allParts[2] = allParts[2] || '';
75 allParts[3] = allParts[3] || '';
79 dir: allParts[0] + allParts[1].slice(0, -1),
82 name: allParts[2].slice(0, allParts[2].length - allParts[3].length)
88 module.exports = win32.parse;
90 module.exports = posix.parse;
92 module.exports.posix = posix.parse;
93 module.exports.win32 = win32.parse;