UNPKG

5.12 kBJavaScriptView Raw
1// Slightly modified (no IE8 support, ES6) and transcribed to TypeScript
2// https://raw.githubusercontent.com/calvinmetcalf/rollup-plugin-node-builtins/master/src/es6/path.js
3Object.defineProperty(exports, "__esModule", { value: true });
4/** JSDoc */
5function 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 }
13 else if (last === '..') {
14 parts.splice(i, 1);
15 // eslint-disable-next-line no-plusplus
16 up++;
17 }
18 else if (up) {
19 parts.splice(i, 1);
20 // eslint-disable-next-line no-plusplus
21 up--;
22 }
23 }
24 // if the path is allowed to go above the root, restore leading ..s
25 if (allowAboveRoot) {
26 // eslint-disable-next-line no-plusplus
27 for (; up--; up) {
28 parts.unshift('..');
29 }
30 }
31 return parts;
32}
33// Split a filename into [root, dir, basename, ext], unix version
34// 'root' is just a slash, or nothing.
35var splitPathRe = /^(\/?|)([\s\S]*?)((?:\.{1,2}|[^/]+?|)(\.[^./]*|))(?:[/]*)$/;
36/** JSDoc */
37function splitPath(filename) {
38 var parts = splitPathRe.exec(filename);
39 return parts ? parts.slice(1) : [];
40}
41// path.resolve([from ...], to)
42// posix version
43/** JSDoc */
44function resolve() {
45 var args = [];
46 for (var _i = 0; _i < arguments.length; _i++) {
47 args[_i] = arguments[_i];
48 }
49 var resolvedPath = '';
50 var resolvedAbsolute = false;
51 for (var i = args.length - 1; i >= -1 && !resolvedAbsolute; i--) {
52 var path = i >= 0 ? args[i] : '/';
53 // Skip empty entries
54 if (!path) {
55 continue;
56 }
57 resolvedPath = path + "/" + resolvedPath;
58 resolvedAbsolute = path.charAt(0) === '/';
59 }
60 // At this point the path should be resolved to a full absolute path, but
61 // handle relative paths to be safe (might happen when process.cwd() fails)
62 // Normalize the path
63 resolvedPath = normalizeArray(resolvedPath.split('/').filter(function (p) { return !!p; }), !resolvedAbsolute).join('/');
64 return (resolvedAbsolute ? '/' : '') + resolvedPath || '.';
65}
66exports.resolve = resolve;
67/** JSDoc */
68function trim(arr) {
69 var start = 0;
70 for (; start < arr.length; start++) {
71 if (arr[start] !== '') {
72 break;
73 }
74 }
75 var end = arr.length - 1;
76 for (; end >= 0; end--) {
77 if (arr[end] !== '') {
78 break;
79 }
80 }
81 if (start > end) {
82 return [];
83 }
84 return arr.slice(start, end - start + 1);
85}
86// path.relative(from, to)
87// posix version
88/** JSDoc */
89function relative(from, to) {
90 /* eslint-disable no-param-reassign */
91 from = resolve(from).substr(1);
92 to = resolve(to).substr(1);
93 /* eslint-enable no-param-reassign */
94 var fromParts = trim(from.split('/'));
95 var toParts = trim(to.split('/'));
96 var length = Math.min(fromParts.length, toParts.length);
97 var samePartsLength = length;
98 for (var i = 0; i < length; i++) {
99 if (fromParts[i] !== toParts[i]) {
100 samePartsLength = i;
101 break;
102 }
103 }
104 var outputParts = [];
105 for (var i = samePartsLength; i < fromParts.length; i++) {
106 outputParts.push('..');
107 }
108 outputParts = outputParts.concat(toParts.slice(samePartsLength));
109 return outputParts.join('/');
110}
111exports.relative = relative;
112// path.normalize(path)
113// posix version
114/** JSDoc */
115function normalizePath(path) {
116 var isPathAbsolute = isAbsolute(path);
117 var trailingSlash = path.substr(-1) === '/';
118 // Normalize the path
119 var normalizedPath = normalizeArray(path.split('/').filter(function (p) { return !!p; }), !isPathAbsolute).join('/');
120 if (!normalizedPath && !isPathAbsolute) {
121 normalizedPath = '.';
122 }
123 if (normalizedPath && trailingSlash) {
124 normalizedPath += '/';
125 }
126 return (isPathAbsolute ? '/' : '') + normalizedPath;
127}
128exports.normalizePath = normalizePath;
129// posix version
130/** JSDoc */
131function isAbsolute(path) {
132 return path.charAt(0) === '/';
133}
134exports.isAbsolute = isAbsolute;
135// posix version
136/** JSDoc */
137function join() {
138 var args = [];
139 for (var _i = 0; _i < arguments.length; _i++) {
140 args[_i] = arguments[_i];
141 }
142 return normalizePath(args.join('/'));
143}
144exports.join = join;
145/** JSDoc */
146function dirname(path) {
147 var result = splitPath(path);
148 var root = result[0];
149 var dir = result[1];
150 if (!root && !dir) {
151 // No dirname whatsoever
152 return '.';
153 }
154 if (dir) {
155 // It has a dirname, strip trailing slash
156 dir = dir.substr(0, dir.length - 1);
157 }
158 return root + dir;
159}
160exports.dirname = dirname;
161/** JSDoc */
162function basename(path, ext) {
163 var f = splitPath(path)[2];
164 if (ext && f.substr(ext.length * -1) === ext) {
165 f = f.substr(0, f.length - ext.length);
166 }
167 return f;
168}
169exports.basename = basename;
170//# sourceMappingURL=path.js.map
\No newline at end of file