Packages

elixir porting of https://github.com/ffaker/ffaker

Current section

Files

Jump to
ffaker lib ffaker.ex
Raw

lib/ffaker.ex

defmodule Ffaker do
@moduledoc """
Common modules
"""
@doc """
make list from file
## Parameters
- file_name: data file name
- dir_path: directory path of file
## Examples
iex> Ffaker.list_file("numbers", "test")
["1", "2", "3"]
"""
@spec list_file(String.t, String.t) :: Enum.t
def list_file(file_name, dir_path) do
file_path = "#{File.cwd!}/data/#{dir_path}/#{file_name}"
case File.read(file_path) do
{:ok, res} ->
res
|> String.trim
|> String.split("\n")
{:error, _} -> raise RuntimeError, message: "#{file_name} not found"
end
end
@doc """
replace # with number
## Parameters
- string: original string
## Examples
iex> Ffaker.number("##-###")
"12-345"
"""
def numerify(string) do
string
|> String.graphemes
|> Enum.map(fn x ->
case x do
"#" -> 0..9 |> Enum.random |> to_string
x -> x
end
end)
|> Enum.join
end
end