general.go

 1package fake
 2
 3var lowerLetters = []rune("abcdefghijklmnopqrstuvwxyz")
 4var upperLetters = []rune("ABCDEFGHIJKLMNOPQRSTUVWXYZ")
 5var numeric = []rune("0123456789")
 6var specialChars = []rune(`!'@#$%^&*()_+-=[]{};:",./?`)
 7var hexDigits = []rune("0123456789abcdef")
 8
 9func text(atLeast, atMost int, allowLower, allowUpper, allowNumeric, allowSpecial bool) string {
10	allowedChars := []rune{}
11	if allowLower {
12		allowedChars = append(allowedChars, lowerLetters...)
13	}
14	if allowUpper {
15		allowedChars = append(allowedChars, upperLetters...)
16	}
17	if allowNumeric {
18		allowedChars = append(allowedChars, numeric...)
19	}
20	if allowSpecial {
21		allowedChars = append(allowedChars, specialChars...)
22	}
23
24	result := []rune{}
25	nTimes := r.Intn(atMost-atLeast+1) + atLeast
26	for i := 0; i < nTimes; i++ {
27		result = append(result, allowedChars[r.Intn(len(allowedChars))])
28	}
29	return string(result)
30}
31
32// Password generates password with the length from atLeast to atMOst charachers,
33// allow* parameters specify whether corresponding symbols can be used
34func Password(atLeast, atMost int, allowUpper, allowNumeric, allowSpecial bool) string {
35	return text(atLeast, atMost, true, allowUpper, allowNumeric, allowSpecial)
36}
37
38// SimplePassword is a wrapper around Password,
39// it generates password with the length from 6 to 12 symbols, with upper characters and numeric symbols allowed
40func SimplePassword() string {
41	return Password(6, 12, true, true, false)
42}
43
44// Color generates color name
45func Color() string {
46	return lookup(lang, "colors", true)
47}
48
49// DigitsN returns n digits as a string
50func DigitsN(n int) string {
51	digits := make([]rune, n)
52	for i := 0; i < n; i++ {
53		digits[i] = numeric[r.Intn(len(numeric))]
54	}
55	return string(digits)
56}
57
58// Digits returns from 1 to 5 digits as a string
59func Digits() string {
60	return DigitsN(r.Intn(5) + 1)
61}
62
63func hexDigitsStr(n int) string {
64	var num []rune
65	for i := 0; i < n; i++ {
66		num = append(num, hexDigits[r.Intn(len(hexDigits))])
67	}
68	return string(num)
69}
70
71// HexColor generates hex color name
72func HexColor() string {
73	return hexDigitsStr(6)
74}
75
76// HexColorShort generates short hex color name
77func HexColorShort() string {
78	return hexDigitsStr(3)
79}