Skip to content

If Atom, BitString, Integer and Float values wanna be a boolean value, they can using to_boolean/1 function.

License

Notifications You must be signed in to change notification settings

prodis/wannabe_bool_elixir

Repository files navigation

Wannabe Bool

Hex.pm Docs Build Status Coverage Status License

If Atom, String (BitString), Integer and Float values wanna be a boolean value, they can using to_boolean/1 function.

Installation

The package can be installed by adding wannabe_bool to your list of dependencies in mix.exs:

def deps do
  [
    {:wannabe_bool, "~> 0.1.3"}
  ]
end

Usage

The is_boolean/1 function is implemented for Atom, BitString, Integer and Float types.

You can use is_boolean/1 function importing WannabeBool protocol:

iex> import WannabeBool
iex>
iex> to_boolean("true")
true

Or calling it in WannabeBool protocol directly:

iex> WannabeBool.to_boolean("true")
true

Truthy values

Each type has its own "truthy values".

BitString (String)

Returns true if the given string is one of these values: "t", "true", "on", "y", "yes", "1".

Otherwise, returns false.

Trailling spaces and letter cases are ignored.

Examples

iex> to_boolean("t")
true
iex> to_boolean("T")
true
iex> to_boolean("true")
true
iex> to_boolean("TRUE")
true
iex> to_boolean("on")
true
iex> to_boolean("ON")
true
iex> to_boolean("y")
true
iex> to_boolean("yes")
true
iex> to_boolean("YES")
true
iex> to_boolean("1")
true
iex> to_boolean(" t ")
true
iex> to_boolean(" T ")
true
iex> to_boolean(" true ")
true
iex> to_boolean(" TRUE ")
true
iex> to_boolean(" on ")
true
iex> to_boolean(" ON ")
true
iex> to_boolean(" y ")
true
iex> to_boolean("Y")
true
iex> to_boolean(" Y ")
true
iex> to_boolean(" yes ")
true
iex> to_boolean(" YES ")
true
iex> to_boolean(" 1 ")
true

iex> to_boolean("false")
false
iex> to_boolean("whatever")
false
iex> to_boolean("")
false

Atom

The same as my_atom |> to_string() |> to_boolean().

true and false obvisouly returns true and false respectively. :)

nil returns false.

Examples

iex> to_boolean(:"t")
true
iex> to_boolean(:"true")
true
iex> to_boolean(:"on")
true
iex> to_boolean(:"y")
true
iex> to_boolean(:"yes")
true
iex> to_boolean(:"1")
true

iex> to_boolean(:"false")
false
iex> to_boolean(:"whatever")
false
iex> to_boolean(:"")
false

iex> to_boolean(true)
true
iex> to_boolean(false)
false
iex> to_boolean(nil)
false

Integer

Returns false if the given integer is zero. Otherwise, returns true.

Examples

iex> to_boolean(0)
false

iex> to_boolean(1)
true
iex> to_boolean(2)
true
iex> to_boolean(-1)
true
iex> to_boolean(-2)
true

Float

Returns false if the given float is zero. Otherwise, returns true.

Examples

iex> to_boolean(0.0)
false

iex> to_boolean(0.1)
true
iex> to_boolean(1.0)
true
iex> to_boolean(-0.1)
true
iex> to_boolean(-1.0)
true

Other types

For other not implemented types a Protocol.UndefinedError is raised.

Example

iex> to_boolean([])
** (Protocol.UndefinedError) protocol WannabeBool not implemented for []. This protocol is implemented for: Atom, BitString, Float, Integer

Full documentation

The full documentation is available at https://hexdocs.pm/wannabe_bool.

Contributing

See the contributing guide.

License

Wannabe Bool is released under the Apache 2.0 License. See the LICENSE file.

Copyright © 2018-2021 Fernando Hamasaki de Amorim

Author

Fernando Hamasaki de Amorim (prodis)

Prodis

About

If Atom, BitString, Integer and Float values wanna be a boolean value, they can using to_boolean/1 function.

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published