Skip to content

Commit

Permalink
path: simplify code and remove obsolete checks
Browse files Browse the repository at this point in the history
Either `end` is `-1` or `startPart` is not `0`. Therefore it's
possible to move the conditions in a way that we eliminate a few code
branches.

PR-URL: #25278
Reviewed-By: Michaël Zasso <[email protected]>
  • Loading branch information
BridgeAR authored and addaleax committed Mar 1, 2019
1 parent 55d6b49 commit e00c8cd
Showing 1 changed file with 25 additions and 34 deletions.
59 changes: 25 additions & 34 deletions lib/path.js
Original file line number Diff line number Diff line change
Expand Up @@ -960,21 +960,20 @@ const win32 = {
}
}

if (startDot === -1 ||
end === -1 ||
// We saw a non-dot character immediately before the dot
preDotState === 0 ||
// The (right-most) trimmed path component is exactly '..'
(preDotState === 1 &&
startDot === end - 1 &&
startDot === startPart + 1)) {
if (end !== -1) {
if (end !== -1) {
if (startDot === -1 ||
// We saw a non-dot character immediately before the dot
preDotState === 0 ||
// The (right-most) trimmed path component is exactly '..'
(preDotState === 1 &&
startDot === end - 1 &&
startDot === startPart + 1)) {
ret.base = ret.name = path.slice(startPart, end);
} else {
ret.name = path.slice(startPart, startDot);
ret.base = path.slice(startPart, end);
ret.ext = path.slice(startDot, end);
}
} else {
ret.name = path.slice(startPart, startDot);
ret.base = path.slice(startPart, end);
ret.ext = path.slice(startDot, end);
}

// If the directory is the root, use the entire root as the `dir` including
Expand Down Expand Up @@ -1380,29 +1379,21 @@ const posix = {
}
}

if (startDot === -1 ||
end === -1 ||
// We saw a non-dot character immediately before the dot
preDotState === 0 ||
// The (right-most) trimmed path component is exactly '..'
(preDotState === 1 &&
startDot === end - 1 &&
startDot === startPart + 1)) {
if (end !== -1) {
if (startPart === 0 && isAbsolute)
ret.base = ret.name = path.slice(1, end);
else
ret.base = ret.name = path.slice(startPart, end);
}
} else {
if (startPart === 0 && isAbsolute) {
ret.name = path.slice(1, startDot);
ret.base = path.slice(1, end);
if (end !== -1) {
const start = startPart === 0 && isAbsolute ? 1 : startPart;
if (startDot === -1 ||
// We saw a non-dot character immediately before the dot
preDotState === 0 ||
// The (right-most) trimmed path component is exactly '..'
(preDotState === 1 &&
startDot === end - 1 &&
startDot === startPart + 1)) {
ret.base = ret.name = path.slice(start, end);
} else {
ret.name = path.slice(startPart, startDot);
ret.base = path.slice(startPart, end);
ret.name = path.slice(start, startDot);
ret.base = path.slice(start, end);
ret.ext = path.slice(startDot, end);
}
ret.ext = path.slice(startDot, end);
}

if (startPart > 0)
Expand Down

0 comments on commit e00c8cd

Please sign in to comment.