-
Notifications
You must be signed in to change notification settings - Fork 0
/
teamvite.go
66 lines (56 loc) · 1.35 KB
/
teamvite.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package teamvite
import (
"context"
"crypto/md5"
"database/sql"
"encoding/hex"
"errors"
"fmt"
"log"
"regexp"
"strconv"
_ "github.com/mattn/go-sqlite3"
)
// Build version & commit SHA.
var (
Version string
Commit string
)
// ReportError notifies an external service of errors. No-op by default.
var ReportError = func(ctx context.Context, err error, args ...interface{}) {}
// ReportPanic notifies an external service of panics. No-op by default.
var ReportPanic = func(err interface{}) {}
type Item interface {
ItemID() uint64
ItemType() string
}
func GravatarKey(email string) string {
sum := md5.Sum([]byte(email))
return hex.EncodeToString(sum[:])
}
func checkErr(err error, msg string) {
if err != nil && !errors.Is(err, sql.ErrNoRows) {
log.Println("[ERROR]: ", msg, err)
}
}
// format an int as a telephone number
func Telify(num int) string {
str := fmt.Sprintf("%d", num)
if len(str) != 10 {
return ""
}
return fmt.Sprintf("%s-%s-%s", str[0:3], str[3:6], str[6:])
}
// convert a string to an integer telephone number. Valid phone numbers are 10
// characters, no country codes.
//
// returns -1 if the string is not a valid phone number
func UnTelify(str string) int {
reg, _ := regexp.Compile("[^0-9]+")
strTel := reg.ReplaceAllString(str, "")
if len(strTel) == 10 {
tel, _ := strconv.Atoi(strTel)
return tel
}
return -1
}