Skip to content

Commit

Permalink
feat: add native code to Js.String.split
Browse files Browse the repository at this point in the history
  • Loading branch information
pedrobslisboa committed Aug 23, 2024
1 parent 26affb8 commit 58be5f9
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 15 deletions.
15 changes: 14 additions & 1 deletion packages/melange.js/Js.ml
Original file line number Diff line number Diff line change
Expand Up @@ -757,7 +757,20 @@ end = struct
if start_idx >= end_idx then ""
else Stdlib.String.sub str start_idx (end_idx - start_idx)

let split ?sep ?limit _str = notImplemented "Js.String" "split"
let split ?sep ?limit str =
match%platform () with
| Client -> Js.String.split ~sep ~limit str
| Server -> (
let items =
Str.split (Str.regexp_string (Option.value sep ~default:"")) str
|> Stdlib.Array.of_list
in
let limit = Option.value limit ~default:(Stdlib.Array.length items) in

match limit with
| limit when limit >= 0 && limit < Stdlib.Array.length items ->
Stdlib.Array.sub items 0 limit
| _ -> items)

let splitByRe ~regexp ?limit str =
let rev_array arr =
Expand Down
4 changes: 0 additions & 4 deletions packages/melange.js/Js.mli
Original file line number Diff line number Diff line change
Expand Up @@ -380,11 +380,7 @@ module String : sig
not_implemented "is not implemented in native under server-reason-react.js"]

val slice : ?start:int -> ?end_:int -> t -> t

val split : ?sep:t -> ?limit:int -> t -> t array
[@@alert
not_implemented "is not implemented in native under server-reason-react.js"]

val splitByRe : regexp:Re.t -> ?limit:int -> t -> t nullable array
val startsWith : prefix:t -> ?start:int -> t -> bool
val substr : ?start:int -> ?len:int -> t -> t
Expand Down
2 changes: 1 addition & 1 deletion packages/melange.js/dune
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
(public_name server-reason-react.js)
(libraries quickjs lwt str)
(preprocess
(pps lwt_ppx)))
(pps lwt_ppx browser_ppx)))

(test
(name test)
Expand Down
34 changes: 25 additions & 9 deletions packages/melange.js/test.ml
Original file line number Diff line number Diff line change
Expand Up @@ -302,15 +302,31 @@ let string_tests =
(* assert_string (Js.String.sliceToEnd ~from:(-2) "abcdefg") "fg"; *)
assert_string (Js.String.slice ~start:7 "abcdefg") "");
test "split" (fun () ->
(* assert_string_array (split "-" "2018-01-02") [| "2018"; "01"; "02" |];
assert_string_array (split "," "a,b,,c") [| "a"; "b"; ""; "c" |];
assert_string_array
(split "::" "good::bad as great::awful")
[| "good"; "bad as great"; "awful" |];
assert_string_array
(split ";" "has-no-delimiter")
[| "has-no-delimiter" |] *)
());
assert_string_array
(Js.String.split ~sep:"-" "2018-01-02")
[| "2018"; "01"; "02" |];
assert_string_array
(Js.String.split ~sep:"," "a,b,,c")
[| "a"; "b"; ""; "c" |];
assert_string_array
(Js.String.split ~sep:"::" "good::bad as great::awful")
[| "good"; "bad as great"; "awful" |];
assert_string_array
(Js.String.split ~sep:";" "has-no-delimiter")
[| "has-no-delimiter" |];
assert_string_array
(Js.String.split ~sep:"-" "with-delimiter;" ~limit:1)
[| "with" |];
assert_string_array
(Js.String.split ~sep:"-" "with-delimiter-equal-to-zero" ~limit:0)
[||];
assert_string_array
(Js.String.split ~sep:"-" "with-delimiter-greater-than-length"
~limit:5)
[| "with"; "delimiter"; "greater"; "than"; "length" |];
assert_string_array
(Js.String.split ~sep:"-" "with-delimiter-less-than-zero" ~limit:(-2))
[| "with"; "delimiter"; "less"; "than"; "zero" |]);
test "splitAtMost" (fun () ->
(* assert_string_array
(splitAtMost "/" ~limit:3 "ant/bee/cat/dog/elk")
Expand Down

0 comments on commit 58be5f9

Please sign in to comment.