Skip to content

Commit 05fbe53

Browse files
committed
Refactor end_, add of_json
1 parent 1cd694b commit 05fbe53

File tree

1 file changed

+13
-18
lines changed

1 file changed

+13
-18
lines changed

src/nodejs.ml

Lines changed: 13 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -29,15 +29,17 @@ let m = Js.Unsafe.meth_call
2929
let i = Js.Unsafe.inject
3030

3131
(** Turn a JavaScript Object into a string *)
32-
let stringify o =
33-
m (Js.Unsafe.variable "JSON") "stringify" [|i o|] |> Js.to_string
32+
let stringify o = Js._JSON##stringify o |> Js.to_string
3433

3534
(** Turn an OCaml string into a JavaScript string *)
3635
let to_js_str s = Js.string s |> Js.Unsafe.inject
3736

3837
(** Turn a JavaScript Object into a Yojson object *)
3938
let to_json obj = stringify obj |> Yojson.Basic.from_string
4039

40+
(** Turn Yojson object into JavaScript Object *)
41+
let of_json j = Js._JSON##parse (j |> Yojson.Basic.to_string |> Js.string)
42+
4143
(** Create a JavaScript Object out of an alist *)
4244
let obj_of_alist a_l =
4345
List.map (fun (key, value) -> (key, Js.Unsafe.inject value)) a_l
@@ -705,26 +707,19 @@ module Http = struct
705707
(* method send_date *)
706708
(* method get_header *)
707709
(* method remove_header *)
708-
method write
709-
?(callback : (unit -> unit) option)
710-
?(encoding: string option)
711-
chunk : unit =
712-
match chunk with
713-
| String s ->
714-
m raw_js "write" [|to_js_str s|]
710+
method write ?(callback : (unit -> unit) option) ?encoding chunk : unit =
711+
match (chunk, encoding) with
712+
| (String s, Some e) ->
713+
m raw_js "write" [|to_js_str s; string_of_encoding e |> to_js_str|]
715714
| _ -> assert false
716715

717716
(* method add_trailers *)
718717

719-
method end_
720-
?data
721-
?(encoding: string option)
722-
?(callback : (unit -> unit) option)
723-
()
724-
: unit =
725-
match data with
726-
| Some (String s) ->
727-
m raw_js "end" [|to_js_str s|]
718+
method end_ ?data ?encoding ?(callback : (unit -> unit) option) () : unit =
719+
match (data, encoding) with
720+
| (Some (String s), None) -> m raw_js "end" [|to_js_str s|]
721+
| (Some (String s), Some e) ->
722+
m raw_js "end" [|to_js_str s; string_of_encoding e |> to_js_str|]
728723
| _ -> assert false
729724

730725
(* method finished *)

0 commit comments

Comments
 (0)