UNPKG

1.11 kBJavaScriptView Raw
1'use strict';
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6exports.unixify = unixify;
7exports.correctPath = correctPath;
8var isWin = process.platform === 'win32';
9
10function removeTrailingSeparator(str) {
11 var i = str.length - 1;
12 if (i < 2) {
13 return str;
14 }
15 while (isSeparator(str, i)) {
16 i--;
17 }
18 return str.substr(0, i + 1);
19}
20
21function isSeparator(str, i) {
22 var char = str[i];
23 return i > 0 && (char === '/' || isWin && char === '\\');
24}
25
26function normalizePath(str, stripTrailing) {
27 if (typeof str !== 'string') {
28 throw new TypeError('expected a string');
29 }
30 str = str.replace(/[\\\/]+/g, '/');
31 if (stripTrailing !== false) {
32 str = removeTrailingSeparator(str);
33 }
34 return str;
35}
36
37function unixify(filepath) {
38 var stripTrailing = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : true;
39
40 if (isWin) {
41 filepath = normalizePath(filepath, stripTrailing);
42 return filepath.replace(/^([a-zA-Z]+:|\.\/)/, '');
43 }
44 return filepath;
45}
46
47function correctPath(filepath) {
48 return unixify(filepath.replace(/^\\\\\?\\.:\\/, '\\'));
49}
\No newline at end of file