Skip to content

Commit e3b6d38

Browse files
fix(core/path): remove suffix in basename only once (#9166)
* 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>
1 parent b705f89 commit e3b6d38

File tree

3 files changed

+25
-19
lines changed

3 files changed

+25
-19
lines changed
+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
'tauri': 'patch:bug'
3+
'@tauri-apps/api': patch:bug
4+
---
5+
6+
Fix `basename(path, 'ext')` JS API when removing all occurances of `ext` where it should only remove the last one.

core/tauri/src/endpoints/path.rs

+11-9
Original file line numberDiff line numberDiff line change
@@ -150,15 +150,17 @@ impl Cmd {
150150
path: String,
151151
ext: Option<String>,
152152
) -> super::Result<String> {
153-
match Path::new(&path)
154-
.file_name()
155-
.and_then(std::ffi::OsStr::to_str)
156-
{
157-
Some(p) => Ok(if let Some(ext) = ext {
158-
p.replace(ext.as_str(), "")
159-
} else {
160-
p.to_string()
161-
}),
153+
let file_name = Path::new(&path).file_name().map(|f| f.to_string_lossy());
154+
match file_name {
155+
Some(p) => {
156+
let maybe_stripped = if let Some(ext) = ext {
157+
p.strip_suffix(&ext).unwrap_or(&p).to_string()
158+
} else {
159+
p.to_string()
160+
};
161+
Ok(maybe_stripped)
162+
}
163+
162164
None => Err(crate::error::into_anyhow(crate::api::Error::Path(
163165
"Couldn't get the basename".into(),
164166
))),

tooling/api/src/path.ts

+8-10
Original file line numberDiff line numberDiff line change
@@ -726,9 +726,9 @@ async function join(...paths: string[]): Promise<string> {
726726
* Returns the directory name of a `path`. Trailing directory separators are ignored.
727727
* @example
728728
* ```typescript
729-
* import { dirname, appDataDir } from '@tauri-apps/api/path';
730-
* const appDataDirPath = await appDataDir();
731-
* const dir = await dirname(appDataDirPath);
729+
* import { dirname } from '@tauri-apps/api/path';
730+
* const dir = await dirname('/path/to/somedir/');
731+
* assert(dir === 'somedir');
732732
* ```
733733
*
734734
* @since 1.0.0
@@ -747,10 +747,9 @@ async function dirname(path: string): Promise<string> {
747747
* Returns the extension of the `path`.
748748
* @example
749749
* ```typescript
750-
* import { extname, resolveResource } from '@tauri-apps/api/path';
751-
* const resourcePath = await resolveResource('app.conf');
752-
* const ext = await extname(resourcePath);
753-
* assert(ext === 'conf');
750+
* import { extname } from '@tauri-apps/api/path';
751+
* const ext = await extname('/path/to/file.html');
752+
* assert(ext === 'html');
754753
* ```
755754
*
756755
* @since 1.0.0
@@ -769,9 +768,8 @@ async function extname(path: string): Promise<string> {
769768
* Returns the last portion of a `path`. Trailing directory separators are ignored.
770769
* @example
771770
* ```typescript
772-
* import { basename, resolveResource } from '@tauri-apps/api/path';
773-
* const resourcePath = await resolveResource('app.conf');
774-
* const base = await basename(resourcePath);
771+
* import { basename } from '@tauri-apps/api/path';
772+
* const base = await basename('path/to/app.conf');
775773
* assert(base === 'app.conf');
776774
* ```
777775
*

0 commit comments

Comments
 (0)