18
pathx/path.go
Normal file
18
pathx/path.go
Normal file
@@ -0,0 +1,18 @@
|
||||
package pathx
|
||||
|
||||
import (
|
||||
"path"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func FilenameWithoutExt(s string) string {
|
||||
if s == "" {
|
||||
return ""
|
||||
}
|
||||
|
||||
filename := path.Base(s)
|
||||
if ext := path.Ext(s); ext != "" {
|
||||
filename = strings.TrimSuffix(filename, ext)
|
||||
}
|
||||
return filename
|
||||
}
|
||||
25
pathx/path_test.go
Normal file
25
pathx/path_test.go
Normal file
@@ -0,0 +1,25 @@
|
||||
package pathx
|
||||
|
||||
import (
|
||||
"github.com/stretchr/testify/assert"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestFilenameWithoutExt(t *testing.T) {
|
||||
testCases := []struct {
|
||||
tag string
|
||||
path string
|
||||
expected string
|
||||
}{
|
||||
{"t1", "a.jpg", "a"},
|
||||
{"t2", "/a/b/c.jpg", "c"},
|
||||
{"t3", "/a/b/c", "c"},
|
||||
{"t4", "/a/b/c/", "c"},
|
||||
{"t5", "/a/b/c/中文.jpg", "中文"},
|
||||
{"t5", "https://www.example.com/a/b/c/中文.jpg", "中文"},
|
||||
}
|
||||
for _, testCase := range testCases {
|
||||
v := FilenameWithoutExt(testCase.path)
|
||||
assert.Equal(t, testCase.expected, v, testCase.tag)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user