-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathhaikunator.ex
53 lines (39 loc) · 1.4 KB
/
haikunator.ex
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
defmodule Haikunator do
@moduledoc """
# Haikunator
Generate Heroku-like memorable random names to use in your apps or anywhere else.
"""
@adjectives Application.get_env(:haikunator, :adjectives)
@nouns Application.get_env(:haikunator, :nouns)
@doc """
Generate a memorable name.
`range` is the first parameter, and sets the range for the token (i.e. the number) from 0 to n.
Defaults to `9999`.
`delimiter` is the second parameter, and it's the string that joins the sections.
Defaults to `"-"`.
## Examples
# Default
iex> Haikunator.build # => "morning-star-6817"
# Token range
iex> Haikunator.build(100) # => "summer-dawn-24"
# Don't include the token
iex> Haikunator.build(0) # => "ancient-frost"
# Use a different delimiter
iex> Haikunator.build(9999, ".") # => "frosty.leaf.8347"
# No token, no delimiter
iex> Haikunator.build(0, "") # => "twilightbreeze"
"""
@spec build(integer, String.t) :: String.t
def build(range \\ 9999, delimiter \\ "-") do
:rand.seed(:exsplus)
token = if range > 0, do: random(range)
[@adjectives, @nouns]
|> Enum.map(&sample/1)
|> Enum.concat(List.wrap(token))
|> Enum.join(delimiter)
end
@spec random(integer) :: integer
defp random(range) when range > 0, do: :rand.uniform(range)
@spec sample([any]) :: any
defp sample(array), do: Enum.random(array)
end