Skip to content

Commit

Permalink
Speed up Internet::Password generation using constants
Browse files Browse the repository at this point in the history
  • Loading branch information
MicBruz committed Mar 2, 2023
1 parent a67007a commit 0eba2bd
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 9 deletions.
16 changes: 8 additions & 8 deletions lib/faker/default/internet.rb
Original file line number Diff line number Diff line change
Expand Up @@ -170,23 +170,23 @@ def password(min_length: 8, max_length: 16, mix_case: true, special_characters:
character_bag = []

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

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

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

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

shuffle(password).join
end
Expand Down
2 changes: 1 addition & 1 deletion test/faker/default/test_faker_internet.rb
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ def test_username_with_range_and_separators
end

def test_password
assert_match(/\w{3}/, @tester.password)
assert_match(/\w{8}/, @tester.password)
end

def test_password_with_integer_arg
Expand Down

0 comments on commit 0eba2bd

Please sign in to comment.