MCPcopy Create free account
hub / github.com/Effect-TS/effect / normalizeStringPosix

Function normalizeStringPosix

packages/effect/src/Path.ts:266–335  ·  view source on GitHub ↗
(path: string, allowAboveRoot: boolean)

Source from the content-addressed store, hash-verified

264
265// Resolves . and .. elements in a path with directory names
266function normalizeStringPosix(path: string, allowAboveRoot: boolean) {
267 let res = ""
268 let lastSegmentLength = 0
269 let lastSlash = -1
270 let dots = 0
271 let code
272 for (let i = 0; i <= path.length; ++i) {
273 if (i < path.length) {
274 code = path.charCodeAt(i)
275 } else if (code === 47 /*/*/) {
276 break
277 } else {
278 code = 47 /*/*/
279 }
280 if (code === 47 /*/*/) {
281 if (lastSlash === i - 1 || dots === 1) {
282 // NOOP
283 } else if (lastSlash !== i - 1 && dots === 2) {
284 if (
285 res.length < 2 || lastSegmentLength !== 2 || res.charCodeAt(res.length - 1) !== 46 /*.*/ ||
286 res.charCodeAt(res.length - 2) !== 46 /*.*/
287 ) {
288 if (res.length > 2) {
289 const lastSlashIndex = res.lastIndexOf("/")
290 if (lastSlashIndex !== res.length - 1) {
291 if (lastSlashIndex === -1) {
292 res = ""
293 lastSegmentLength = 0
294 } else {
295 res = res.slice(0, lastSlashIndex)
296 lastSegmentLength = res.length - 1 - res.lastIndexOf("/")
297 }
298 lastSlash = i
299 dots = 0
300 continue
301 }
302 } else if (res.length === 2 || res.length === 1) {
303 res = ""
304 lastSegmentLength = 0
305 lastSlash = i
306 dots = 0
307 continue
308 }
309 }
310 if (allowAboveRoot) {
311 if (res.length > 0) {
312 res += "/.."
313 } else {
314 res = ".."
315 }
316 lastSegmentLength = 2
317 }
318 } else {
319 if (res.length > 0) {
320 res += "/" + path.slice(lastSlash + 1, i)
321 } else {
322 res = path.slice(lastSlash + 1, i)
323 }

Callers 2

resolveFunction · 0.85
normalizeFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected