Aspire v0.1.0 Aspire View Source
Aspire functions are trivial. Each function performs type conversion if it is 100% safe. Else it returns first argument as is.
Link to this section Summary
Functions
Safe conversion to atom type
Safe conversion to boolean type
Safe conversion to float type
Safe conversion to integer type
Safe conversion to list type
Safe conversion to map type
Safe conversion to number type
Safe conversion to string (binary) type. Second argument is %Aspire{} structure with options
Link to this section Functions
Safe conversion to atom type.
Examples
iex> Aspire.to_atom("erlang")
:erlang
iex> Aspire.to_atom("hello_world")
"hello_world"
iex> Aspire.to_atom('erlang')
:erlang
iex> Aspire.to_atom('hello_world')
'hello_world'
iex> Aspire.to_atom(123.123)
123.123
iex> Aspire.to_atom(%{hello: "world"})
%{hello: "world"}
Safe conversion to boolean type.
Examples
iex> Aspire.to_boolean(1)
true
iex> Aspire.to_boolean(0)
false
iex> Aspire.to_boolean(nil)
false
iex> Aspire.to_boolean(:undefined)
false
iex> Aspire.to_boolean("true")
true
iex> Aspire.to_boolean("false")
false
iex> Aspire.to_boolean(1.0)
1.0
iex> Aspire.to_boolean(2)
2
iex> Aspire.to_boolean("hello")
"hello"
Safe conversion to float type.
Examples
iex> Aspire.to_float(0)
0.0
iex> Aspire.to_float(123)
123.0
iex> Aspire.to_float(-123)
-123.0
iex> Aspire.to_float("0")
0.0
iex> Aspire.to_float("123")
123.0
iex> Aspire.to_float("-123")
-123.0
iex> Aspire.to_float("123.123")
123.123
iex> Aspire.to_float("-123.12300")
-123.123
iex> Aspire.to_float("hello")
"hello"
iex> Aspire.to_float("123.")
"123."
iex> Aspire.to_float(".123")
".123"
iex> Aspire.to_float("123.1ww23")
"123.1ww23"
iex> Aspire.to_float(:"0")
0.0
iex> Aspire.to_float(:"123")
123.0
iex> Aspire.to_float(:"-123")
-123.0
iex> Aspire.to_float(:"123.123")
123.123
iex> Aspire.to_float(:"-123.12300")
-123.123
iex> Aspire.to_float(:hello)
:hello
iex> Aspire.to_float(:"123.")
:"123."
iex> Aspire.to_float(:".123")
:".123"
iex> Aspire.to_float(:"123.1ww23")
:"123.1ww23"
iex> Aspire.to_float(nil)
nil
iex> Aspire.to_float('0')
0.0
iex> Aspire.to_float('123')
123.0
iex> Aspire.to_float('-123')
-123.0
iex> Aspire.to_float('123.123')
123.123
iex> Aspire.to_float('-123.12300')
-123.123
iex> Aspire.to_float('hello')
'hello'
iex> Aspire.to_float('123.')
'123.'
iex> Aspire.to_float('.123')
'.123'
iex> Aspire.to_float('123.1ww23')
'123.1ww23'
iex> Aspire.to_float([])
[]
iex> Aspire.to_float([12345, 12345])
[12345, 12345]
iex> Aspire.to_float(%{hello: "world"})
%{hello: "world"}
Safe conversion to integer type.
Examples
iex> Aspire.to_integer("0")
0
iex> Aspire.to_integer("123")
123
iex> Aspire.to_integer("-123")
-123
iex> Aspire.to_integer("0.0")
0
iex> Aspire.to_integer("123.0")
123
iex> Aspire.to_integer("-123.0")
-123
iex> Aspire.to_integer("123.1")
"123.1"
iex> Aspire.to_integer("hello")
"hello"
iex> Aspire.to_integer(0.0)
0
iex> Aspire.to_integer(123.0)
123
iex> Aspire.to_integer(-123.0)
-123
iex> Aspire.to_integer(123.1)
123.1
iex> Aspire.to_integer(:"0")
0
iex> Aspire.to_integer(:"123")
123
iex> Aspire.to_integer(:"-123")
-123
iex> Aspire.to_integer(:"0.0")
0
iex> Aspire.to_integer(:"123.0")
123
iex> Aspire.to_integer(:"-123.0")
-123
iex> Aspire.to_integer(:"123.1")
:"123.1"
iex> Aspire.to_integer(:hello)
:hello
iex> Aspire.to_integer(nil)
nil
iex> Aspire.to_integer('0')
0
iex> Aspire.to_integer('123')
123
iex> Aspire.to_integer('-123')
-123
iex> Aspire.to_integer('0.0')
0
iex> Aspire.to_integer('123.0')
123
iex> Aspire.to_integer('-123.0')
-123
iex> Aspire.to_integer('123.1')
'123.1'
iex> Aspire.to_integer('hello')
'hello'
iex> Aspire.to_integer([])
[]
iex> Aspire.to_integer([12345, 12345, 12345])
[12345, 12345, 12345]
iex> Aspire.to_integer(%{hello: "world"})
%{hello: "world"}
Safe conversion to list type.
Examples
iex> Aspire.to_list(%Aspire{decimals: 5}) |> Enum.sort
[compact: true, decimals: 5]
iex> Aspire.to_list(%{foo: 123, bar: 321}) |> Enum.sort
[bar: 321, foo: 123]
iex> Aspire.to_list("hello")
'hello'
iex> Aspire.to_list(123)
123
Safe conversion to map type.
Examples
iex> Aspire.to_map(%Aspire{decimals: 5})
%{decimals: 5, compact: true}
iex> Aspire.to_map([foo: 123, bar: 321])
%{foo: 123, bar: 321}
iex> Aspire.to_map([{"foo", 123}, {"bar", 321}])
[{"foo", 123}, {"bar", 321}]
iex> Aspire.to_map([123, 321])
[123, 321]
iex> Aspire.to_map("hello")
"hello"
Safe conversion to number type.
Examples
iex> Aspire.to_number("0.0")
0
iex> Aspire.to_number("123.0")
123
iex> Aspire.to_number("-123.0")
-123
iex> Aspire.to_number("123.123")
123.123
iex> Aspire.to_number("-123.123")
-123.123
iex> Aspire.to_number("hello")
"hello"
Safe conversion to string (binary) type. Second argument is %Aspire{} structure with options
- decimals: non negative integer, required if first argument is float
- compact: boolean, optional, default true
Examples
iex> Aspire.to_string(:hello)
"hello"
iex> Aspire.to_string(MyApp.Mymodule)
"Elixir.MyApp.Mymodule"
iex> Aspire.to_string(nil)
"nil"
iex> Aspire.to_string(:undefined)
"undefined"
iex> Aspire.to_string(false)
"false"
iex> Aspire.to_string(0)
"0"
iex> Aspire.to_string(123)
"123"
iex> Aspire.to_string(-123)
"-123"
iex> Aspire.to_string(0.00000, %Aspire{decimals: 8})
"0.0"
iex> Aspire.to_string(1.230000, %Aspire{decimals: 8})
"1.23"
iex> Aspire.to_string(1.230000, %Aspire{decimals: 8, compact: false})
"1.23000000"
iex> Aspire.to_string(1.23e5, %Aspire{decimals: 8})
"123000.0"
iex> Aspire.to_string(1.23)
1.23
iex> Aspire.to_string('hello world')
"hello world"
iex> Aspire.to_string([])
""
iex> Aspire.to_string([12345, 12345, 12345])
[12345, 12345, 12345]
iex> Aspire.to_string([hello: "world"])
[hello: "world"]
iex> Aspire.to_string(%{hello: "world"})
%{hello: "world"}