-
Notifications
You must be signed in to change notification settings - Fork 1
/
Extensions.fs
73 lines (58 loc) · 1.89 KB
/
Extensions.fs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
[<AutoOpen>]
module MinhaCarteira.Extensions
open System.Threading.Tasks
type System.Threading.Tasks.Task with
static member Sequential (funcs: seq<(unit -> Task<'a>)>) : Task<'a[]> =
task {
let result = ResizeArray()
for fn in funcs do
let! x = fn ()
result.Add x
return result.ToArray()
}
open MinhaCarteira.Models
open System.Text.RegularExpressions
let private numberAtEnd = Regex(@"\d+$", RegexOptions.Compiled)
let private rendaFixa =
[
"LCI"; "LCA"; "CDB";
"CRI"; "CRA"; "Debenture";
"IPCA"; "CDI"; "SELIC"; "Tesouro"
]
let private matchAnyRegex regexes =
fun (ticker:string)-> regexes |> List.exists (fun (regex: Regex) -> regex.IsMatch(ticker))
let private isRendaFixa =
rendaFixa
|> List.map (fun produto -> Regex($"(^|\s){produto}(\s|$)", RegexOptions.IgnoreCase))
|> matchAnyRegex
let private poupancaRegex =
Regex(@"(^|\s)poupan.a(\s|$)", RegexOptions.IgnoreCase)
let private isCDI100Regex =
Regex(@"^(?=.*100)(?=.*CDI(\s|$)).{6,}$", RegexOptions.IgnoreCase)
let private isCaixa =
[ poupancaRegex ; isCDI100Regex ]
|> matchAnyRegex
let getNumeroTicker ativo =
let m = numberAtEnd.Match(ativo)
if m.Success then m.Value |> int |> Some
else None
let getTipoAtivo fallback (ticker: string) =
let ticker = ticker.TrimEnd('F')
ticker
|> getNumeroTicker
|> Option.bind (function
| n when n >= 3 && n <= 6 -> Some Acao
| n when n >= 31 && n <= 35 -> Some BDR
| _ -> None
)
|> Option.defaultWith (fun () ->
if isCaixa ticker then
Caixa
elif isRendaFixa ticker then
RendaFixa
else
fallback
|> Map.tryFind ticker
|> Option.map (fun x -> x.Tipo)
|> Option.defaultValue Outro
)