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

Function normalizeString

lib/path.js:92–154  ·  view source on GitHub ↗
(path, allowAboveRoot, separator, isPathSeparator)

Source from the content-addressed store, hash-verified

90
91// Resolves . and .. elements in a path with directory names
92function normalizeString(path, allowAboveRoot, separator, isPathSeparator) {
93 let res = '';
94 let lastSegmentLength = 0;
95 let lastSlash = -1;
96 let dots = 0;
97 let code = 0;
98 for (let i = 0; i <= path.length; ++i) {
99 if (i < path.length)
100 code = StringPrototypeCharCodeAt(path, i);
101 else if (isPathSeparator(code))
102 break;
103 else
104 code = CHAR_FORWARD_SLASH;
105
106 if (isPathSeparator(code)) {
107 if (lastSlash === i - 1 || dots === 1) {
108 // NOOP
109 } else if (dots === 2) {
110 if (res.length < 2 || lastSegmentLength !== 2 ||
111 StringPrototypeCharCodeAt(res, res.length - 1) !== CHAR_DOT ||
112 StringPrototypeCharCodeAt(res, res.length - 2) !== CHAR_DOT) {
113 if (res.length > 2) {
114 const lastSlashIndex = res.length - lastSegmentLength - 1;
115 if (lastSlashIndex === -1) {
116 res = '';
117 lastSegmentLength = 0;
118 } else {
119 res = StringPrototypeSlice(res, 0, lastSlashIndex);
120 lastSegmentLength =
121 res.length - 1 - StringPrototypeLastIndexOf(res, separator);
122 }
123 lastSlash = i;
124 dots = 0;
125 continue;
126 } else if (res.length !== 0) {
127 res = '';
128 lastSegmentLength = 0;
129 lastSlash = i;
130 dots = 0;
131 continue;
132 }
133 }
134 if (allowAboveRoot) {
135 res += res.length > 0 ? `${separator}..` : '..';
136 lastSegmentLength = 2;
137 }
138 } else {
139 if (res.length > 0)
140 res += `${separator}${StringPrototypeSlice(path, lastSlash + 1, i)}`;
141 else
142 res = StringPrototypeSlice(path, lastSlash + 1, i);
143 lastSegmentLength = i - lastSlash - 1;
144 }
145 lastSlash = i;
146 dots = 0;
147 } else if (code === CHAR_DOT && dots !== -1) {
148 ++dots;
149 } else {

Callers 2

resolveFunction · 0.85
normalizeFunction · 0.85

Calls 1

isPathSeparatorFunction · 0.85

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…