1 | 'use strict';
|
---|
2 |
|
---|
3 | var isWindows = process.platform === 'win32';
|
---|
4 |
|
---|
5 | // Regex to split a windows path into into [dir, root, basename, name, ext]
|
---|
6 | var splitWindowsRe =
|
---|
7 | /^(((?:[a-zA-Z]:|[\\\/]{2}[^\\\/]+[\\\/]+[^\\\/]+)?[\\\/]?)(?:[^\\\/]*[\\\/])*)((\.{1,2}|[^\\\/]+?|)(\.[^.\/\\]*|))[\\\/]*$/;
|
---|
8 |
|
---|
9 | var win32 = {};
|
---|
10 |
|
---|
11 | function win32SplitPath(filename) {
|
---|
12 | return splitWindowsRe.exec(filename).slice(1);
|
---|
13 | }
|
---|
14 |
|
---|
15 | win32.parse = function(pathString) {
|
---|
16 | if (typeof pathString !== 'string') {
|
---|
17 | throw new TypeError(
|
---|
18 | "Parameter 'pathString' must be a string, not " + typeof pathString
|
---|
19 | );
|
---|
20 | }
|
---|
21 | var allParts = win32SplitPath(pathString);
|
---|
22 | if (!allParts || allParts.length !== 5) {
|
---|
23 | throw new TypeError("Invalid path '" + pathString + "'");
|
---|
24 | }
|
---|
25 | return {
|
---|
26 | root: allParts[1],
|
---|
27 | dir: allParts[0] === allParts[1] ? allParts[0] : allParts[0].slice(0, -1),
|
---|
28 | base: allParts[2],
|
---|
29 | ext: allParts[4],
|
---|
30 | name: allParts[3]
|
---|
31 | };
|
---|
32 | };
|
---|
33 |
|
---|
34 |
|
---|
35 |
|
---|
36 | // Split a filename into [dir, root, basename, name, ext], unix version
|
---|
37 | // 'root' is just a slash, or nothing.
|
---|
38 | var splitPathRe =
|
---|
39 | /^((\/?)(?:[^\/]*\/)*)((\.{1,2}|[^\/]+?|)(\.[^.\/]*|))[\/]*$/;
|
---|
40 | var posix = {};
|
---|
41 |
|
---|
42 |
|
---|
43 | function posixSplitPath(filename) {
|
---|
44 | return splitPathRe.exec(filename).slice(1);
|
---|
45 | }
|
---|
46 |
|
---|
47 |
|
---|
48 | posix.parse = function(pathString) {
|
---|
49 | if (typeof pathString !== 'string') {
|
---|
50 | throw new TypeError(
|
---|
51 | "Parameter 'pathString' must be a string, not " + typeof pathString
|
---|
52 | );
|
---|
53 | }
|
---|
54 | var allParts = posixSplitPath(pathString);
|
---|
55 | if (!allParts || allParts.length !== 5) {
|
---|
56 | throw new TypeError("Invalid path '" + pathString + "'");
|
---|
57 | }
|
---|
58 |
|
---|
59 | return {
|
---|
60 | root: allParts[1],
|
---|
61 | dir: allParts[0].slice(0, -1),
|
---|
62 | base: allParts[2],
|
---|
63 | ext: allParts[4],
|
---|
64 | name: allParts[3],
|
---|
65 | };
|
---|
66 | };
|
---|
67 |
|
---|
68 |
|
---|
69 | if (isWindows)
|
---|
70 | module.exports = win32.parse;
|
---|
71 | else /* posix */
|
---|
72 | module.exports = posix.parse;
|
---|
73 |
|
---|
74 | module.exports.posix = posix.parse;
|
---|
75 | module.exports.win32 = win32.parse;
|
---|