SPARK-1108 Enums for Humans 1/2

This commit is contained in:
Eric Harding committed 2018-05-18 08:18:29 -04:00
1 parent 68ca16103d
commit ba02db4e40
2 files changed
+58

No files matched your search

+57
View File
@@ -0,0 +1,57 @@
module EnumUtils
module String =
let commonPrefixLen (items:string[]) =
let maxlen = Array.map String.length items |> Array.min
let rec loop i =
let first = items.[0].[i]
printfn "%i %i" i maxlen
if items |> Array.forall (fun item -> item.[i] = first) && i+1 < maxlen then
loop (i+1)
else
i
loop 0
let trimCommonPrefix items (item:string) =
let len = commonPrefixLen items
item.[len..]
let toHuman items item =
if Array.length items < 2 then
item
else
if item = items.[0] then "*"
else
trimCommonPrefix items item
let allToHuman items =
let len = commonPrefixLen items
items |> Array.mapi (fun i item -> if i = 0 then "*" else item.[len..])
let fromHuman (items:string[]) (item:string) =
if item = "*" then
items.[0]
else
let len = String.length item
items
|> Array.find (fun i -> i.[i.Length-len..] = item)
let toHuman<'a> (items:'a[]) (item:'a) =
String.toHuman (unbox items) (unbox item)
let allToHuman<'a> (items:'a[]) =
String.allToHuman (unbox items)
let inline fromHuman<'a> (items:'a[]) (item:string) : 'a =
String.fromHuman (unbox items) item |> unbox
// let testData = [|
// "SECURITYTYPE_UNSET"
// "SECURITYTYPE_STOCK"
// "SECURITYTYPE_OPTION"
// "SECURITYTYPE_FUTURE"
// "SECURITYTYPE_SPREAD"
// "SECURITYTYPE_SYNTHETIC" |]
// let h = toHuman testData "SECURITYTYPE_UNSET"
// fromHuman testData h
+1
View File
@@ -7,6 +7,7 @@
<Compile Include="../../../../build/web/api/DynamicService.fs" />
<Compile Include="Imports.fs" />
<Compile Include="Util/JSMap.fs" />
<Compile Include="Util/EnumUtils.fs" />
<Compile Include="../../../../build/web/api/fable/timestamp.fable.fs" />
<Compile Include="../../../../build/web/api/fable/apicore.fable.fs" />
<Compile Include="../../../../build/web/api/fable/apioptions.fable.fs" />