-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtransform.go
43 lines (35 loc) · 1.2 KB
/
transform.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
package passit
import (
"io"
"strings"
"golang.org/x/text/cases"
"golang.org/x/text/language"
)
type transformGenerator struct {
gen Generator
fn func(string) string
}
// Transform returns a Generator that converts the generated password according to
// the user supplied function fn.
func Transform(gen Generator, fn func(string) string) Generator {
return &transformGenerator{gen, fn}
}
func (tg *transformGenerator) Password(r io.Reader) (string, error) {
pass, err := tg.gen.Password(r)
return tg.fn(pass), err
}
// LowerCase returns a Generator that uses [strings.ToLower] to map all Unicode
// letters in the generated password to their lower case.
func LowerCase(gen Generator) Generator {
return Transform(gen, strings.ToLower)
}
// UpperCase returns a Generator that uses [strings.ToUpper] to map all Unicode
// letters in the generated password to their upper case.
func UpperCase(gen Generator) Generator {
return Transform(gen, strings.ToUpper)
}
// TitleCase returns a Generator that uses [golang.org/x/text/cases.Title] to
// convert the generated password to language-specific title case.
func TitleCase(gen Generator, t language.Tag) Generator {
return Transform(gen, cases.Title(t).String)
}