Skip to content
This repository has been archived by the owner on Dec 15, 2022. It is now read-only.

Commit

Permalink
Adds Form module
Browse files Browse the repository at this point in the history
Fix #1
  • Loading branch information
cuducos committed Apr 3, 2021
1 parent 75c562f commit d37d7b7
Show file tree
Hide file tree
Showing 9 changed files with 531 additions and 346 deletions.
68 changes: 68 additions & 0 deletions src/Form/Mask.elm
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
module Form.Mask exposing (cnpj, cpf, date, postalCode)


cpf : String -> String
cpf value =
let
cleaned : String
cleaned =
String.filter Char.isDigit value
in
String.concat
[ String.slice 0 3 cleaned
, "."
, String.slice 3 6 cleaned
, "."
, String.slice 6 9 cleaned
, "-"
, String.slice 9 11 cleaned
]


cnpj : String -> String
cnpj value =
let
cleaned : String
cleaned =
String.filter Char.isDigit value
in
String.concat
[ String.slice 0 2 cleaned
, "."
, String.slice 2 5 cleaned
, "."
, String.slice 5 8 cleaned
, "/"
, String.slice 8 12 cleaned
, "-"
, String.slice 12 14 cleaned
]


date : String -> String
date value =
let
cleaned : String
cleaned =
String.filter Char.isDigit value
in
String.join
"/"
[ String.slice 0 2 cleaned
, String.slice 2 4 cleaned
, String.slice 4 8 cleaned
]


postalCode : String -> String
postalCode value =
let
cleaned : String
cleaned =
String.filter Char.isDigit value
in
String.concat
[ String.slice 0 5 cleaned
, "-"
, String.slice 5 8 cleaned
]
46 changes: 46 additions & 0 deletions src/Form/Model.elm
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
module Form.Model exposing (Field, FieldRow, FieldSet, Width(..), newField)


type Width
= One
| Two
| Three
| Four
| Five
| Six
| Seven
| Eight
| Nine
| Ten
| Eleven
| Twelve
| Thirteen
| Fourteen
| Fifteen
| Sixteen


type alias Field =
{ value : String
, label : String
, width : Width
, validators : List (String -> Bool)
, mask : Maybe (String -> String)
, valid : Bool
, initialized : Bool
}


type alias FieldRow =
List Field


type alias FieldSet =
{ title : String
, rows : List FieldRow
}


newField : String -> Width -> List (String -> Bool) -> Maybe (String -> String) -> Field
newField label width validators mask =
Field "" label width validators mask False False
58 changes: 58 additions & 0 deletions src/Form/Update.elm
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
module Form.Update exposing (field, set)

import Form.Model exposing (Field, FieldRow, FieldSet)


validate : Field -> Field
validate f =
let
notEmpty : String -> Bool
notEmpty value =
value
|> String.trim
|> String.isEmpty
|> not

validations : List Bool
validations =
f.validators
|> (::) notEmpty
|> List.map (\v -> v f.value)
in
{ f | valid = not (List.member False validations) }


field : Field -> String -> Field
field f value =
let
mask : String -> String
mask =
Maybe.withDefault (\s -> s) f.mask

newField : Field
newField =
validate { f | initialized = True, value = value }
in
if newField.valid then
{ newField | value = mask newField.value }

else
newField


rows : String -> String -> FieldRow -> FieldRow
rows key value r =
List.map
(\f ->
if f.label == key then
field f value

else
f
)
r


set : String -> String -> FieldSet -> FieldSet
set key value s =
{ s | rows = List.map (\r -> rows key value r) s.rows }
97 changes: 97 additions & 0 deletions src/Form/Validators.elm
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
module Form.Validators exposing (cnpj, cpf, date, postalCode, state)

import Form.Model exposing (Field)
import Set exposing (Set)


states : Set String
states =
Set.fromList
[ "AC"
, "AL"
, "AP"
, "AM"
, "BA"
, "CE"
, "ES"
, "GO"
, "MA"
, "MT"
, "MS"
, "MG"
, "PA"
, "PB"
, "PR"
, "PE"
, "PI"
, "RJ"
, "RN"
, "RS"
, "RO"
, "RR"
, "SC"
, "SP"
, "SE"
, "TO"
, "DF"
]


state : String -> Bool
state value =
Set.member value states


cpf : String -> Bool
cpf value =
value
|> String.filter Char.isDigit
|> String.length
|> (==) 11


cnpj : String -> Bool
cnpj value =
value
|> String.filter Char.isDigit
|> String.length
|> (==) 14


date : String -> Bool
date value =
let
digits : String
digits =
String.filter Char.isDigit value

day : Int
day =
digits
|> String.slice 0 2
|> String.toInt
|> Maybe.withDefault 0

month : Int
month =
digits
|> String.slice 2 4
|> String.toInt
|> Maybe.withDefault 0

year : Int
year =
digits
|> String.slice 4 8
|> String.toInt
|> Maybe.withDefault 0
in
year >= 1900 && year <= 2100 && month >= 1 && month <= 12 && day >= 1 && day <= 31


postalCode : String -> Bool
postalCode value =
value
|> String.filter Char.isDigit
|> String.length
|> (==) 8
Loading

0 comments on commit d37d7b7

Please sign in to comment.