Skip to content

Commit

Permalink
Actually use the posix version
Browse files Browse the repository at this point in the history
Instead of accidentally putting in the win32 verison for both
:facepalm:
  • Loading branch information
es128 committed Oct 18, 2016
1 parent 7298ea2 commit 084add2
Showing 1 changed file with 9 additions and 82 deletions.
91 changes: 9 additions & 82 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,86 +11,15 @@ function assertPath(path) {

function posix(path) {
assertPath(path);
var len = path.length;
if (len === 0)
if (path.length === 0)
return '.';
var rootEnd = -1;
var code = path.charCodeAt(0);
var hasRoot = (code === 47/*/*/);
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) {
for (var i = path.length - 1; i >= 1; --i) {
code = path.charCodeAt(i);
if (code === 47/*/*/ || code === 92/*\*/) {
if (code === 47/*/*/) {
if (!matchedSlash) {
end = i;
break;
Expand All @@ -101,12 +30,10 @@ function posix(path) {
}
}

if (end === -1) {
if (rootEnd === -1)
return '.';
else
end = rootEnd;
}
if (end === -1)
return hasRoot ? '/' : '.';
if (hasRoot && end === 1)
return '//';
return path.slice(0, end);
}

Expand Down

0 comments on commit 084add2

Please sign in to comment.