Skip to content

Path should format when 'base' missing and 'name' and 'ext' exist #2305

Closed
@reggi

Description

@reggi

This test shows that name and ext are not taken into account when using path.format without a base.

The common usage this is preventing is this:

var src = path.parse(srcpath)
src.base = null
if(src.ext === '') src.ext = '.js'
src = path.format(src)

However this doesn't do anything.

var path = require('path')
var assert = require('assert')

// pulled from https://nodejs.org/api/path.html#path_path_format_pathobject
/* global describe, it */

describe('node path', function () {

  it('should work as documented', function () {
    assert.equal(path.format({
      root: '/',
      dir: '/home/user/dir',
      base: 'file.txt',
      ext: '.txt',
      name: 'file'
    }), '/home/user/dir/file.txt')
  })

  it('should not work if missing base', function () {
    assert.notEqual(path.format({
      root: '/',
      dir: '/home/user/dir',
      ext: '.txt',
      name: 'file'
    }), '/home/user/dir/file.txt')
  })

  it('should show ext and name are irrelevant', function () {
    assert.equal(path.format({
      root: '/',
      dir: '/home/user/dir',
      ext: '.txt',
      name: 'file'
    }), path.format({
      root: '/',
      dir: '/home/user/dir'
    }))
  })

})

https://github.com/nodejs/io.js/blob/master/lib/path.js#L584

I can write a pull request, if anyone else thinks this is a good idea.

Metadata

Metadata

Assignees

No one assigned

    Labels

    docIssues and PRs related to the documentations.feature requestIssues that request new features to be added to Node.js.pathIssues and PRs related to the path subsystem.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions