Skip to content

Commit e3a8552

Browse files
committed
Port test-path-resolve.
1 parent 9228436 commit e3a8552

File tree

3 files changed

+98
-35
lines changed

3 files changed

+98
-35
lines changed

index.js

Lines changed: 52 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -38,45 +38,56 @@ function normalizeStringPosix(path, allowAboveRoot) {
3838
var dots = 0;
3939
var code;
4040
for (var i = 0; i <= path.length; ++i) {
41-
if (i < path.length) code = path.charCodeAt(i);else if (code === 47 /*/*/) break;else code = 47 /*/*/;
41+
if (i < path.length)
42+
code = path.charCodeAt(i);
43+
else if (code === 47 /*/*/)
44+
break;
45+
else
46+
code = 47 /*/*/;
4247
if (code === 47 /*/*/) {
43-
if (lastSlash === i - 1 || dots === 1) {
44-
// NOOP
45-
} else if (lastSlash !== i - 1 && dots === 2) {
46-
if (res.length < 2 || lastSegmentLength !== 2 || res.charCodeAt(res.length - 1) !== 46 /*.*/ || res.charCodeAt(res.length - 2) !== 46 /*.*/) {
47-
if (res.length > 2) {
48-
var lastSlashIndex = res.lastIndexOf('/');
49-
if (lastSlashIndex !== res.length - 1) {
50-
if (lastSlashIndex === -1) {
51-
res = '';
52-
lastSegmentLength = 0;
53-
} else {
54-
res = res.slice(0, lastSlashIndex);
55-
lastSegmentLength = res.length - 1 - res.lastIndexOf('/');
56-
}
57-
lastSlash = i;
58-
dots = 0;
59-
continue;
60-
}
61-
} else if (res.length === 2 || res.length === 1) {
48+
if (lastSlash === i - 1 || dots === 1) {
49+
// NOOP
50+
} else if (lastSlash !== i - 1 && dots === 2) {
51+
if (res.length < 2 || lastSegmentLength !== 2 || res.charCodeAt(res.length - 1) !== 46 /*.*/ || res.charCodeAt(res.length - 2) !== 46 /*.*/) {
52+
if (res.length > 2) {
53+
var lastSlashIndex = res.lastIndexOf('/');
54+
if (lastSlashIndex !== res.length - 1) {
55+
if (lastSlashIndex === -1) {
6256
res = '';
6357
lastSegmentLength = 0;
64-
lastSlash = i;
65-
dots = 0;
66-
continue;
58+
} else {
59+
res = res.slice(0, lastSlashIndex);
60+
lastSegmentLength = res.length - 1 - res.lastIndexOf('/');
6761
}
62+
lastSlash = i;
63+
dots = 0;
64+
continue;
6865
}
69-
if (allowAboveRoot) {
70-
if (res.length > 0) res += '/..';else res = '..';
71-
lastSegmentLength = 2;
66+
} else if (res.length === 2 || res.length === 1) {
67+
res = '';
68+
lastSegmentLength = 0;
69+
lastSlash = i;
70+
dots = 0;
71+
continue;
7272
}
73-
} else {
74-
if (res.length > 0) res += '/' + path.slice(lastSlash + 1, i);else res = path.slice(lastSlash + 1, i);
75-
lastSegmentLength = i - lastSlash - 1;
7673
}
77-
lastSlash = i;
78-
dots = 0;
79-
} else if (code === 46 /*.*/ && dots !== -1) {
74+
if (allowAboveRoot) {
75+
if (res.length > 0)
76+
res += '/..';
77+
else
78+
res = '..';
79+
lastSegmentLength = 2;
80+
}
81+
} else {
82+
if (res.length > 0)
83+
res += '/' + path.slice(lastSlash + 1, i);
84+
else
85+
res = path.slice(lastSlash + 1, i);
86+
lastSegmentLength = i - lastSlash - 1;
87+
}
88+
lastSlash = i;
89+
dots = 0;
90+
} else if (code === 46 /*.*/ && dots !== -1) {
8091
++dots;
8192
} else {
8293
dots = -1;
@@ -106,8 +117,11 @@ var posix = {
106117

107118
for (var i = arguments.length - 1; i >= -1 && !resolvedAbsolute; i--) {
108119
var path;
109-
if (i >= 0) path = arguments[i];else {
110-
if (cwd === undefined) cwd = process.cwd();
120+
if (i >= 0)
121+
path = arguments[i];
122+
else {
123+
if (cwd === undefined)
124+
cwd = process.cwd();
111125
path = cwd;
112126
}
113127

@@ -129,7 +143,10 @@ var posix = {
129143
resolvedPath = normalizeStringPosix(resolvedPath, !resolvedAbsolute);
130144

131145
if (resolvedAbsolute) {
132-
if (resolvedPath.length > 0) return '/' + resolvedPath;else return '/';
146+
if (resolvedPath.length > 0)
147+
return '/' + resolvedPath;
148+
else
149+
return '/';
133150
} else if (resolvedPath.length > 0) {
134151
return resolvedPath;
135152
} else {

test/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@ require('./test-path-dirname');
33
require('./test-path-extname');
44
require('./test-path-join');
55
require('./test-path-relative');
6+
require('./test-path-resolve');

test/test-path-resolve.js

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
'use strict';
2+
var tape = require('tape');
3+
var path = require('../');
4+
5+
var windowsTests =
6+
// arguments result
7+
[[['c:/blah\\blah', 'd:/games', 'c:../a'], 'c:\\blah\\a'],
8+
[['c:/ignore', 'd:\\a/b\\c/d', '\\e.exe'], 'd:\\e.exe'],
9+
[['c:/ignore', 'c:/some/file'], 'c:\\some\\file'],
10+
[['d:/ignore', 'd:some/dir//'], 'd:\\ignore\\some\\dir'],
11+
[['.'], process.cwd()],
12+
[['//server/share', '..', 'relative\\'], '\\\\server\\share\\relative'],
13+
[['c:/', '//'], 'c:\\'],
14+
[['c:/', '//dir'], 'c:\\dir'],
15+
[['c:/', '//server/share'], '\\\\server\\share\\'],
16+
[['c:/', '//server//share'], '\\\\server\\share\\'],
17+
[['c:/', '///some//dir'], 'c:\\some\\dir'],
18+
[['C:\\foo\\tmp.3\\', '..\\tmp.3\\cycles\\root.js'],
19+
'C:\\foo\\tmp.3\\cycles\\root.js']
20+
];
21+
var posixTests =
22+
// arguments result
23+
[[['/var/lib', '../', 'file/'], '/var/file'],
24+
[['/var/lib', '/../', 'file/'], '/file'],
25+
[['a/b/c/', '../../..'], process.cwd()],
26+
[['.'], process.cwd()],
27+
[['/some/dir', '.', '/absolute/'], '/absolute'],
28+
[['/foo/tmp.3/', '../tmp.3/cycles/root.js'], '/foo/tmp.3/cycles/root.js']
29+
];
30+
31+
tape('path.posix.resolve', function (t) {
32+
posixTests.forEach(function (p) {
33+
var actual = path.posix.resolve.apply(null, p[0]);
34+
t.strictEqual(actual, p[1]);
35+
});
36+
t.end();
37+
});
38+
39+
tape('path.win32.resolve', { skip: true }, function (t) {
40+
windowsTests.forEach(function (p) {
41+
var actual = path.win32.resolve.apply(null, p[0]);
42+
t.strictEqual(actual, p[1]);
43+
});
44+
t.end();
45+
});

0 commit comments

Comments
 (0)