MCPcopy Index your code
hub / github.com/nodejs/node / parse

Function parse

lib/path.js:1062–1212  ·  view source on GitHub ↗

* @param {string} path * @returns {{ * dir: string; * root: string; * base: string; * name: string; * ext: string; * }}

(path)

Source from the content-addressed store, hash-verified

1060 * }}
1061 */
1062 parse(path) {
1063 validateString(path, 'path');
1064
1065 const ret = { root: '', dir: '', base: '', ext: '', name: '' };
1066 if (path.length === 0)
1067 return ret;
1068
1069 const len = path.length;
1070 let rootEnd = 0;
1071 let code = StringPrototypeCharCodeAt(path, 0);
1072
1073 if (len === 1) {
1074 if (isPathSeparator(code)) {
1075 // `path` contains just a path separator, exit early to avoid
1076 // unnecessary work
1077 ret.root = ret.dir = path;
1078 return ret;
1079 }
1080 ret.base = ret.name = path;
1081 return ret;
1082 }
1083 // Try to match a root
1084 if (isPathSeparator(code)) {
1085 // Possible UNC root
1086
1087 rootEnd = 1;
1088 if (isPathSeparator(StringPrototypeCharCodeAt(path, 1))) {
1089 // Matched double path separator at beginning
1090 let j = 2;
1091 let last = j;
1092 // Match 1 or more non-path separators
1093 while (j < len &&
1094 !isPathSeparator(StringPrototypeCharCodeAt(path, j))) {
1095 j++;
1096 }
1097 if (j < len && j !== last) {
1098 // Matched!
1099 last = j;
1100 // Match 1 or more path separators
1101 while (j < len &&
1102 isPathSeparator(StringPrototypeCharCodeAt(path, j))) {
1103 j++;
1104 }
1105 if (j < len && j !== last) {
1106 // Matched!
1107 last = j;
1108 // Match 1 or more non-path separators
1109 while (j < len &&
1110 !isPathSeparator(StringPrototypeCharCodeAt(path, j))) {
1111 j++;
1112 }
1113 if (j === len) {
1114 // We matched a UNC root only
1115 rootEnd = j;
1116 } else if (j !== last) {
1117 // We matched a UNC root with leftovers
1118 rootEnd = j + 1;
1119 }

Callers 3

parseTypeScriptFunction · 0.50
checkParentPathsFunction · 0.50
mainFunction · 0.50

Calls 2

isPathSeparatorFunction · 0.85
isWindowsDeviceRootFunction · 0.85

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…