aboutsummaryrefslogtreecommitdiff
path: root/src/State.ml
diff options
context:
space:
mode:
authorJoris2020-08-09 14:44:02 +0200
committerJoris2020-08-09 14:44:02 +0200
commit225068497c5fd41da12030a6bbf58a0fc9c294d0 (patch)
treea2432c1c8004a3e5897a4a9b445e256a3ca6c651 /src/State.ml
parentad6abcd5fc5e4e66062c8a01b511a1bd4bda2e94 (diff)
Import from CSV
Diffstat (limited to 'src/State.ml')
-rw-r--r--src/State.ml64
1 files changed, 47 insertions, 17 deletions
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