From 2aedf0c44432310da649e208b40e2b3176627af5 Mon Sep 17 00:00:00 2001 From: Antonio Nuno Monteiro Date: Mon, 11 Dec 2023 08:20:11 -0800 Subject: [PATCH] feat: improve Js.Re and change some functions to pipe-last (#969) * feat: improve Js.Re and change some functions to pipe-last * chore: add changelog entry --- Changes.md | 2 ++ jscomp/runtime/js_re.ml | 8 ++++---- jscomp/test/gpr_1501_test.ml | 4 ++-- jscomp/test/gpr_3895_test.ml | 2 +- jscomp/test/js_re_test.ml | 16 ++++++++-------- 5 files changed, 17 insertions(+), 15 deletions(-) diff --git a/Changes.md b/Changes.md index 21b0324ac8..a336446d7a 100644 --- a/Changes.md +++ b/Changes.md @@ -92,6 +92,8 @@ Unreleased ([#966](https://github.com/melange-re/melange/pull/966)) - BREAKING(runtime): Improve `Js.Date` and change some of its functions to pipe-last ([#967](https://github.com/melange-re/melange/pull/967)) +- BREAKING(runtime): Improve `Js.Re` and change some of its functions to + pipe-last ([#969](https://github.com/melange-re/melange/pull/969)) 2.2.0 2023-12-05 --------------- diff --git a/jscomp/runtime/js_re.ml b/jscomp/runtime/js_re.ml index 89402df852..e1f2ac7c43 100644 --- a/jscomp/runtime/js_re.ml +++ b/jscomp/runtime/js_re.ml @@ -154,8 +154,8 @@ external unicode : t -> bool = "unicode" [@@mel.get] (** returns a bool indicating whether the [unicode] flag is set *) -external exec : t -> string -> result option = "exec" -[@@mel.send] [@@mel.return null_to_opt] +external exec : string -> result option = "exec" +[@@mel.send.pipe: t] [@@mel.return null_to_opt] (** executes a search on a given string using the given RegExp object {b returns} [Some] {! result} if a match is found, [None] otherwise @@ -173,8 +173,8 @@ let result = re |. Js.Re.exec_ "The Quick Brown Fox Jumps Over The Lazy Dog" @see MDN *) -external test : t -> string -> bool = "test" -[@@mel.send] +external test : string -> bool = "test" +[@@mel.send.pipe: t] (** tests whether the given RegExp object will match a given string {b returns} [true] if a match is found, [false] otherwise diff --git a/jscomp/test/gpr_1501_test.ml b/jscomp/test/gpr_1501_test.ml index 9024cd4484..bb1d90fa30 100644 --- a/jscomp/test/gpr_1501_test.ml +++ b/jscomp/test/gpr_1501_test.ml @@ -13,10 +13,10 @@ let () = eq __LOC__ "Not_found" (Printexc.to_string Not_found); eq __LOC__ - (Js.Re.test [%re{|/Gpr_1501_test.A\/[0-9]+/|}] (Printexc.to_string A)) + (Js.Re.test (Printexc.to_string A) [%re{|/Gpr_1501_test.A\/[0-9]+/|}]) true; eq __LOC__ - (Js.Re.test [%re{|/Gpr_1501_test.B\/[0-9]+\(1\)/|}] (Printexc.to_string (B 1))) true + (Js.Re.test (Printexc.to_string (B 1)) [%re{|/Gpr_1501_test.B\/[0-9]+\(1\)/|}]) true let () = Mt.from_pair_suites __MODULE__ !suites diff --git a/jscomp/test/gpr_3895_test.ml b/jscomp/test/gpr_3895_test.ml index 9b168a1d9c..4858929cdd 100644 --- a/jscomp/test/gpr_3895_test.ml +++ b/jscomp/test/gpr_3895_test.ml @@ -1,3 +1,3 @@ let f re = - let _ = re |. Js.Re.exec "banana" in + let _ = re |> Js.Re.exec "banana" in 3 diff --git a/jscomp/test/js_re_test.ml b/jscomp/test/js_re_test.ml index b1655ebc0f..024d00fe9f 100644 --- a/jscomp/test/js_re_test.ml +++ b/jscomp/test/js_re_test.ml @@ -2,7 +2,7 @@ let suites = Mt.[ "captures", (fun _ -> let re = [%re "/(\\d+)-(?:(\\d+))?/g"] in let str = "3-" in - match re |. Js.Re.exec str with + match re |> Js.Re.exec str with | Some result -> let defined = (Js.Re.captures result).(1) in let undefined = (Js.Re.captures result).(2) in @@ -14,7 +14,7 @@ let suites = Mt.[ (* From the example in js_re.mli *) let contentOf tag xmlString = Js.Re.fromString ("<" ^ tag ^ ">(.*?)<\\/" ^ tag ^">") - |. Js.Re.exec xmlString + |> Js.Re.exec xmlString |. function | Some result -> Js.Nullable.toOption (Js.Re.captures result).(1) | None -> None in @@ -22,7 +22,7 @@ let suites = Mt.[ ); "exec_literal", (fun _ -> - match [%re "/[^.]+/"] |. Js.Re.exec "http://xxx.domain.com" with + match [%re "/[^.]+/"] |> Js.Re.exec "http://xxx.domain.com" with | Some res -> Eq(Js.Nullable.return "http://xxx", (Js.Re.captures res).(0)) | None -> @@ -30,7 +30,7 @@ let suites = Mt.[ ); "exec_no_match", (fun _ -> - match [%re "/https:\\/\\/(.*)/"] |. Js.Re.exec "http://xxx.domain.com" with + match [%re "/https:\\/\\/(.*)/"] |> Js.Re.exec "http://xxx.domain.com" with | Some _ -> FailWith "regex should not match" | None -> Ok true ); @@ -38,7 +38,7 @@ let suites = Mt.[ "test_str", (fun _ -> let res = "foo" |. Js.Re.fromString - |. Js.Re.test "#foo#" in + |> Js.Re.test "#foo#" in Eq(true, res) ); @@ -49,7 +49,7 @@ let suites = Mt.[ Eq(true, res |. Js.Re.global) ); "result_index", (fun _ -> - match "zbar" |. Js.Re.fromString |. Js.Re.exec "foobarbazbar" with + match "zbar" |. Js.Re.fromString |> Js.Re.exec "foobarbazbar" with | Some res -> Eq(8, res |> Js.Re.index) | None -> @@ -58,7 +58,7 @@ let suites = Mt.[ "result_input", (fun _ -> let input = "foobar" in - match [%re "/foo/g"] |. Js.Re.exec input with + match [%re "/foo/g"] |> Js.Re.exec input with | Some res -> Eq(input, res |> Js.Re.input) | None -> @@ -78,7 +78,7 @@ let suites = Mt.[ ); "t_lastIndex", (fun _ -> let re = [%re "/na/g"] in - let _ = re |. Js.Re.exec "banana" in (* Caml_option.null_to_opt post operation is not dropped in 4.06 which seems to be reduandant *) + let _ = re |> Js.Re.exec "banana" in (* Caml_option.null_to_opt post operation is not dropped in 4.06 which seems to be reduandant *) Eq(4, re |. Js.Re.lastIndex) ); "t_setLastIndex", (fun _ ->