forked from tangled.org/core
Monorepo for Tangled — https://tangled.org
1package userutil 2 3import ( 4 "regexp" 5 "strings" 6) 7 8var ( 9 handleRegex = regexp.MustCompile(`^([a-zA-Z0-9]([a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?\.)+[a-zA-Z]([a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?$`) 10 didRegex = regexp.MustCompile(`^did:[a-z]+:[a-zA-Z0-9._:%-]*[a-zA-Z0-9._-]$`) 11) 12 13func IsHandleNoAt(s string) bool { 14 // ref: https://atproto.com/specs/handle 15 return handleRegex.MatchString(s) 16} 17 18func UnflattenDid(s string) string { 19 if !IsFlattenedDid(s) { 20 return s 21 } 22 23 return strings.Replace(s, "-", ":", 2) 24} 25 26// IsFlattenedDid checks if the given string is a flattened DID. 27func IsFlattenedDid(s string) bool { 28 // Check if the string starts with "did-" 29 if !strings.HasPrefix(s, "did-") { 30 return false 31 } 32 33 // Reconstruct as a standard DID format using Replace 34 // Example: "did-plc-xyz-abc" becomes "did:plc:xyz-abc" 35 reconstructed := strings.Replace(s, "-", ":", 2) 36 37 return didRegex.MatchString(reconstructed) 38} 39 40// FlattenDid converts a DID to a flattened format. 41// A flattened DID is a DID with the :s swapped to -s to satisfy certain 42// application requirements, such as Go module naming conventions. 43func FlattenDid(s string) string { 44 if IsDid(s) { 45 return strings.Replace(s, ":", "-", 2) 46 } 47 return s 48} 49 50// IsDid checks if the given string is a standard DID. 51func IsDid(s string) bool { 52 return didRegex.MatchString(s) 53} 54 55var subdomainRegex = regexp.MustCompile(`^[a-z0-9]([a-z0-9-]{2,61}[a-z0-9])?$`) 56 57func IsValidSubdomain(name string) bool { 58 return len(name) >= 4 && len(name) <= 63 && subdomainRegex.MatchString(name) 59}