-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrandom_string_generator.ex
95 lines (71 loc) · 2.92 KB
/
random_string_generator.ex
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
defmodule RandomStringGenerator do
@moduledoc """
A Module to generate a random string based on a given string pattern
Accepted string patterns:
Use l for lower case letter from a to z
Use L for upper case letter from A to Z
Use d for digit from 0 to 9
Use p for punctuation
Punctuation is any character on the following group:
@punctuation [
"!", "\"", "#", "$", "%", "&", "'", "(", ")", "*", "+", ",", "-",
".", "/", ":", ";", "<", "=", ">", "?", "@", "[", "\\", "]", "^",
"_", "`", "{", "|","}", "~"
]
Generate a string containing 2 lower case letters followed by 2 digits.
RandomStringGenerator.generate("lldd")
Generate a string containing 2 upper case letters.
RandomStringGenerator.generate("LL")
**Delimiters**
Everything that is not `l`,`L`,`d` and `p` is treated as a delimiter so the
pattern `-dl?` is interpreted as a hyphen followed by a digit followed by
a letter followed by a question mark.
Generate a string containing 2 letters followed by a hyphen.
RandomStringGenerator.generate("ll-")
**Scape**
In order to generate a string containing the characters `l`,`L`,`d` and `p`
as a delimiter you need to use the backslash twice in order to scape it.
Generate a string containing 2 digits followed by the letters `lLdp`.
RandomStringGenerator.generate("dd\\\\l\\\\L\\\\d\\\\p")
"""
@lower_alpha_chars ~w(a b c d e f g h i j k l m n o p q r s t u v w x y z)
@upper_alpha_chars ~w(A B C D E F G H I J K L M N O P Q R S T U V W X Y Z)
@digits ~w(0 1 2 3 4 5 6 7 8 9)
@punctuation [
"!", "\"", "#", "$", "%", "&", "'", "(", ")", "*", "+", ",", "-", ".", "/",
":", ";", "<", "=", ">", "?", "@", "[", "\\", "]", "^", "_", "`", "{", "|",
"}", "~"
]
@doc """
Given a `pattern` string, returns a random generated string.
## Examples
iex> str = RandomStringGenerator.generate("l-L-d")
iex> Regex.match?(~r/[a-z]-[A-Z]-[0-9]/, str)
true
"""
def generate(pattern) do
generate(pattern, "")
end
defp generate("", generated_string) do
generated_string
end
# TODO Dry Enum.random
defp generate("l" <> rest, generated_string) do
generate(rest, generated_string <> Enum.random(@lower_alpha_chars))
end
defp generate("L" <> rest, generated_string) do
generate(rest, generated_string <> Enum.random(@upper_alpha_chars))
end
defp generate("d" <> rest, generated_string) do
generate(rest, generated_string <> Enum.random(@digits))
end
defp generate("p" <> rest, generated_string) do
generate(rest, generated_string <> Enum.random(@punctuation))
end
defp generate("\\" <> <<pattern :: binary-size(1)>> <> rest, generated_string) do
generate(rest, generated_string <> pattern)
end
defp generate(<<pattern :: binary-size(1)>> <> rest, generated_string) do
generate(rest, generated_string <> pattern)
end
end