Skip to content

path.basename accepts ext as any string to remove #44773

Closed
@connorjburton

Description

@connorjburton

Version

18.6.0

Platform

Microsoft Windows NT 10.0.19044.0 x64

Subsystem

No response

What steps will reproduce the bug?

import assert from 'node:assert/strict';
import path from 'path';

assert.deepEqual(path.basename('somerandomchars', 'chars'), 'somerandom');

How often does it reproduce? Is there a required condition?

Always

What is the expected behavior?

Should not accept a non extension string as the ext parameter.

What do you see instead?

I can use path.basename as a generic end of string removal function.

path.basename('somerandomchars', 'chars') removes chars from my path

Additional information

No response

Activity

Trott

Trott commented on Sep 24, 2022

@Trott
Member

IIUC, this is working as designed as path.basename is modeled on the Unix tool basename which works the same way:

$ /usr/bin/basename somerandomchars chars
somerandom
$ 
Trott

Trott commented on Sep 24, 2022

@Trott
Member

I notice that the man page for basename describes the second argument as "suffix" rather than "extension" and it might be a good idea for the Node.js documentation (and code) to follow suit.

connorjburton

connorjburton commented on Sep 24, 2022

@connorjburton
ContributorAuthor

Understood, that makes sense to me. I will raise a PR for this change. I already have one to add more tests for similar behaviour.

Trott

Trott commented on Sep 24, 2022

@Trott
Member

Understood, that makes sense to me. I will raise a PR for this change. I already have one to add more tests for similar behaviour.

Sorry, I already opened the PR for the arg name change: #44774

But if you do incorporate it into the other PR, I'm happy to close mine.

connorjburton

connorjburton commented on Sep 24, 2022

@connorjburton
ContributorAuthor

No it's all good! Thank you for the quick change!

added
pathIssues and PRs related to the path subsystem.
on Sep 26, 2022

6 remaining items

Loading
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    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

      path.basename accepts ext as any string to remove · Issue #44773 · nodejs/node