Skip to content

Commit

Permalink
- clean up code
Browse files Browse the repository at this point in the history
  • Loading branch information
KinNeko-De committed Feb 10, 2024
1 parent 7703c0e commit df6bd97
Showing 1 changed file with 12 additions and 11 deletions.
23 changes: 12 additions & 11 deletions internal/app/humanfriendly/id.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,21 @@ func ParseHumanFriendlyId(userInput string) (HumanFriendlyId, error) {

func NewHumanFriendlyId(length int) (HumanFriendlyId, error) {
idChars := make([]byte, length)
displayLength := length + (length-1)/4
displayIdChars := make([]byte, displayLength)
_, err := rand.Read(idChars)
if err != nil {
return HumanFriendlyId{}, err
}
displayIdChars := createDisplayIdChars(length, idChars)

return HumanFriendlyId{
Id: string(idChars),
DisplayId: string(displayIdChars),
}, nil
}

func createDisplayIdChars(length int, idChars []byte) []byte {
displayLength := length + (length-1)/4
displayIdChars := make([]byte, displayLength)
d := 0
for k, v := range idChars {
if k != 0 && k%4 == 0 {
Expand All @@ -41,13 +50,5 @@ func NewHumanFriendlyId(length int) (HumanFriendlyId, error) {
displayIdChars[d] = idChars[k]
d++
}

return HumanFriendlyId{
Id: string(idChars),
DisplayId: string(displayIdChars),
}, nil
}

func ToDisplayId(s string) {

return displayIdChars
}

0 comments on commit df6bd97

Please sign in to comment.