Skip to content

Commit

Permalink
path: improve parse => format combination
Browse files Browse the repository at this point in the history
make `parse` return an object where `base` is a computed property

Ref: nodejs#1999
  • Loading branch information
refack committed Jul 23, 2017
1 parent 69f653d commit f5d22a2
Showing 1 changed file with 40 additions and 22 deletions.
62 changes: 40 additions & 22 deletions lib/path.js
Original file line number Diff line number Diff line change
Expand Up @@ -1114,19 +1114,12 @@ const win32 = {
startDot === end - 1 &&
startDot === startPart + 1)) {
if (end !== -1) {
if (startPart === 0 && isAbsolute)
ret.base = ret.name = path.slice(rootEnd, end);
else
ret.base = ret.name = path.slice(startPart, end);
const start = (startPart === 0 && isAbsolute) ? rootEnd : startPart;
ret.name = path.slice(start, end);
}
} else {
if (startPart === 0 && isAbsolute) {
ret.name = path.slice(rootEnd, startDot);
ret.base = path.slice(rootEnd, end);
} else {
ret.name = path.slice(startPart, startDot);
ret.base = path.slice(startPart, end);
}
const start = (startPart === 0 && isAbsolute) ? rootEnd : startPart;
ret.name = path.slice(start, startDot);
ret.ext = path.slice(startDot, end);
}

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

Object.defineProperty(ret, 'base', {
enumerable: true,
configurable: false,
get() { return this.name + this.ext; },
set(value) {
const li = value.lastIndexOf('.');
if (li > 0) {
this.ext = value.slice(li);
this.name = value.slice(0, li);
} else {
this.name = value;
this.ext = '';
}
}
});

return ret;
},

Expand Down Expand Up @@ -1574,19 +1583,12 @@ const posix = {
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);
const start = (startPart === 0 && isAbsolute) ? 1 : startPart;
ret.name = path.slice(start, end);
}
} else {
if (startPart === 0 && isAbsolute) {
ret.name = path.slice(1, startDot);
ret.base = path.slice(1, end);
} else {
ret.name = path.slice(startPart, startDot);
ret.base = path.slice(startPart, end);
}
const start = (startPart === 0 && isAbsolute) ? 1 : startPart;
ret.name = path.slice(start, startDot);
ret.ext = path.slice(startDot, end);
}

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

Object.defineProperty(ret, 'base', {
enumerable: true,
configurable: false,
get() { return this.name + this.ext; },
set(value) {
const li = value.lastIndexOf('.');
if (li > 0) {
this.ext = value.slice(li);
this.name = value.slice(0, li);
} else {
this.name = value;
this.ext = '';
}
}
});

return ret;
},

Expand Down

0 comments on commit f5d22a2

Please sign in to comment.