MCPcopy
hub / github.com/QwikDev/qwik / normalizeStringPosix

Function normalizeStringPosix

packages/qwik/src/optimizer/src/path.ts:32–103  ·  view source on GitHub ↗
(path: string, allowAboveRoot: boolean)

Source from the content-addressed store, hash-verified

30
31 // Resolves . and .. elements in a path with directory names
32 function normalizeStringPosix(path: string, allowAboveRoot: boolean): string {
33 let res = '';
34 let lastSegmentLength = 0;
35 let lastSlash = -1;
36 let dots = 0;
37 let code;
38 for (let i = 0; i <= path.length; ++i) {
39 if (i < path.length) {
40 code = path.charCodeAt(i);
41 } else if (code === 47 /*/*/) {
42 break;
43 } else {
44 code = 47 /*/*/;
45 }
46 if (code === 47 /*/*/) {
47 if (lastSlash === i - 1 || dots === 1) {
48 // NOOP
49 } else if (lastSlash !== i - 1 && dots === 2) {
50 if (
51 res.length < 2 ||
52 lastSegmentLength !== 2 ||
53 res.charCodeAt(res.length - 1) !== 46 /*.*/ ||
54 res.charCodeAt(res.length - 2) !== 46 /*.*/
55 ) {
56 if (res.length > 2) {
57 const lastSlashIndex = res.lastIndexOf('/');
58 if (lastSlashIndex !== res.length - 1) {
59 if (lastSlashIndex === -1) {
60 res = '';
61 lastSegmentLength = 0;
62 } else {
63 res = res.slice(0, lastSlashIndex);
64 lastSegmentLength = res.length - 1 - res.lastIndexOf('/');
65 }
66 lastSlash = i;
67 dots = 0;
68 continue;
69 }
70 } else if (res.length === 2 || res.length === 1) {
71 res = '';
72 lastSegmentLength = 0;
73 lastSlash = i;
74 dots = 0;
75 continue;
76 }
77 }
78 if (allowAboveRoot) {
79 if (res.length > 0) {
80 res += '/..';
81 } else {
82 res = '..';
83 }
84 lastSegmentLength = 2;
85 }
86 } else {
87 if (res.length > 0) {
88 res += '/' + path.slice(lastSlash + 1, i);
89 } else {

Callers 2

resolveFunction · 0.85
normalizeFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…