mirror of
https://github.com/photoprism/photoprism.git
synced 2025-12-12 00:34:13 +01:00
Cluster: Add AppName, AppVersion and Theme request/response fields #98
Signed-off-by: Michael Mayer <michael@photoprism.app>
This commit is contained in:
@@ -16,6 +16,50 @@ func Type(s string) string {
|
||||
return clip.Chars(ASCII(s), LengthType)
|
||||
}
|
||||
|
||||
// TypeUnicode removes unsafe runes, collapses whitespace, and enforces the
|
||||
// maximum type length while preserving non-ASCII characters when possible.
|
||||
func TypeUnicode(s string) string {
|
||||
if s == "" {
|
||||
return s
|
||||
}
|
||||
|
||||
buf := make([]rune, 0, len([]rune(s)))
|
||||
lastWasSpace := false
|
||||
|
||||
for _, r := range s {
|
||||
if len(buf) >= LengthType {
|
||||
break
|
||||
}
|
||||
|
||||
if unicode.IsSpace(r) {
|
||||
if len(buf) == 0 || lastWasSpace {
|
||||
continue
|
||||
}
|
||||
buf = append(buf, ' ')
|
||||
lastWasSpace = true
|
||||
continue
|
||||
}
|
||||
|
||||
if r <= 31 {
|
||||
continue
|
||||
}
|
||||
|
||||
switch r {
|
||||
case '`', '\\', '|', '"', '\'', '?', '*', '<', '>', '{', '}':
|
||||
continue
|
||||
}
|
||||
|
||||
buf = append(buf, r)
|
||||
lastWasSpace = false
|
||||
}
|
||||
|
||||
for len(buf) > 0 && unicode.IsSpace(buf[len(buf)-1]) {
|
||||
buf = buf[:len(buf)-1]
|
||||
}
|
||||
|
||||
return string(buf)
|
||||
}
|
||||
|
||||
// TypeUnderscore replaces whitespace, dividers, quotes, brackets, and other special characters with an underscore.
|
||||
func TypeUnderscore(s string) string {
|
||||
if s == "" {
|
||||
|
||||
@@ -53,6 +53,52 @@ func TestType(t *testing.T) {
|
||||
})
|
||||
}
|
||||
|
||||
func TestTypeUnicode(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
input string
|
||||
want string
|
||||
maxRunes64 bool
|
||||
}{
|
||||
{
|
||||
name: "Clip",
|
||||
input: " 幸福 Hanzi are logograms developed for the writing of Chinese! Expressions in an index may not ...!",
|
||||
want: "幸福 Hanzi are logograms developed for the writing of Chinese! Exp",
|
||||
maxRunes64: true,
|
||||
},
|
||||
{
|
||||
name: "WhitespaceCollapsed",
|
||||
input: "a b\tc\nd",
|
||||
want: "a b c d",
|
||||
},
|
||||
{
|
||||
name: "SpecialCharacters",
|
||||
input: "a-`~/\\:|\"'?*<>{}b",
|
||||
want: "a-~/:b",
|
||||
},
|
||||
{
|
||||
name: "NonASCII",
|
||||
input: "äöü",
|
||||
want: "äöü",
|
||||
},
|
||||
{
|
||||
name: "Empty",
|
||||
input: "",
|
||||
want: "",
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
got := TypeUnicode(tt.input)
|
||||
assert.Equal(t, tt.want, got)
|
||||
if tt.maxRunes64 {
|
||||
assert.LessOrEqual(t, len([]rune(got)), LengthType)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestTypeLower(t *testing.T) {
|
||||
t.Run("Clip", func(t *testing.T) {
|
||||
result := TypeLower(" 幸福 Hanzi are logograms developed for the writing of Chinese! Expressions in an index may not ...!")
|
||||
|
||||
@@ -45,4 +45,5 @@ const (
|
||||
AssetsJsonFile = "assets.json"
|
||||
ManifestJsonFile = "manifest.json"
|
||||
SwJsFile = "sw.js"
|
||||
VersionTxtFile = "version.txt"
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user