From 225068497c5fd41da12030a6bbf58a0fc9c294d0 Mon Sep 17 00:00:00 2001 From: Joris Date: Sun, 9 Aug 2020 14:44:02 +0200 Subject: Import from CSV --- src/State.ml | 64 ++++++++++++++++++++++++++++++++++++++++++++---------------- 1 file changed, 47 insertions(+), 17 deletions(-) (limited to 'src/State.ml') diff --git a/src/State.ml b/src/State.ml index 4c6cedb..c1cb99d 100644 --- a/src/State.ml +++ b/src/State.ml @@ -60,25 +60,10 @@ let from_url_string str = (Js.Array.from (Js.String.castToArrayLike ((String.decode str) ^ sep))) in res -(* CSV Serialization *) - -let to_csv_line marker = - [| Js.Float.toString marker.pos.lat - ; Js.Float.toString marker.pos.lng - ; marker.name - ; marker.color - ; marker.icon - |] - |> Js.Array.joinWith "," - -let to_csv_string state = - state - |> Js.Array.map to_csv_line - |> Fun.flip Js.Array.concat [| "lat,lng,name,color,icon" |] - |> Js.Array.joinWith "\n" - (* Colors *) +let default_color = "#3f92cf" + let colors = Js.Array.reduce (fun colors marker -> @@ -87,3 +72,48 @@ let colors = else colors) [| |] + +(* CSV Serialization *) + +let lat_key = "lat" +let lng_key = "lng" +let name_key = "name" +let color_key = "color" +let icon_key = "icon" + +let to_csv_string state = + let to_csv_line marker = + [| Js.Float.toString marker.pos.lat + ; Js.Float.toString marker.pos.lng + ; marker.name + ; marker.color + ; marker.icon + |] + in let + header = + [| lat_key; lng_key; name_key; color_key; icon_key |] + in + state + |> Js.Array.map to_csv_line + |> Fun.flip Js.Array.concat [| header |] + |> CSV.to_string + +let from_dicts dicts = + Js.Array.map + (fun dict -> + (* let get key default = Js.Dict.get dict key |> Option.withDefault default in *) + { pos = + { lat = + Js.Dict.get dict lat_key + |> Option.map Js.Float.fromString + |> Option.withDefault 0.0 + ; lng = + Js.Dict.get dict lng_key + |> Option.map Js.Float.fromString + |> Option.withDefault 0.0 + } + ; name = Js.Dict.get dict name_key |> Option.withDefault "" + ; color = Js.Dict.get dict color_key |> Option.withDefault default_color + ; icon = Js.Dict.get dict icon_key |> Option.withDefault "" + }) + dicts -- cgit v1.2.3