UNPKG

1.08 kBJavaScriptView Raw
1"use strict";
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6exports.normalizePath = normalizePath;
7
8function normalizePath(path, stripTrailing) {
9 if (path === '\\' || path === '/') {
10 return '/';
11 }
12
13 const len = path.length;
14
15 if (len <= 1) {
16 return path;
17 } // ensure that win32 namespaces has two leading slashes, so that the path is
18 // handled properly by the win32 version of path.parse() after being normalized
19 // https://msdn.microsoft.com/library/windows/desktop/aa365247(v=vs.85).aspx#namespaces
20
21
22 let prefix = '';
23
24 if (len > 4 && path[3] === '\\') {
25 // eslint-disable-next-line prefer-destructuring
26 const ch = path[2];
27
28 if ((ch === '?' || ch === '.') && path.slice(0, 2) === '\\\\') {
29 // eslint-disable-next-line no-param-reassign
30 path = path.slice(2);
31 prefix = '//';
32 }
33 }
34
35 const segs = path.split(/[/\\]+/);
36
37 if (stripTrailing !== false && segs[segs.length - 1] === '') {
38 segs.pop();
39 }
40
41 return prefix + segs.join('/');
42} // eslint-disable-next-line import/prefer-default-export
\No newline at end of file