(Feat): Initial Commit
This commit is contained in:
40
internal/util/path.go
Normal file
40
internal/util/path.go
Normal file
@@ -0,0 +1,40 @@
|
||||
// nolint:revive
|
||||
package util
|
||||
|
||||
import (
|
||||
"path/filepath"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// FileNameWithoutExtension returns the name of the file referenced by the
|
||||
// provided path without the file's extension.
|
||||
// The function accepts a full (local) file path as well, only the latest
|
||||
// element of the path will be considered as a name.
|
||||
// If the provided path is empty or consists entirely of separators, an
|
||||
// empty string will be returned.
|
||||
func FileNameWithoutExtension(path string) string {
|
||||
base := filepath.Base(path)
|
||||
if base == "." || base == "/" {
|
||||
return ""
|
||||
}
|
||||
|
||||
return strings.TrimSuffix(base, filepath.Ext(path))
|
||||
}
|
||||
|
||||
// FileNameAndExtension returns the name of the file referenced by the
|
||||
// provided path as well as its extension as separated strings.
|
||||
// The function accepts a full (local) file path as well, only the latest
|
||||
// element of the path will be considered as a name.
|
||||
// If the provided path is empty or consists entirely of separators,
|
||||
// empty strings will be returned.
|
||||
func FileNameAndExtension(path string) (string, string) {
|
||||
base := filepath.Base(path)
|
||||
if base == "." || base == "/" {
|
||||
return "", ""
|
||||
}
|
||||
|
||||
extension := filepath.Ext(path)
|
||||
fileName := strings.TrimSuffix(base, extension)
|
||||
|
||||
return fileName, extension
|
||||
}
|
||||
Reference in New Issue
Block a user