Created
Some checks failed
Go / build (push) Failing after 7s

This commit is contained in:
scheibling
2025-04-08 19:16:39 +02:00
commit b4eb50ab55
63 changed files with 7333 additions and 0 deletions

18
pathx/path.go Normal file
View 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
View 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)
}
}