Faker v0.10.0 Faker.Util View Source

Collection of useful functions for your fake data. Functions aware of locale.

Link to this section Summary

Functions

Cycle randomly through the given list with guarantee every element of the list is used once before elements are being picked again. This is done by keeping a list of remaining elements that have not been picked yet. The list of remaining element is returned, as well as the randomly picked element

Start a cycle. See cycle/1

Get a random digit as a string; one of 0-9

Format a string with randomly generated data. Format specifiers are replaced by random values. A format specifier follows this prototype

Execute fun n times with the index as first param and join the results with joiner

Get a random alphabet character as a string; one of a-z or A-Z

Execute fun n times with the index as first param and return the results as a list

Get a random lowercase character as a string; one of a-z

Pick a random element from the list

Converts a list to a string, with “and” before the last item. Uses an Oxford comma

Get a random uppercase character as a string; one of A-Z

Link to this section Functions

Link to this function cycle(cycle_pid) View Source
cycle(pid()) :: any()

Cycle randomly through the given list with guarantee every element of the list is used once before elements are being picked again. This is done by keeping a list of remaining elements that have not been picked yet. The list of remaining element is returned, as well as the randomly picked element.

Example

my_cycle = cycle_start ~w(1 2 3)
cycle my_cycle #=> 2
cycle my_cycle #=> 3
cycle my_cycle #=> 1
Link to this function cycle_start(items) View Source
cycle_start([any()]) :: pid()

Start a cycle. See cycle/1

Get a random digit as a string; one of 0-9

Link to this function format(format_str, rules \\ [d: &digit/0, A: &upper_letter/0, a: &lower_letter/0, b: &letter/0]) View Source
format(binary(), Keyword.t()) :: binary()

Format a string with randomly generated data. Format specifiers are replaced by random values. A format specifier follows this prototype:

%[length]specifier

The following specifier rules are present by default:

  • d: digits 0-9
  • a: lowercase letter a-z
  • A: uppercase letter A-Z
  • b: anycase letter a-z, A-Z

The specifier rules can be overriden using the second argument.

Examples

format("%2d-%3d %a%A %2d%%") #=> "74-381 sK 32%"
format("%8nBATMAN", n: fn() -> "nana " end) #=> "nana nana nana nana nana nana nana nana BATMAN"
Link to this function join(n, joiner \\ "", fun) View Source
join(integer(), binary(), (integer() -> binary())) :: binary()

Execute fun n times with the index as first param and join the results with joiner

Examples

join(3, ", ", &Faker.Code.isbn13/0) # => "9785053574562, 9787187574302, 9798231475230"

Get a random alphabet character as a string; one of a-z or A-Z

Link to this function list(n, fun) View Source
list(integer(), (integer() -> any())) :: [any()]
list(integer(), (() -> any())) :: [any()]

Execute fun n times with the index as first param and return the results as a list

Link to this function lower_letter() View Source
lower_letter() :: binary()

Get a random lowercase character as a string; one of a-z

Pick a random element from the list

Link to this function to_sentence(items) View Source
to_sentence([binary()]) :: binary()

Converts a list to a string, with “and” before the last item. Uses an Oxford comma.

Link to this function upper_letter() View Source
upper_letter() :: binary()

Get a random uppercase character as a string; one of A-Z