Skip to content

Commit 0eba2bd

Browse files
committed
Speed up Internet::Password generation using constants
1 parent a67007a commit 0eba2bd

File tree

2 files changed

+9
-9
lines changed

2 files changed

+9
-9
lines changed

lib/faker/default/internet.rb

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -170,23 +170,23 @@ def password(min_length: 8, max_length: 16, mix_case: true, special_characters:
170170
character_bag = []
171171

172172
# use lower_chars by default and add upper_chars if mix_case
173-
lower_chars = ('a'..'z').to_a
174-
password << lower_chars[rand(lower_chars.count - 1)]
173+
lower_chars = self::LLetters
174+
password << sample(lower_chars)
175175
character_bag += lower_chars
176176

177-
if character_types.include?(:mix_case)
178-
upper_chars = ('A'..'Z').to_a
179-
password << upper_chars[rand(upper_chars.count - 1)]
177+
if mix_case
178+
upper_chars = self::ULetters
179+
password << sample(upper_chars)
180180
character_bag += upper_chars
181181
end
182182

183-
if character_types.include?(:special_characters)
183+
if special_characters
184184
special_chars = %w[! @ # $ % ^ & *]
185-
password << special_chars[rand(special_chars.count - 1)]
185+
password << sample(special_chars)
186186
character_bag += special_chars
187187
end
188188

189-
password << character_bag[rand(character_bag.count - 1)] while password.length < target_length
189+
password << sample(character_bag) while password.length < target_length
190190

191191
shuffle(password).join
192192
end

test/faker/default/test_faker_internet.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ def test_username_with_range_and_separators
117117
end
118118

119119
def test_password
120-
assert_match(/\w{3}/, @tester.password)
120+
assert_match(/\w{8}/, @tester.password)
121121
end
122122

123123
def test_password_with_integer_arg

0 commit comments

Comments
 (0)