Skip to content

Commit ebe38ed

Browse files
committed
Port test-path-relative.
1 parent 80e866b commit ebe38ed

File tree

3 files changed

+93
-15
lines changed

3 files changed

+93
-15
lines changed

index.js

Lines changed: 26 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -188,15 +188,17 @@ var posix = {
188188
// Trim any leading backslashes
189189
var fromStart = 1;
190190
for (; fromStart < from.length; ++fromStart) {
191-
if (from.charCodeAt(fromStart) !== 47 /*/*/) break;
191+
if (from.charCodeAt(fromStart) !== 47 /*/*/)
192+
break;
192193
}
193194
var fromEnd = from.length;
194195
var fromLen = fromEnd - fromStart;
195196

196197
// Trim any leading backslashes
197198
var toStart = 1;
198199
for (; toStart < to.length; ++toStart) {
199-
if (to.charCodeAt(toStart) !== 47 /*/*/) break;
200+
if (to.charCodeAt(toStart) !== 47 /*/*/)
201+
break;
200202
}
201203
var toEnd = to.length;
202204
var toLen = toEnd - toStart;
@@ -209,20 +211,20 @@ var posix = {
209211
if (i === length) {
210212
if (toLen > length) {
211213
if (to.charCodeAt(toStart + i) === 47 /*/*/) {
212-
// We get here if `from` is the exact base path for `to`.
213-
// For example: from='/foo/bar'; to='/foo/bar/baz'
214-
return to.slice(toStart + i + 1);
215-
} else if (i === 0) {
214+
// We get here if `from` is the exact base path for `to`.
215+
// For example: from='/foo/bar'; to='/foo/bar/baz'
216+
return to.slice(toStart + i + 1);
217+
} else if (i === 0) {
216218
// We get here if `from` is the root
217219
// For example: from='/'; to='/foo'
218220
return to.slice(toStart + i);
219221
}
220222
} else if (fromLen > length) {
221223
if (from.charCodeAt(fromStart + i) === 47 /*/*/) {
222-
// We get here if `to` is the exact base path for `from`.
223-
// For example: from='/foo/bar/baz'; to='/foo/bar'
224-
lastCommonSep = i;
225-
} else if (i === 0) {
224+
// We get here if `to` is the exact base path for `from`.
225+
// For example: from='/foo/bar/baz'; to='/foo/bar'
226+
lastCommonSep = i;
227+
} else if (i === 0) {
226228
// We get here if `to` is the root.
227229
// For example: from='/foo'; to='/'
228230
lastCommonSep = 0;
@@ -232,23 +234,32 @@ var posix = {
232234
}
233235
var fromCode = from.charCodeAt(fromStart + i);
234236
var toCode = to.charCodeAt(toStart + i);
235-
if (fromCode !== toCode) break;else if (fromCode === 47 /*/*/) lastCommonSep = i;
237+
if (fromCode !== toCode)
238+
break;
239+
else if (fromCode === 47 /*/*/)
240+
lastCommonSep = i;
236241
}
237242

238243
var out = '';
239244
// Generate the relative path based on the path difference between `to`
240245
// and `from`
241246
for (i = fromStart + lastCommonSep + 1; i <= fromEnd; ++i) {
242247
if (i === fromEnd || from.charCodeAt(i) === 47 /*/*/) {
243-
if (out.length === 0) out += '..';else out += '/..';
244-
}
248+
if (out.length === 0)
249+
out += '..';
250+
else
251+
out += '/..';
252+
}
245253
}
246254

247255
// Lastly, append the rest of the destination (`to`) path that comes after
248256
// the common path parts
249-
if (out.length > 0) return out + to.slice(toStart + lastCommonSep);else {
257+
if (out.length > 0)
258+
return out + to.slice(toStart + lastCommonSep);
259+
else {
250260
toStart += lastCommonSep;
251-
if (to.charCodeAt(toStart) === 47 /*/*/) ++toStart;
261+
if (to.charCodeAt(toStart) === 47 /*/*/)
262+
++toStart;
252263
return to.slice(toStart);
253264
}
254265
},

test/index.js

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

test/test-path-relative.js

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
'use strict';
2+
var tape = require('tape');
3+
var path = require('../');
4+
5+
var relativeTests = {
6+
win32:
7+
// arguments result
8+
[['c:/blah\\blah', 'd:/games', 'd:\\games'],
9+
['c:/aaaa/bbbb', 'c:/aaaa', '..'],
10+
['c:/aaaa/bbbb', 'c:/cccc', '..\\..\\cccc'],
11+
['c:/aaaa/bbbb', 'c:/aaaa/bbbb', ''],
12+
['c:/aaaa/bbbb', 'c:/aaaa/cccc', '..\\cccc'],
13+
['c:/aaaa/', 'c:/aaaa/cccc', 'cccc'],
14+
['c:/', 'c:\\aaaa\\bbbb', 'aaaa\\bbbb'],
15+
['c:/aaaa/bbbb', 'd:\\', 'd:\\'],
16+
['c:/AaAa/bbbb', 'c:/aaaa/bbbb', ''],
17+
['c:/aaaaa/', 'c:/aaaa/cccc', '..\\aaaa\\cccc'],
18+
['C:\\foo\\bar\\baz\\quux', 'C:\\', '..\\..\\..\\..'],
19+
['C:\\foo\\test', 'C:\\foo\\test\\bar\\package.json', 'bar\\package.json'],
20+
['C:\\foo\\bar\\baz-quux', 'C:\\foo\\bar\\baz', '..\\baz'],
21+
['C:\\foo\\bar\\baz', 'C:\\foo\\bar\\baz-quux', '..\\baz-quux'],
22+
['\\\\foo\\bar', '\\\\foo\\bar\\baz', 'baz'],
23+
['\\\\foo\\bar\\baz', '\\\\foo\\bar', '..'],
24+
['\\\\foo\\bar\\baz-quux', '\\\\foo\\bar\\baz', '..\\baz'],
25+
['\\\\foo\\bar\\baz', '\\\\foo\\bar\\baz-quux', '..\\baz-quux'],
26+
['C:\\baz-quux', 'C:\\baz', '..\\baz'],
27+
['C:\\baz', 'C:\\baz-quux', '..\\baz-quux'],
28+
['\\\\foo\\baz-quux', '\\\\foo\\baz', '..\\baz'],
29+
['\\\\foo\\baz', '\\\\foo\\baz-quux', '..\\baz-quux'],
30+
['C:\\baz', '\\\\foo\\bar\\baz', '\\\\foo\\bar\\baz'],
31+
['\\\\foo\\bar\\baz', 'C:\\baz', 'C:\\baz']
32+
],
33+
posix:
34+
// arguments result
35+
[['/var/lib', '/var', '..'],
36+
['/var/lib', '/bin', '../../bin'],
37+
['/var/lib', '/var/lib', ''],
38+
['/var/lib', '/var/apache', '../apache'],
39+
['/var/', '/var/lib', 'lib'],
40+
['/', '/var/lib', 'var/lib'],
41+
['/foo/test', '/foo/test/bar/package.json', 'bar/package.json'],
42+
['/Users/a/web/b/test/mails', '/Users/a/web/b', '../..'],
43+
['/foo/bar/baz-quux', '/foo/bar/baz', '../baz'],
44+
['/foo/bar/baz', '/foo/bar/baz-quux', '../baz-quux'],
45+
['/baz-quux', '/baz', '../baz'],
46+
['/baz', '/baz-quux', '../baz-quux']
47+
]
48+
};
49+
50+
tape('path.posix.relative', function (t) {
51+
relativeTests.posix.forEach(function (p) {
52+
var expected = p[2];
53+
var actual = path.posix.relative(p[0], p[1]);
54+
t.strictEqual(actual, expected);
55+
});
56+
t.end();
57+
});
58+
59+
tape('path.win32.relative', { skip: true }, function (t) {
60+
relativeTests.win32.forEach(function (p) {
61+
var expected = p[2];
62+
var actual = path.win32.relative(p[0], p[1]);
63+
t.strictEqual(actual, expected);
64+
});
65+
t.end();
66+
});

0 commit comments

Comments
 (0)