Skip to content

Commit

Permalink
feat: improve Js.Re and change some functions to pipe-last (#969)
Browse files Browse the repository at this point in the history
* feat: improve Js.Re and change some functions to pipe-last

* chore: add changelog entry
  • Loading branch information
anmonteiro authored Dec 11, 2023
1 parent d8de798 commit 2aedf0c
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 15 deletions.
2 changes: 2 additions & 0 deletions Changes.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
---------------
Expand Down
8 changes: 4 additions & 4 deletions jscomp/runtime/js_re.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -173,8 +173,8 @@ let result = re |. Js.Re.exec_ "The Quick Brown Fox Jumps Over The Lazy Dog"
@see <https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/exec> 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
Expand Down
4 changes: 2 additions & 2 deletions jscomp/test/gpr_1501_test.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 1 addition & 1 deletion jscomp/test/gpr_3895_test.ml
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
let f re =
let _ = re |. Js.Re.exec "banana" in
let _ = re |> Js.Re.exec "banana" in
3
16 changes: 8 additions & 8 deletions jscomp/test/js_re_test.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -14,31 +14,31 @@ 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
Eq (contentOf "div" "<div>Hi</div>", Some "Hi")
);

"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 ->
FailWith "regex should match"
);

"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
);

"test_str", (fun _ ->
let res = "foo"
|. Js.Re.fromString
|. Js.Re.test "#foo#" in
|> Js.Re.test "#foo#" in

Eq(true, res)
);
Expand All @@ -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 ->
Expand All @@ -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 ->
Expand All @@ -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 _ ->
Expand Down

0 comments on commit 2aedf0c

Please sign in to comment.