Description
We have Path.format
/ Path.parse
functions.
They can be chained which is very convenient.
import Path from "path";
let path = "/foo/bar/bazz.js";
let pathP = Path.format(Path.parse(path));
Currently parse
converts string to an object with such structure
{ root: '/',
dir: '/Users/ivankleshnin/Projects/demo',
base: 'config.yml',
ext: '.yml',
name: 'config' }
This object contains denormalized data between base
, name
and ext
key values.
Now let's try to replace file extension.
import Path from "path";
import {assoc} from "ramda"
let path = "/Users/ivankleshnin/Projects/demo/config.js";
let pathP = assoc("ext", ".json", Path.parse(path));
/* {...
base: 'config.js', -- these two are
ext: '.json', -- unsynced!
...} */
console.log(Path.format(pathP)); // extension weren't changed :(
The simplest task is going to be not so simple?!
Now if format
took into consideration ext
and name
rather than base
this could lead to an equal problem with changes to base
key being ignored.
Can we get rid of this base
key? It's always parsed.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.
Activity
benjamingr commentedon Jun 17, 2015
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
ivan-kleshnin commentedon Jun 17, 2015
Examples:
Proof implementation:
Self-contained working gist
Should be fully backward compatible, unless I miss something.
To be precise: will break code which depends on
base
changes not propagated toname
andext
i.e. on the "buggy" aspect of current behavior. Hard to imagine such code IMO.Platform Requirements
(of possible feature implementation, not a provided gist)
https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Object/defineProperty#Configurable_attribute
Basically: IE 9+
Note: I'm not aware of platform support requirements of IO JS.
nwoltman commentedon Feb 9, 2016
IMO this sort of functionality should be implemented in user-land (npm). The
path.parse
function is just a convenient way to split a path into its main components andpath.format
is mainly for completeness so we can return the path components to the original path string (see the original issue).benjamingr commentedon Apr 17, 2017
@nodejs/collaborators anyone wants to promote this or should we close the issue?
sam-github commentedon Apr 17, 2017
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
path
module and promote an npm module to take its place?refack commentedon Apr 17, 2017
I'm interested.
nwoltman commentedon Apr 17, 2017
@sam-github In case it helps, the example you gave from your talk:
does work like the
url
module now, so it outputs'some/dir/index.html'
. Also,path.format()
is documented well enough now that people should know what to expect when using it.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
path.parse()
) should be provided by an npm module. There's a similar situation with thequerystring
core module where there's an npm module calledqs
that provides more functionality than the core module.refack commentedon Apr 17, 2017
I have a faint memory that all the logic was in
parse
and the others just projected parts of theparse
resault... Now I'm confused why is there quadruple duplication (win
/posix
×parse
/specific)?49 remaining items