Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ Change log itself follows [Keep a CHANGELOG](http://keepachangelog.com) format.
- `Faker.Gov.It` [[@neslinesli93](https://github.com/neslinesli93)]
- `Faker.Vehicle.model/1` [[@daytonn](https://github.com/daytonn)]
- `Faker.Person`[[@anthonator](https://github.com/anthonator)]
- `Faker.String.hexadecimal/0` and `Faker.String.hexadecimal/1`[[@ilyashuma](https://github.com/ilyashuma)]

### Changed
- Fix `Faker.Code.Iban.iban` and `Faker.Gov.It.fiscal_id` doctests [[@vbrazo](https://github.com/vbrazo)]
Expand Down
20 changes: 20 additions & 0 deletions lib/faker/string.ex
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,24 @@ defmodule Faker.String do
|> Base.encode64()
|> binary_part(0, length)
end

@doc """
Returns a random hexadecimal String
## Examples
iex> Faker.String.hexadecimal()

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Even though it returns a String, hexadecimal is a number according to its definition.

Suggested change
iex> Faker.String.hexadecimal()
iex> Faker.Number.hexadecimal()

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"d6d98b88c866f496dbd4de7ba48d0f52"
iex> Faker.String.hexadecimal()
"c9edd02cc9f381def9d922a146bf8550"
iex> Faker.String.hexadecimal(5)
"9b5c2"
iex> Faker.String.hexadecimal(100)
"9a3a4993ad56b778dc26eddb7e0c2e49c4e638e62de32933bc3525bb4594a1a378dc29f741dd703efd94dd3b6d08feaa53a9"
"""
@spec hexadecimal(pos_integer) :: String.t()
def hexadecimal(length \\ 32) do
length
|> Faker.random_bytes()
|> Base.encode16(case: :lower)
|> binary_part(0, length)
end
end