Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit f5d22a2

Browse files
committedJul 23, 2017
path: improve parse => format combination
make `parse` return an object where `base` is a computed property Ref: nodejs#1999
1 parent 69f653d commit f5d22a2

File tree

1 file changed

+40
-22
lines changed

1 file changed

+40
-22
lines changed
 

‎lib/path.js

Lines changed: 40 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1114,19 +1114,12 @@ const win32 = {
11141114
startDot === end - 1 &&
11151115
startDot === startPart + 1)) {
11161116
if (end !== -1) {
1117-
if (startPart === 0 && isAbsolute)
1118-
ret.base = ret.name = path.slice(rootEnd, end);
1119-
else
1120-
ret.base = ret.name = path.slice(startPart, end);
1117+
const start = (startPart === 0 && isAbsolute) ? rootEnd : startPart;
1118+
ret.name = path.slice(start, end);
11211119
}
11221120
} else {
1123-
if (startPart === 0 && isAbsolute) {
1124-
ret.name = path.slice(rootEnd, startDot);
1125-
ret.base = path.slice(rootEnd, end);
1126-
} else {
1127-
ret.name = path.slice(startPart, startDot);
1128-
ret.base = path.slice(startPart, end);
1129-
}
1121+
const start = (startPart === 0 && isAbsolute) ? rootEnd : startPart;
1122+
ret.name = path.slice(start, startDot);
11301123
ret.ext = path.slice(startDot, end);
11311124
}
11321125

@@ -1135,6 +1128,22 @@ const win32 = {
11351128
else if (isAbsolute)
11361129
ret.dir = path.slice(0, rootEnd);
11371130

1131+
Object.defineProperty(ret, 'base', {
1132+
enumerable: true,
1133+
configurable: false,
1134+
get() { return this.name + this.ext; },
1135+
set(value) {
1136+
const li = value.lastIndexOf('.');
1137+
if (li > 0) {
1138+
this.ext = value.slice(li);
1139+
this.name = value.slice(0, li);
1140+
} else {
1141+
this.name = value;
1142+
this.ext = '';
1143+
}
1144+
}
1145+
});
1146+
11381147
return ret;
11391148
},
11401149

@@ -1574,19 +1583,12 @@ const posix = {
15741583
startDot === end - 1 &&
15751584
startDot === startPart + 1)) {
15761585
if (end !== -1) {
1577-
if (startPart === 0 && isAbsolute)
1578-
ret.base = ret.name = path.slice(1, end);
1579-
else
1580-
ret.base = ret.name = path.slice(startPart, end);
1586+
const start = (startPart === 0 && isAbsolute) ? 1 : startPart;
1587+
ret.name = path.slice(start, end);
15811588
}
15821589
} else {
1583-
if (startPart === 0 && isAbsolute) {
1584-
ret.name = path.slice(1, startDot);
1585-
ret.base = path.slice(1, end);
1586-
} else {
1587-
ret.name = path.slice(startPart, startDot);
1588-
ret.base = path.slice(startPart, end);
1589-
}
1590+
const start = (startPart === 0 && isAbsolute) ? 1 : startPart;
1591+
ret.name = path.slice(start, startDot);
15901592
ret.ext = path.slice(startDot, end);
15911593
}
15921594

@@ -1595,6 +1597,22 @@ const posix = {
15951597
else if (isAbsolute)
15961598
ret.dir = '/';
15971599

1600+
Object.defineProperty(ret, 'base', {
1601+
enumerable: true,
1602+
configurable: false,
1603+
get() { return this.name + this.ext; },
1604+
set(value) {
1605+
const li = value.lastIndexOf('.');
1606+
if (li > 0) {
1607+
this.ext = value.slice(li);
1608+
this.name = value.slice(0, li);
1609+
} else {
1610+
this.name = value;
1611+
this.ext = '';
1612+
}
1613+
}
1614+
});
1615+
15981616
return ret;
15991617
},
16001618

0 commit comments

Comments
 (0)
Please sign in to comment.