From f6c7df9fe5fbc0dfe650698d9d5c30113f3f0407 Mon Sep 17 00:00:00 2001 From: Ilya Shumeyko Date: Tue, 12 May 2020 22:55:38 +0300 Subject: [PATCH] Add Faker.String.hexadecimal/1 --- CHANGELOG.md | 1 + lib/faker/string.ex | 20 ++++++++++++++++++++ 2 files changed, 21 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 39a583528..f8e6694af 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -22,6 +22,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)] diff --git a/lib/faker/string.ex b/lib/faker/string.ex index 0c861e09a..f077146dc 100644 --- a/lib/faker/string.ex +++ b/lib/faker/string.ex @@ -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() + "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