diff --git a/grouping.go b/grouping.go new file mode 100644 index 0000000..98399b9 --- /dev/null +++ b/grouping.go @@ -0,0 +1,23 @@ +package apg + +import "unicode" + +// GroupCharsForMobile takes a given string of characters and groups them in a mobile-friendly +// manner. The grouping is based on the following precedense: uppercase, lowercase, numbers +// and special characters. The grouped string is then returned. +func GroupCharsForMobile(chars string) string { + var uppers, lowers, numbers, others []rune + for _, char := range chars { + switch { + case unicode.IsUpper(char): + uppers = append(uppers, char) + case unicode.IsLower(char): + lowers = append(lowers, char) + case unicode.IsNumber(char): + numbers = append(numbers, char) + default: + others = append(others, char) + } + } + return string(uppers) + string(lowers) + string(numbers) + string(others) +} diff --git a/grouping_test.go b/grouping_test.go new file mode 100644 index 0000000..972cb52 --- /dev/null +++ b/grouping_test.go @@ -0,0 +1,24 @@ +package apg + +import "testing" + +func TestGroupCharacters(t *testing.T) { + tests := []struct { + name string + password string + want string + }{ + {`PW: A1c9.Ba`, `A1c9.Ba`, `ABca19.`}, + {`PW: PX4xDoiKrs,[egEAief{`, `PX4xDoiKrs,[egEAief{`, `PXDKEAxoirsegief4,[{`}, + {`PW: *Z%C9d+PZYkD7D+{~r'w`, `*Z%C9d+PZYkD7D+{~r'w`, `ZCPZYDDdkrw97*%++{~'`}, + {`PW: 4?r2YV:Abo&/z<3tJ*Z{`, `4?r2YV:Abo&/z<3tJ*Z{`, `YVAJZrbozt423?:&/<*{`}, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + grouped := GroupCharsForMobile(tt.password) + if grouped != tt.want { + t.Errorf("GroupCharsForMobile() failed, expected: %s, got: %s", tt.want, grouped) + } + }) + } +}