-
-
Notifications
You must be signed in to change notification settings - Fork 7
/
index.js
143 lines (130 loc) · 3.52 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
'use strict';
var path = require('path');
var inspect = require('util').inspect;
function assertPath(path) {
if (typeof path !== 'string') {
throw new TypeError('Path must be a string. Received ' + inspect(path));
}
}
function posix(path) {
assertPath(path);
if (path.length === 0)
return '.';
var code = path.charCodeAt(0);
var hasRoot = (code === 47/*/*/);
var end = -1;
var matchedSlash = true;
for (var i = path.length - 1; i >= 1; --i) {
code = path.charCodeAt(i);
if (code === 47/*/*/) {
if (!matchedSlash) {
end = i;
break;
}
} else {
// We saw the first non-path separator
matchedSlash = false;
}
}
if (end === -1)
return hasRoot ? '/' : '.';
if (hasRoot && end === 1)
return '//';
return path.slice(0, end);
}
function win32(path) {
assertPath(path);
var len = path.length;
if (len === 0)
return '.';
var rootEnd = -1;
var end = -1;
var matchedSlash = true;
var offset = 0;
var code = path.charCodeAt(0);
// Try to match a root
if (len > 1) {
if (code === 47/*/*/ || code === 92/*\*/) {
// Possible UNC root
rootEnd = offset = 1;
code = path.charCodeAt(1);
if (code === 47/*/*/ || code === 92/*\*/) {
// Matched double path separator at beginning
var j = 2;
var last = j;
// Match 1 or more non-path separators
for (; j < len; ++j) {
code = path.charCodeAt(j);
if (code === 47/*/*/ || code === 92/*\*/)
break;
}
if (j < len && j !== last) {
// Matched!
last = j;
// Match 1 or more path separators
for (; j < len; ++j) {
code = path.charCodeAt(j);
if (code !== 47/*/*/ && code !== 92/*\*/)
break;
}
if (j < len && j !== last) {
// Matched!
last = j;
// Match 1 or more non-path separators
for (; j < len; ++j) {
code = path.charCodeAt(j);
if (code === 47/*/*/ || code === 92/*\*/)
break;
}
if (j === len) {
// We matched a UNC root only
return path;
}
if (j !== last) {
// We matched a UNC root with leftovers
// Offset by 1 to include the separator after the UNC root to
// treat it as a "normal root" on top of a (UNC) root
rootEnd = offset = j + 1;
}
}
}
}
} else if ((code >= 65/*A*/ && code <= 90/*Z*/) ||
(code >= 97/*a*/ && code <= 122/*z*/)) {
// Possible device root
code = path.charCodeAt(1);
if (path.charCodeAt(1) === 58/*:*/) {
rootEnd = offset = 2;
if (len > 2) {
code = path.charCodeAt(2);
if (code === 47/*/*/ || code === 92/*\*/)
rootEnd = offset = 3;
}
}
}
} else if (code === 47/*/*/ || code === 92/*\*/) {
return path[0];
}
for (var i = len - 1; i >= offset; --i) {
code = path.charCodeAt(i);
if (code === 47/*/*/ || code === 92/*\*/) {
if (!matchedSlash) {
end = i;
break;
}
} else {
// We saw the first non-path separator
matchedSlash = false;
}
}
if (end === -1) {
if (rootEnd === -1)
return '.';
else
end = rootEnd;
}
return path.slice(0, end);
}
module.exports = process.platform === 'win32' ? win32 : posix;
module.exports.posix = posix;
module.exports.win32 = win32;