Move email hide to utils

This commit is contained in:
Melon 2024-11-28 00:11:38 +00:00
parent a0b3570aab
commit 60890d766c
Signed by: melon
GPG Key ID: 6C9D970C50D26A25
4 changed files with 24 additions and 22 deletions

View File

@ -36,7 +36,7 @@ func LoadPages(wd string) error {
o = overlapfs.OverlapFS{A: wwwPages, B: wdFs}
}
wwwTemplates, err = template.New("pages").Funcs(template.FuncMap{
"emailHide": EmailHide,
"emailHide": utils.EmailHide,
}).ParseFS(o, "*.go.html")
glob, err := fs.Glob(o, "assets/*")
@ -67,13 +67,3 @@ func RenderCss(name string) io.ReadSeeker {
}
return bytes.NewReader(b)
}
func EmailHide(a string) string {
b := []byte(a)
for i := range b {
if b[i] != '@' && b[i] != '.' {
b[i] = 'x'
}
}
return string(b)
}

View File

@ -1,11 +0,0 @@
package pages
import (
"github.com/stretchr/testify/assert"
"testing"
)
func TestEmailHide(t *testing.T) {
assert.Equal(t, "xx", EmailHide("hi"))
assert.Equal(t, "xxxxxxx@xxxxxxx.xxx", EmailHide("example@example.com"))
}

12
utils/email-hide.go Normal file
View File

@ -0,0 +1,12 @@
package utils
import "strings"
func EmailHide(email string) string {
return strings.Map(func(r rune) rune {
if r != '@' && r != '.' {
return 'x'
}
return r
}, email)
}

11
utils/email-hide_test.go Normal file
View File

@ -0,0 +1,11 @@
package utils
import (
"github.com/stretchr/testify/require"
"testing"
)
func TestEmailHide(t *testing.T) {
require.Equal(t, "xx", EmailHide("hi"))
require.Equal(t, "xxxxxxx@xxxxxxx.xxx", EmailHide("example@example.com"))
}