Skip to content

Commit

Permalink
fix(core/path): remove suffix in basename only once (#9166)
Browse files Browse the repository at this point in the history
* fix(core/path): remove suffix in basename only once

ref: #9064

* Update tooling/api/src/path.ts

---------

Co-authored-by: Lucas Fernandes Nogueira <lucas@tauri.app>
  • Loading branch information
amrbashir and lucasfernog committed Mar 18, 2024
1 parent b705f89 commit e3b6d38
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 19 deletions.
6 changes: 6 additions & 0 deletions .changes/core-path-basename-replace.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'tauri': 'patch:bug'
'@tauri-apps/api': patch:bug
---

Fix `basename(path, 'ext')` JS API when removing all occurances of `ext` where it should only remove the last one.
20 changes: 11 additions & 9 deletions core/tauri/src/endpoints/path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -150,15 +150,17 @@ impl Cmd {
path: String,
ext: Option<String>,
) -> super::Result<String> {
match Path::new(&path)
.file_name()
.and_then(std::ffi::OsStr::to_str)
{
Some(p) => Ok(if let Some(ext) = ext {
p.replace(ext.as_str(), "")
} else {
p.to_string()
}),
let file_name = Path::new(&path).file_name().map(|f| f.to_string_lossy());
match file_name {
Some(p) => {
let maybe_stripped = if let Some(ext) = ext {
p.strip_suffix(&ext).unwrap_or(&p).to_string()
} else {
p.to_string()
};
Ok(maybe_stripped)
}

None => Err(crate::error::into_anyhow(crate::api::Error::Path(
"Couldn't get the basename".into(),
))),
Expand Down
18 changes: 8 additions & 10 deletions tooling/api/src/path.ts
Original file line number Diff line number Diff line change
Expand Up @@ -726,9 +726,9 @@ async function join(...paths: string[]): Promise<string> {
* Returns the directory name of a `path`. Trailing directory separators are ignored.
* @example
* ```typescript
* import { dirname, appDataDir } from '@tauri-apps/api/path';
* const appDataDirPath = await appDataDir();
* const dir = await dirname(appDataDirPath);
* import { dirname } from '@tauri-apps/api/path';
* const dir = await dirname('/path/to/somedir/');
* assert(dir === 'somedir');
* ```
*
* @since 1.0.0
Expand All @@ -747,10 +747,9 @@ async function dirname(path: string): Promise<string> {
* Returns the extension of the `path`.
* @example
* ```typescript
* import { extname, resolveResource } from '@tauri-apps/api/path';
* const resourcePath = await resolveResource('app.conf');
* const ext = await extname(resourcePath);
* assert(ext === 'conf');
* import { extname } from '@tauri-apps/api/path';
* const ext = await extname('/path/to/file.html');
* assert(ext === 'html');
* ```
*
* @since 1.0.0
Expand All @@ -769,9 +768,8 @@ async function extname(path: string): Promise<string> {
* Returns the last portion of a `path`. Trailing directory separators are ignored.
* @example
* ```typescript
* import { basename, resolveResource } from '@tauri-apps/api/path';
* const resourcePath = await resolveResource('app.conf');
* const base = await basename(resourcePath);
* import { basename } from '@tauri-apps/api/path';
* const base = await basename('path/to/app.conf');
* assert(base === 'app.conf');
* ```
*
Expand Down

0 comments on commit e3b6d38

Please sign in to comment.