forked from keybase/client
-
Notifications
You must be signed in to change notification settings - Fork 0
/
username.go
46 lines (35 loc) · 1.48 KB
/
username.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
// Copyright 2018 Keybase, Inc. All rights reserved. Use of
// this source code is governed by the included BSD license.
package kbun
import (
"regexp"
"strings"
)
// UsernameRE is the regex for matching usernames. Underscores are allowed,
// just not first or doubled.
const UsernameRE = `(?:[a-zA-Z0-9]+_?)+`
var usernameRE = regexp.MustCompile("^" + UsernameRE + "$")
// CheckUsername returns true if the given string can be a Keybase
// username.
func CheckUsername(s string) bool {
return len(s) >= 2 && len(s) <= 16 && usernameRE.MatchString(s)
}
// NormalizedUsername is a Keybase username that has been normalized
// (toLowered) and therefore will compare correctly against other
// normalized usernames.
type NormalizedUsername string
// NewNormalizedUsername makes a normalized username out of a non-normalized
// plain string username
func NewNormalizedUsername(s string) NormalizedUsername {
return NormalizedUsername(strings.ToLower(s))
}
// Eq returns true if the given normalized usernames are equal
func (n NormalizedUsername) Eq(n2 NormalizedUsername) bool {
return string(n) == string(n2)
}
// String returns the normalized username as a string (in lower case)
func (n NormalizedUsername) String() string { return string(n) }
// IsNil returns true if the username is the empty string
func (n NormalizedUsername) IsNil() bool { return len(string(n)) == 0 }
// IsValid returns CheckUsername(n.String()).
func (n NormalizedUsername) IsValid() bool { return CheckUsername(string(n)) }