Skip to content

Improve Path.parse / Path.format combo #1999

Closed
@ivan-kleshnin

Description

@ivan-kleshnin

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    feature requestIssues that request new features to be added to Node.js.pathIssues and PRs related to the path subsystem.staletest-action

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions