Skip to content
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

path: change basename() argument from ext to suffix #44774

Merged
merged 1 commit into from Sep 27, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 5 additions & 5 deletions doc/api/path.md
Expand Up @@ -62,7 +62,7 @@ example, `path.resolve('C:\\')` can potentially return a different result than
`path.resolve('C:')`. For more information, see
[this MSDN page][MSDN-Rel-Path].

## `path.basename(path[, ext])`
## `path.basename(path[, suffix])`

<!-- YAML
added: v0.1.25
Expand All @@ -73,12 +73,12 @@ changes:
-->

* `path` {string}
* `ext` {string} An optional file extension
* `suffix` {string} An optional suffix to remove
* Returns: {string}

The `path.basename()` method returns the last portion of a `path`, similar to
the Unix `basename` command. Trailing directory separators are ignored, see
[`path.sep`][].
the Unix `basename` command. Trailing [directory separators][`path.sep`] are
ignored.

```js
path.basename('/foo/bar/baz/asdf/quux.html');
Expand All @@ -101,7 +101,7 @@ path.win32.basename('C:\\foo.HTML', '.html');
// Returns: 'foo.HTML'
```

A [`TypeError`][] is thrown if `path` is not a string or if `ext` is given
A [`TypeError`][] is thrown if `path` is not a string or if `suffix` is given
and is not a string.

## `path.delimiter`
Expand Down
32 changes: 16 additions & 16 deletions lib/path.js
Expand Up @@ -743,12 +743,12 @@ const win32 = {

/**
* @param {string} path
* @param {string} [ext]
* @param {string} [suffix]
* @returns {string}
*/
basename(path, ext) {
if (ext !== undefined)
validateString(ext, 'ext');
basename(path, suffix) {
if (suffix !== undefined)
validateString(suffix, 'ext');
validateString(path, 'path');
let start = 0;
let end = -1;
Expand All @@ -763,10 +763,10 @@ const win32 = {
start = 2;
}

if (ext !== undefined && ext.length > 0 && ext.length <= path.length) {
if (ext === path)
if (suffix !== undefined && suffix.length > 0 && suffix.length <= path.length) {
if (suffix === path)
return '';
let extIdx = ext.length - 1;
let extIdx = suffix.length - 1;
let firstNonSlashEnd = -1;
for (let i = path.length - 1; i >= start; --i) {
const code = StringPrototypeCharCodeAt(path, i);
Expand All @@ -786,7 +786,7 @@ const win32 = {
}
if (extIdx >= 0) {
// Try to match the explicit extension
if (code === StringPrototypeCharCodeAt(ext, extIdx)) {
if (code === StringPrototypeCharCodeAt(suffix, extIdx)) {
if (--extIdx === -1) {
// We matched the extension, so mark this as the end of our path
// component
Expand Down Expand Up @@ -1300,22 +1300,22 @@ const posix = {

/**
* @param {string} path
* @param {string} [ext]
* @param {string} [suffix]
* @returns {string}
*/
basename(path, ext) {
if (ext !== undefined)
validateString(ext, 'ext');
basename(path, suffix) {
if (suffix !== undefined)
validateString(suffix, 'ext');
validateString(path, 'path');

let start = 0;
let end = -1;
let matchedSlash = true;

if (ext !== undefined && ext.length > 0 && ext.length <= path.length) {
if (ext === path)
if (suffix !== undefined && suffix.length > 0 && suffix.length <= path.length) {
if (suffix === path)
return '';
let extIdx = ext.length - 1;
let extIdx = suffix.length - 1;
let firstNonSlashEnd = -1;
for (let i = path.length - 1; i >= 0; --i) {
const code = StringPrototypeCharCodeAt(path, i);
Expand All @@ -1335,7 +1335,7 @@ const posix = {
}
if (extIdx >= 0) {
// Try to match the explicit extension
if (code === StringPrototypeCharCodeAt(ext, extIdx)) {
if (code === StringPrototypeCharCodeAt(suffix, extIdx)) {
if (--extIdx === -1) {
// We matched the extension, so mark this as the end of our path
// component
Expand Down