aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/arrayUtils.ml8
-rw-r--r--src/domUtils.ml37
-rw-r--r--src/main.ml63
-rw-r--r--src/number.ml57
4 files changed, 0 insertions, 165 deletions
diff --git a/src/arrayUtils.ml b/src/arrayUtils.ml
deleted file mode 100644
index 75319d8..0000000
--- a/src/arrayUtils.ml
+++ /dev/null
@@ -1,8 +0,0 @@
-let flatMap (f : 'a -> 'b option) (xs : 'a Js.Array.t) : 'b Js.Array.t =
- xs |> Js.Array.map f
- |> Js.Array.filter (fun maybe ->
- match maybe with Some _ -> true | None -> false)
- |> Js.Array.map (fun maybe ->
- match maybe with
- | Some x -> x
- | None -> Js.Exn.raiseError "Unexpected None")
diff --git a/src/domUtils.ml b/src/domUtils.ml
deleted file mode 100644
index 282ac12..0000000
--- a/src/domUtils.ml
+++ /dev/null
@@ -1,37 +0,0 @@
-open Webapi.Dom
-
-let toggleClassName (element : Dom.element) (className : string) : unit =
- Element.setClassName element
- (if Element.className element == className then "" else className)
-
-type child = TextChild of string | ElemChild of Dom.element
-
-let h (tag : string) (attributes : (string * string) Js.Array.t)
- (children : child Js.Array.t) : Dom.element =
- let element = Document.createElement tag document in
- let () =
- attributes
- |> Js.Array.forEach (fun a -> Element.setAttribute (fst a) (snd a) element)
- in
- let () =
- children
- |> Js.Array.forEach (fun c ->
- match c with
- | TextChild t ->
- Element.appendChild (Document.createTextNode t document) element
- | ElemChild e -> Element.appendChild e element)
- in
- element
-
-external replace_child : Dom.node -> Dom.element -> Dom.element -> unit
- = "replaceChild"
- [@@bs.send]
-
-let replace (element : Dom.element) (replacement : Dom.element) : unit =
- match Element.parentNode element with
- | Some parent -> replace_child parent replacement element
- | _ -> ()
-
-external value : Dom.eventTarget -> string option = "value" [@@bs.get]
-
-external setValue : Dom.element -> string -> unit = "value" [@@bs.set]
diff --git a/src/main.ml b/src/main.ml
deleted file mode 100644
index a2174f2..0000000
--- a/src/main.ml
+++ /dev/null
@@ -1,63 +0,0 @@
-open Webapi.Dom
-
-(* Set up inputs for the ingredients *)
-
-type ingredient = { quantity : float; element : Dom.element }
-
-let ingredients : ingredient Js.Array.t =
- document
- |> Document.querySelectorAll ".g-Recipe__Content ul > li"
- |> NodeList.toArray
- |> ArrayUtils.flatMap (fun node ->
- Belt.Option.map (Element.ofNode node) (fun e -> ("li", e)))
- |> Js.Array.concat
- ( match Document.querySelector ".g-Recipe__Content h1" document with
- | Some element -> [| ("h1", element) |]
- | _ -> [||] )
- |> ArrayUtils.flatMap (fun (tag, element) ->
- Belt.Option.map
- (Number.parseInsideText (Element.innerHTML element))
- (fun parsed ->
- let created = Number.createElement tag parsed in
- let () = DomUtils.replace element created.element in
- { quantity = parsed.number; element = created.numberInput }))
-
-(* Update ingredients amounts *)
-
-let () =
- ingredients
- |> Js.Array.forEach (fun ingredient ->
- Element.addEventListener "input"
- (fun e ->
- Belt.Option.forEach
- (DomUtils.value (Event.target e))
- (fun numberStr ->
- Belt.Option.forEach (Number.parse numberStr) (fun parsed ->
- let factor = parsed.number /. ingredient.quantity in
- ingredients
- |> Js.Array.forEach (fun otherIngredient ->
- if ingredient.element != otherIngredient.element
- then
- DomUtils.setValue otherIngredient.element
- (Number.prettyPrint
- (factor *. otherIngredient.quantity))
- else ()))))
- ingredient.element)
-
-(* Set up done marks for steps *)
-
-let () =
- document
- |> Document.querySelectorAll ".g-Recipe__Content ol > li"
- |> NodeList.toArray
- |> Js.Array.forEach (fun node ->
- match Element.ofNode node with
- | Some element ->
- Element.addEventListener "click"
- (fun e ->
- let () =
- DomUtils.toggleClassName element "g-Recipe__Completed"
- in
- Event.stopPropagation e)
- element
- | _ -> ())
diff --git a/src/number.ml b/src/number.ml
deleted file mode 100644
index cdd9ef8..0000000
--- a/src/number.ml
+++ /dev/null
@@ -1,57 +0,0 @@
-type parseInsideTextResult = { before : string; number : float; after : string }
-
-let execRegex (regex : Js.Re.t) (str : string) : string option Js.Array.t =
- match Js.Re.exec_ regex str with
- | Some result -> Js.Array.map Js.toOption (Js.Re.captures result)
- | None -> [||]
-
-let parseInsideText (str : string) : parseInsideTextResult option =
- match execRegex [%re "/^([^\\d]*)(\\d+)((\\.|,)(\\d+))?(.*)/"] str with
- | [| _; Some before; Some intPart; _; _; decPart; Some after |] ->
- Some
- {
- before;
- number =
- Js.Float.fromString
- ( intPart
- ^ Belt.Option.mapWithDefault decPart "" (fun str -> "." ^ str) );
- after;
- }
- | _ -> None
-
-type parseResult = { number : float; remaining : string }
-
-let parse (str : string) : parseResult option =
- match parseInsideText str with
- | Some parseResult ->
- if parseResult.before == "" then
- Some { number = parseResult.number; remaining = parseResult.after }
- else None
- | _ -> None
-
-type numberElement = { element : Dom.element; numberInput : Dom.element }
-
-let prettyPrint (number : float) : string =
- let strNumber = Js.Float.toString number in
- match Js.String.split "." strNumber with
- | [| intPart; decPart |] ->
- intPart ^ "," ^ Js.String.slice ~from:0 ~to_:2 decPart
- | _ -> strNumber
-
-let createElement (tag : string) (content : parseInsideTextResult) :
- numberElement =
- let numberInput =
- DomUtils.h "input"
- [| ("class", "g-Number"); ("value", prettyPrint content.number) |]
- [| DomUtils.TextChild "" |]
- in
- {
- element =
- DomUtils.h tag [||]
- [|
- DomUtils.TextChild content.before;
- DomUtils.ElemChild numberInput;
- DomUtils.TextChild content.after;
- |];
- numberInput;
- }