UNPKG

6.67 kBJavaScriptView Raw
1//TODO wrap properly
2 //The rest is `path`, copied from path-browserify
3 var path = (function() {
4 var exports = {};
5 function normalizeArray(parts, allowAboveRoot) {
6 // if the path tries to go above the root, `up` ends up > 0
7 var up = 0;
8 for (var i = parts.length - 1; i >= 0; i--) {
9 var last = parts[i];
10 if (last === '.') {
11 parts.splice(i, 1);
12 } else if (last === '..') {
13 parts.splice(i, 1);
14 up++;
15 } else if (up) {
16 parts.splice(i, 1);
17 up--;
18 }
19 }
20
21 // if the path is allowed to go above the root, restore leading ..s
22 if (allowAboveRoot) {
23 for (; up--; up) {
24 parts.unshift('..');
25 }
26 }
27
28 return parts;
29 }
30
31 // Split a filename into [root, dir, basename, ext], unix version
32 // 'root' is just a slash, or nothing.
33 var splitPathRe =
34 /^(\/?|)([\s\S]*?)((?:\.{1,2}|[^\/]+?|)(\.[^.\/]*|))(?:[\/]*)$/;
35 var splitPath = function(filename) {
36 return splitPathRe.exec(filename).slice(1);
37 };
38
39 // path.resolve([from ...], to)
40 // posix version
41 exports.resolve = function() {
42 var resolvedPath = '',
43 resolvedAbsolute = false;
44
45 for (var i = arguments.length - 1; i >= -1 && !resolvedAbsolute; i--) {
46 var path = (i >= 0) ? arguments[i] : process.cwd();
47
48 // Skip empty and invalid entries
49 if (typeof path !== 'string') {
50 throw new TypeError('Arguments to path.resolve must be strings');
51 } else if (!path) {
52 continue;
53 }
54
55 resolvedPath = path + '/' + resolvedPath;
56 resolvedAbsolute = path.charAt(0) === '/';
57 }
58
59 // At this point the path should be resolved to a full absolute path, but
60 // handle relative paths to be safe (might happen when process.cwd() fails)
61
62 // Normalize the path
63 resolvedPath = normalizeArray(filter(resolvedPath.split('/'), function(p) {
64 return !!p;
65 }), !resolvedAbsolute).join('/');
66
67 return ((resolvedAbsolute ? '/' : '') + resolvedPath) || '.';
68 };
69
70 // path.normalize(path)
71 // posix version
72 exports.normalize = function(path) {
73 var isAbsolute = exports.isAbsolute(path),
74 trailingSlash = substr(path, -1) === '/';
75
76 // Normalize the path
77 path = normalizeArray(filter(path.split('/'), function(p) {
78 return !!p;
79 }), !isAbsolute).join('/');
80
81 if (!path && !isAbsolute) {
82 path = '.';
83 }
84 if (path && trailingSlash) {
85 path += '/';
86 }
87
88 return (isAbsolute ? '/' : '') + path;
89 };
90
91 // posix version
92 exports.isAbsolute = function(path) {
93 return path.charAt(0) === '/';
94 };
95
96 // posix version
97 exports.join = function() {
98 var paths = Array.prototype.slice.call(arguments, 0);
99 return exports.normalize(filter(paths, function(p, index) {
100 if (typeof p !== 'string') {
101 throw new TypeError('Arguments to path.join must be strings');
102 }
103 return p;
104 }).join('/'));
105 };
106
107
108 // path.relative(from, to)
109 // posix version
110 exports.relative = function(from, to) {
111 from = exports.resolve(from).substr(1);
112 to = exports.resolve(to).substr(1);
113
114 function trim(arr) {
115 var start = 0;
116 for (; start < arr.length; start++) {
117 if (arr[start] !== '') break;
118 }
119
120 var end = arr.length - 1;
121 for (; end >= 0; end--) {
122 if (arr[end] !== '') break;
123 }
124
125 if (start > end) return [];
126 return arr.slice(start, end - start + 1);
127 }
128
129 var fromParts = trim(from.split('/'));
130 var toParts = trim(to.split('/'));
131
132 var length = Math.min(fromParts.length, toParts.length);
133 var samePartsLength = length;
134 for (var i = 0; i < length; i++) {
135 if (fromParts[i] !== toParts[i]) {
136 samePartsLength = i;
137 break;
138 }
139 }
140
141 var outputParts = [];
142 for (var i = samePartsLength; i < fromParts.length; i++) {
143 outputParts.push('..');
144 }
145
146 outputParts = outputParts.concat(toParts.slice(samePartsLength));
147
148 return outputParts.join('/');
149 };
150
151 exports.sep = '/';
152 exports.delimiter = ':';
153
154 exports.dirname = function(path) {
155 var result = splitPath(path),
156 root = result[0],
157 dir = result[1];
158
159 if (!root && !dir) {
160 // No dirname whatsoever
161 return '.';
162 }
163
164 if (dir) {
165 // It has a dirname, strip trailing slash
166 dir = dir.substr(0, dir.length - 1);
167 }
168
169 return root + dir;
170 };
171
172
173 exports.basename = function(path, ext) {
174 var f = splitPath(path)[2];
175 // TODO: make this comparison case-insensitive on windows?
176 if (ext && f.substr(-1 * ext.length) === ext) {
177 f = f.substr(0, f.length - ext.length);
178 }
179 return f;
180 };
181
182
183 exports.extname = function(path) {
184 return splitPath(path)[3];
185 };
186
187 function filter (xs, f) {
188 if (xs.filter) return xs.filter(f);
189 var res = [];
190 for (var i = 0; i < xs.length; i++) {
191 if (f(xs[i], i, xs)) res.push(xs[i]);
192 }
193 return res;
194 }
195
196 // String.prototype.substr - negative index don't work in IE8
197 var substr = 'ab'.substr(-1) === 'b'
198 ? function (str, start, len) { return str.substr(start, len) }
199 : function (str, start, len) {
200 if (start < 0) start = str.length + start;
201 return str.substr(start, len);
202 };
203 return { exports: exports };
204 })();
205
206 m['path'] = path;
207 var Path = path.exports;