This repository has been archived by the owner on Dec 15, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix #1
- Loading branch information
Showing
9 changed files
with
531 additions
and
346 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Oops, something went wrong.