diff --git a/packages/melange.js/Js.ml b/packages/melange.js/Js.ml index e897c2961..628097667 100644 --- a/packages/melange.js/Js.ml +++ b/packages/melange.js/Js.ml @@ -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 = diff --git a/packages/melange.js/Js.mli b/packages/melange.js/Js.mli index f7655f629..81d1cd17b 100644 --- a/packages/melange.js/Js.mli +++ b/packages/melange.js/Js.mli @@ -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 diff --git a/packages/melange.js/dune b/packages/melange.js/dune index 79bc78050..f65719ee2 100644 --- a/packages/melange.js/dune +++ b/packages/melange.js/dune @@ -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) diff --git a/packages/melange.js/test.ml b/packages/melange.js/test.ml index da033e0e4..10fb86897 100644 --- a/packages/melange.js/test.ml +++ b/packages/melange.js/test.ml @@ -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")