https://github.com/prodis/wannabe_bool_elixir
If Atom, BitString, Integer and Float values wanna be a boolean value, they can using to_boolean/1 function.
https://github.com/prodis/wannabe_bool_elixir
boolean conversion elixir elixir-lang prodis wannabe-bool
Last synced: 7 months ago
JSON representation
If Atom, BitString, Integer and Float values wanna be a boolean value, they can using to_boolean/1 function.
- Host: GitHub
- URL: https://github.com/prodis/wannabe_bool_elixir
- Owner: prodis
- License: apache-2.0
- Created: 2018-12-09T10:37:45.000Z (about 7 years ago)
- Default Branch: master
- Last Pushed: 2021-11-04T11:37:29.000Z (about 4 years ago)
- Last Synced: 2025-06-17T11:50:07.362Z (7 months ago)
- Topics: boolean, conversion, elixir, elixir-lang, prodis, wannabe-bool
- Language: Elixir
- Homepage:
- Size: 36.1 KB
- Stars: 6
- Watchers: 1
- Forks: 0
- Open Issues: 3
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- Contributing: CONTRIBUTING.md
- License: LICENSE
Awesome Lists containing this project
README
# Wannabe Bool
[](https://hex.pm/packages/wannabe_bool)
[](https://hexdocs.pm/wannabe_bool)
[](https://travis-ci.org/prodis/wannabe_bool_elixir)
[](https://coveralls.io/github/prodis/wannabe_bool_elixir?branch=master)
[](https://github.com/prodis/wannabe_bool_elixir/blob/master/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`:
```elixir
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:
```elixir
iex> import WannabeBool
iex>
iex> to_boolean("true")
true
```
Or calling it in `WannabeBool` protocol directly:
```elixir
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
```elixir
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
```elixir
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
```elixir
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
```elixir
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
```elixir
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](https://hexdocs.pm/wannabe_bool).
## Contributing
See the [contributing guide](https://github.com/prodis/wannabe_bool_elixir/blob/master/CONTRIBUTING.md).
## License
Wannabe Bool is released under the Apache 2.0 License. See the [LICENSE](https://github.com/prodis/wannabe_bool_elixir/blob/master/LICENSE) file.
Copyright © 2018-2021 Fernando Hamasaki de Amorim
## Author
[Fernando Hamasaki de Amorim (prodis)](https://github.com/prodis)
