Skip to content

Commit

Permalink
path: using .relative() should not return a trailing slash
Browse files Browse the repository at this point in the history
Resolving a path against root with `path.relative()` should not
include a trailing slash.

Fixes: #28549

PR-URL: #28556
Reviewed-By: Rich Trott <rtrott@gmail.com>
Reviewed-By: Weijia Wang <starkwang@126.com>
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Jiawen Geng <technicalcute@gmail.com>
Reviewed-By: Yongsheng Zhang <zyszys98@gmail.com>
  • Loading branch information
BridgeAR authored and ZYSzys committed Jul 14, 2019
1 parent c31f7e5 commit 5a6aa66
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 6 deletions.
15 changes: 10 additions & 5 deletions lib/path.js
Expand Up @@ -1090,11 +1090,16 @@ const posix = {
// For example: from='/'; to='/foo'
return to.slice(toStart + i);
}
} else if (fromLen > length &&
from.charCodeAt(fromStart + i) === CHAR_FORWARD_SLASH) {
// We get here if `to` is the exact base path for `from`.
// For example: from='/foo/bar/baz'; to='/foo/bar'
lastCommonSep = i;
} else if (fromLen > length) {
if (from.charCodeAt(fromStart + i) === CHAR_FORWARD_SLASH) {
// We get here if `to` is the exact base path for `from`.
// For example: from='/foo/bar/baz'; to='/foo/bar'
lastCommonSep = i;
} else if (i === 0) {
// We get here if `to` is the root.
// For example: from='/foo/bar'; to='/'
lastCommonSep = 0;
}
}
}

Expand Down
3 changes: 2 additions & 1 deletion test/parallel/test-path-relative.js
Expand Up @@ -47,7 +47,8 @@ const relativeTests = [
['/foo/bar/baz-quux', '/foo/bar/baz', '../baz'],
['/foo/bar/baz', '/foo/bar/baz-quux', '../baz-quux'],
['/baz-quux', '/baz', '../baz'],
['/baz', '/baz-quux', '../baz-quux']
['/baz', '/baz-quux', '../baz-quux'],
['/page1/page2/foo', '/', '../../..']
]
]
];
Expand Down

0 comments on commit 5a6aa66

Please sign in to comment.