blob: f062d0a23e6299b2eee1797c6c476159348f147c [file] [log] [blame]
Yang Guo4fd355c2019-09-19 10:59:03 +02001'use strict';
2
3var isWindows = process.platform === 'win32';
4
Brandon Walderman110d5922022-04-19 10:35:07 -07005// Regex to split a windows path into into [dir, root, basename, name, ext]
6var splitWindowsRe =
7 /^(((?:[a-zA-Z]:|[\\\/]{2}[^\\\/]+[\\\/]+[^\\\/]+)?[\\\/]?)(?:[^\\\/]*[\\\/])*)((\.{1,2}|[^\\\/]+?|)(\.[^.\/\\]*|))[\\\/]*$/;
Yang Guo4fd355c2019-09-19 10:59:03 +02008
9var win32 = {};
10
Yang Guo4fd355c2019-09-19 10:59:03 +020011function win32SplitPath(filename) {
Brandon Walderman110d5922022-04-19 10:35:07 -070012 return splitWindowsRe.exec(filename).slice(1);
Yang Guo4fd355c2019-09-19 10:59:03 +020013}
14
15win32.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);
Brandon Walderman110d5922022-04-19 10:35:07 -070022 if (!allParts || allParts.length !== 5) {
Yang Guo4fd355c2019-09-19 10:59:03 +020023 throw new TypeError("Invalid path '" + pathString + "'");
24 }
25 return {
Brandon Walderman110d5922022-04-19 10:35:07 -070026 root: allParts[1],
27 dir: allParts[0] === allParts[1] ? allParts[0] : allParts[0].slice(0, -1),
Yang Guo4fd355c2019-09-19 10:59:03 +020028 base: allParts[2],
Brandon Walderman110d5922022-04-19 10:35:07 -070029 ext: allParts[4],
30 name: allParts[3]
Yang Guo4fd355c2019-09-19 10:59:03 +020031 };
32};
33
34
35
Brandon Walderman110d5922022-04-19 10:35:07 -070036// Split a filename into [dir, root, basename, name, ext], unix version
Yang Guo4fd355c2019-09-19 10:59:03 +020037// 'root' is just a slash, or nothing.
38var splitPathRe =
Brandon Walderman110d5922022-04-19 10:35:07 -070039 /^((\/?)(?:[^\/]*\/)*)((\.{1,2}|[^\/]+?|)(\.[^.\/]*|))[\/]*$/;
Yang Guo4fd355c2019-09-19 10:59:03 +020040var posix = {};
41
42
43function posixSplitPath(filename) {
44 return splitPathRe.exec(filename).slice(1);
45}
46
47
48posix.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);
Brandon Walderman110d5922022-04-19 10:35:07 -070055 if (!allParts || allParts.length !== 5) {
Yang Guo4fd355c2019-09-19 10:59:03 +020056 throw new TypeError("Invalid path '" + pathString + "'");
57 }
Brandon Walderman110d5922022-04-19 10:35:07 -070058
Yang Guo4fd355c2019-09-19 10:59:03 +020059 return {
Brandon Walderman110d5922022-04-19 10:35:07 -070060 root: allParts[1],
61 dir: allParts[0].slice(0, -1),
Yang Guo4fd355c2019-09-19 10:59:03 +020062 base: allParts[2],
Brandon Walderman110d5922022-04-19 10:35:07 -070063 ext: allParts[4],
64 name: allParts[3],
Yang Guo4fd355c2019-09-19 10:59:03 +020065 };
66};
67
68
69if (isWindows)
70 module.exports = win32.parse;
71else /* posix */
72 module.exports = posix.parse;
73
74module.exports.posix = posix.parse;
75module.exports.win32 = win32.parse;