refactor: extract helper modules to remove code duplication
This commit is contained in:
parent
36776f8e28
commit
9af7381843
3 changed files with 163 additions and 0 deletions
49
lib/mv/helpers/type_parsers.ex
Normal file
49
lib/mv/helpers/type_parsers.ex
Normal file
|
|
@ -0,0 +1,49 @@
|
|||
defmodule Mv.Helpers.TypeParsers do
|
||||
@moduledoc """
|
||||
Helper functions for parsing various input types to common Elixir types.
|
||||
|
||||
Provides safe parsing functions for common type conversions, especially useful
|
||||
when dealing with form data or external APIs.
|
||||
"""
|
||||
|
||||
@doc """
|
||||
Parses various input types to boolean.
|
||||
|
||||
Handles: booleans, strings ("true"/"false"), integers (1/0), and other values (defaults to false).
|
||||
|
||||
## Parameters
|
||||
|
||||
- `value` - The value to parse (boolean, string, integer, or other)
|
||||
|
||||
## Returns
|
||||
|
||||
A boolean value
|
||||
|
||||
## Examples
|
||||
|
||||
iex> parse_boolean(true)
|
||||
true
|
||||
|
||||
iex> parse_boolean("true")
|
||||
true
|
||||
|
||||
iex> parse_boolean("false")
|
||||
false
|
||||
|
||||
iex> parse_boolean(1)
|
||||
true
|
||||
|
||||
iex> parse_boolean(0)
|
||||
false
|
||||
|
||||
iex> parse_boolean(nil)
|
||||
false
|
||||
"""
|
||||
@spec parse_boolean(any()) :: boolean()
|
||||
def parse_boolean(value) when is_boolean(value), do: value
|
||||
def parse_boolean("true"), do: true
|
||||
def parse_boolean("false"), do: false
|
||||
def parse_boolean(1), do: true
|
||||
def parse_boolean(0), do: false
|
||||
def parse_boolean(_), do: false
|
||||
end
|
||||
Loading…
Add table
Add a link
Reference in a new issue