mirror of
https://github.com/tauri-apps/tauri.git
synced 2024-08-16 19:30:27 +03:00
fix(core/path): remove suffix in basename
only once (#9165)
* fix(core/path): remove suffix in `basename` only once closes #9064 * Update tooling/api/src/path.ts * remove extra assert --------- Co-authored-by: Lucas Fernandes Nogueira <lucas@tauri.app>
This commit is contained in:
parent
ea0242db4a
commit
379cc2b354
6
.changes/core-path-basename-replace.md
Normal file
6
.changes/core-path-basename-replace.md
Normal file
@ -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.
|
@ -179,16 +179,17 @@ pub fn extname(path: String) -> Result<String> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[command(root = "crate")]
|
#[command(root = "crate")]
|
||||||
pub fn basename(path: String, ext: Option<String>) -> Result<String> {
|
pub fn basename(path: &str, ext: Option<&str>) -> Result<String> {
|
||||||
match Path::new(&path)
|
let file_name = Path::new(path).file_name().map(|f| f.to_string_lossy());
|
||||||
.file_name()
|
match file_name {
|
||||||
.and_then(std::ffi::OsStr::to_str)
|
Some(p) => {
|
||||||
{
|
let maybe_stripped = if let Some(ext) = ext {
|
||||||
Some(p) => Ok(if let Some(ext) = ext {
|
p.strip_suffix(ext).unwrap_or(&p).to_string()
|
||||||
p.replace(ext.as_str(), "")
|
} else {
|
||||||
} else {
|
p.to_string()
|
||||||
p.to_string()
|
};
|
||||||
}),
|
Ok(maybe_stripped)
|
||||||
|
}
|
||||||
None => Err(Error::NoBasename),
|
None => Err(Error::NoBasename),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -245,3 +246,40 @@ pub(crate) fn init<R: Runtime>() -> TauriPlugin<R> {
|
|||||||
})
|
})
|
||||||
.build()
|
.build()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn basename() {
|
||||||
|
let path = "/path/to/some-json-file.json";
|
||||||
|
assert_eq!(
|
||||||
|
super::basename(path, Some(".json")).unwrap(),
|
||||||
|
"some-json-file"
|
||||||
|
);
|
||||||
|
|
||||||
|
let path = "/path/to/some-json-file.json";
|
||||||
|
assert_eq!(
|
||||||
|
super::basename(path, Some("json")).unwrap(),
|
||||||
|
"some-json-file."
|
||||||
|
);
|
||||||
|
|
||||||
|
let path = "/path/to/some-json-file.html.json";
|
||||||
|
assert_eq!(
|
||||||
|
super::basename(path, Some(".json")).unwrap(),
|
||||||
|
"some-json-file.html"
|
||||||
|
);
|
||||||
|
|
||||||
|
let path = "/path/to/some-json-file.json.json";
|
||||||
|
assert_eq!(
|
||||||
|
super::basename(path, Some(".json")).unwrap(),
|
||||||
|
"some-json-file.json"
|
||||||
|
);
|
||||||
|
|
||||||
|
let path = "/path/to/some-json-file.json.html";
|
||||||
|
assert_eq!(
|
||||||
|
super::basename(path, Some(".json")).unwrap(),
|
||||||
|
"some-json-file.json.html"
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@ -609,9 +609,9 @@ async function join(...paths: string[]): Promise<string> {
|
|||||||
* Returns the directory name of a `path`. Trailing directory separators are ignored.
|
* Returns the directory name of a `path`. Trailing directory separators are ignored.
|
||||||
* @example
|
* @example
|
||||||
* ```typescript
|
* ```typescript
|
||||||
* import { dirname, appDataDir } from '@tauri-apps/api/path';
|
* import { dirname } from '@tauri-apps/api/path';
|
||||||
* const appDataDirPath = await appDataDir();
|
* const dir = await dirname('/path/to/somedir/');
|
||||||
* const dir = await dirname(appDataDirPath);
|
* assert(dir === 'somedir');
|
||||||
* ```
|
* ```
|
||||||
*
|
*
|
||||||
* @since 1.0.0
|
* @since 1.0.0
|
||||||
@ -624,10 +624,9 @@ async function dirname(path: string): Promise<string> {
|
|||||||
* Returns the extension of the `path`.
|
* Returns the extension of the `path`.
|
||||||
* @example
|
* @example
|
||||||
* ```typescript
|
* ```typescript
|
||||||
* import { extname, resolveResource } from '@tauri-apps/api/path';
|
* import { extname } from '@tauri-apps/api/path';
|
||||||
* const resourcePath = await resolveResource('app.conf');
|
* const ext = await extname('/path/to/file.html');
|
||||||
* const ext = await extname(resourcePath);
|
* assert(ext === 'html');
|
||||||
* assert(ext === 'conf');
|
|
||||||
* ```
|
* ```
|
||||||
*
|
*
|
||||||
* @since 1.0.0
|
* @since 1.0.0
|
||||||
@ -640,12 +639,10 @@ async function extname(path: string): Promise<string> {
|
|||||||
* Returns the last portion of a `path`. Trailing directory separators are ignored.
|
* Returns the last portion of a `path`. Trailing directory separators are ignored.
|
||||||
* @example
|
* @example
|
||||||
* ```typescript
|
* ```typescript
|
||||||
* import { basename, resolveResource } from '@tauri-apps/api/path';
|
* import { basename } from '@tauri-apps/api/path';
|
||||||
* const resourcePath = await resolveResource('app.conf');
|
* const base = await basename('path/to/app.conf');
|
||||||
* const base = await basename(resourcePath);
|
|
||||||
* assert(base === 'app.conf');
|
* assert(base === 'app.conf');
|
||||||
* ```
|
* ```
|
||||||
*
|
|
||||||
* @param ext An optional file extension to be removed from the returned path.
|
* @param ext An optional file extension to be removed from the returned path.
|
||||||
*
|
*
|
||||||
* @since 1.0.0
|
* @since 1.0.0
|
||||||
|
Loading…
Reference in New Issue
Block a user