-
Notifications
You must be signed in to change notification settings - Fork 29.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Improve Path.parse / Path.format combo #1999
Comments
So your suggestion is that base becomes a getter/setter? That doesn't sound too bad but I wonder what the impact on backwards compatibility is |
Examples: // CURRENT BEHAVIOR
let path0 = "/Users/ivankleshnin/Projects/demo/config.yml";
let parsed0 = Path.parse(path0);
console.log(Path.format(parsed0)); // /Users/ivankleshnin/Projects/demo/config.yml (+)
parsed0.name = "xxx";
console.log(Path.format(parsed0)); // /Users/ivankleshnin/Projects/demo/config.yml (-)
parsed0.ext = ".json";
console.log(Path.format(parsed0)); // /Users/ivankleshnin/Projects/demo/config.yml (-)
parsed0.base = "test.html";
console.log(Path.format(parsed0)); // /Users/ivankleshnin/Projects/demo/test.html (+)
// NEW BEHAVIOR
let path1 = "/Users/ivankleshnin/Projects/demo/config.yml";
let parsed1 = newParse(path1);
console.log(Path.format(parsed1)); // /Users/ivankleshnin/Projects/demo/config.yml (+)
parsed1.name = "xxx";
console.log(Path.format(parsed1)); // /Users/ivankleshnin/Projects/demo/xxx.yml (+)
parsed1.ext = ".json";
console.log(Path.format(parsed1)); // /Users/ivankleshnin/Projects/demo/xxx.json (+)
parsed1.base = "test.html";
console.log(Path.format(parsed1)); // /Users/ivankleshnin/Projects/demo/test.html (+) Proof implementation: ...
function newParse(pathString) {
...
Object.defineProperty(parsed, "base", {
enumerable: true,
configurable: false,
get: function () {
return this.name + this.ext;
},
set: function (value) {
if (value.startsWith(".") || !value.includes(".")) {
this.name = value;
this.ext = "";
} else {
let [name, ext] = value.split(".");
this.name = name;
this.ext = "." + ext;
}
}
});
return parsed;
} Should be fully backward compatible, unless I miss something. To be precise: will break code which depends on Platform Requirements(of possible feature implementation, not a provided gist) Basically: IE 9+ Note: I'm not aware of platform support requirements of IO JS. |
IMO this sort of functionality should be implemented in user-land (npm). The |
@nodejs/collaborators anyone wants to promote this or should we close the issue? |
The current behaviour is indeed bizarre, it one of the things I talk about in https://www.youtube.com/watch?v=jJaIwea8r2A Its not only strange in and of itself, its also inconsistent with node's url.parse/url.format. @nwoltman Is your suggestion to deprecate the |
I'm interested. |
@sam-github In case it helps, the example you gave from your talk: const path = require('path');
const bits = path.parse('some/dir/index.txt');
console.log(bits.base); // > index.txt
delete bits.base;
bits.ext = '.html';
console.log(path.format(bits)); does work like the
I didn't mean to suggest to deprecate the path module. What I meant was that "extended" functionality (such as having getters/setters on the object returned by |
I have a faint memory that all the logic was in |
make `parse` return an object where `base` is a computed property Ref: nodejs#1999
Is the consensus here that this should be a userland npm module? Or is this a bug in Node.js that should be fixed? |
I think the general consensus established in #12818 was that |
#12511 is half way there. It keeps blinking in and out of my focus... AFAICT it's a less complicated issue than |
We have
Path.format
/Path.parse
functions.They can be chained which is very convenient.
Currently
parse
converts string to an object with such structureThis object contains denormalized data between
base
,name
andext
key values.Now let's try to replace file extension.
The simplest task is going to be not so simple?!
Now if
format
took into considerationext
andname
rather thanbase
this could lead to an equal problem with changes tobase
key being ignored.Can we get rid of this
base
key? It's alwaysparsed.name + parsed.ext
formula, not a big deal to make it manually. Example of hidden file parse:{ base: '.gitignore', ext: '', name: '.gitignore' }
- same rule apply.We can probably also implement it in a backward-compatibile way,
keeping
base
but using JS getter / setter for it's evaluation.The text was updated successfully, but these errors were encountered: