MCPcopy Create free account
hub / github.com/GNOME/gjs / normalize

Function normalize

modules/internal/source-map/util.js:86–131  ·  view source on GitHub ↗
(aPath)

Source from the content-addressed store, hash-verified

84 * @param aPath The path or url to normalize.
85 */
86export function normalize(aPath) {
87 var isAbsolute2 = (aPath) => {
88 return aPath.charAt(0) === '/' || urlRegexp.test(aPath);
89 };
90 var path = aPath;
91 var url = urlParse(aPath);
92 if (url) {
93 if (!url.path) {
94 return aPath;
95 }
96 path = url.path;
97 }
98 var isAbsolute = isAbsolute2(path);
99
100 var parts = path.split(/\/+/);
101 for (var part, up = 0, i = parts.length - 1; i >= 0; i--) {
102 part = parts[i];
103 if (part === '.') {
104 parts.splice(i, 1);
105 } else if (part === '..') {
106 up++;
107 } else if (up > 0) {
108 if (part === '') {
109 // The first part is blank if the path is absolute. Trying to go
110 // above the root is a no-op. Therefore we can remove all '..' parts
111 // directly after the root.
112 parts.splice(i + 1, up);
113 up = 0;
114 } else {
115 parts.splice(i, 2);
116 up--;
117 }
118 }
119 }
120 path = parts.join('/');
121
122 if (path === '') {
123 path = isAbsolute ? '/' : '.';
124 }
125
126 if (url) {
127 url.path = path;
128 return urlGenerate(url);
129 }
130 return path;
131}
132
133/**
134 * Joins two paths/URLs.

Callers 2

joinFunction · 0.85
computeSourceURLFunction · 0.85

Calls 3

urlParseFunction · 0.85
isAbsolute2Function · 0.85
urlGenerateFunction · 0.85

Tested by

no test coverage detected