Skip to content

Commit

Permalink
Adding an optional parameter to to_string to end with a newline
Browse files Browse the repository at this point in the history
Signed-off-by: Paul-Elliot <[email protected]>
  • Loading branch information
panglesd committed Jan 24, 2022
1 parent 05d0a00 commit 6696f55
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 3 deletions.
4 changes: 3 additions & 1 deletion lib/write.ml
Original file line number Diff line number Diff line change
Expand Up @@ -460,10 +460,12 @@ let to_output ?buf ?(len=4096) ?std out x =
out#output (Buffer.contents ob) 0 (Buffer.length ob);
()

let to_file ?len ?std file x =
let to_file ?len ?std ?(newline = true) file x =
let oc = open_out file in
try
to_channel ?len ?std oc x;
if newline then
output_string oc "\n";
close_out oc
with e ->
close_out_noerr oc;
Expand Down
5 changes: 4 additions & 1 deletion lib/write.mli
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,12 @@ val to_output :
val to_file :
?len:int ->
?std:bool ->
?newline:bool ->
string -> t -> unit
(** Write a compact JSON value to a file.
See [to_string] for the role of the optional arguments. *)
See [to_string] for the role of the optional arguments.
@param newline whether to end the content of the file with a new line.
Optional parameter with value [true] by default. *)

val to_buffer :
?std:bool ->
Expand Down
2 changes: 1 addition & 1 deletion test/test_write.ml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ let to_file () =
close_in ic;
s
in
Alcotest.(check string) __LOC__ Fixtures.json_string file_content;
Alcotest.(check string) __LOC__ (Fixtures.json_string ^ "\n") file_content;
Sys.remove output_file

let single_json = [
Expand Down

0 comments on commit 6696f55

Please sign in to comment.