Skip to content

Commit 04b638d

Browse files
committed
add Belt.Array.joinWith
rescript implementation of Js Array.prototype.join() Signed-Off-By: Paul Tsnobiladzé <paul.tsnobiladze@gmail.com>
1 parent a6a3585 commit 04b638d

9 files changed

+155
-76
lines changed

jscomp/main/builtin_cmi_datasets.ml

+3-3
Large diffs are not rendered by default.

jscomp/main/builtin_cmj_datasets.ml

+16-16
Large diffs are not rendered by default.

jscomp/others/belt_Array.ml

+11
Original file line numberDiff line numberDiff line change
@@ -511,3 +511,14 @@ let unzip a =
511511
a2.!(i) <- v2
512512
done;
513513
(a1, a2)
514+
515+
let joinWithU a sep toString =
516+
match length a with
517+
| 0 -> ""
518+
| l ->
519+
let lastIndex = l - 1 in
520+
let rec aux i res =
521+
if i = lastIndex then res ^ toString a.!(i) [@bs]
522+
else aux (i + 1) (res ^ toString a.!(i) [@bs] ^ sep) in
523+
aux 0 ""
524+
let joinWith a sep toString = joinWithU a sep (fun [@bs] x -> toString x)

jscomp/others/belt_Array.mli

+16
Original file line numberDiff line numberDiff line change
@@ -524,6 +524,22 @@ val reduceWithIndex: 'a array -> 'b -> ('b -> 'a -> int -> 'b) -> 'b
524524
]}
525525
*)
526526

527+
val joinWithU: 'a array -> string -> ('a -> string [@bs]) -> string
528+
val joinWith: 'a array -> string -> ('a -> string) -> string
529+
(** [joinWith xs sep toString]
530+
531+
Concatenates all the elements of [xs] converted to string with [toString], each separated by [sep], the string
532+
given as the second argument, into a single string.
533+
If the array has only one element, then that element will be returned
534+
without using the separator.
535+
If the array is empty, the empty string will be returned.
536+
@example{[
537+
joinWith [|0; 1|] ", " string_of_int = "0, 1"
538+
joinWith [||] " " string_of_int = ""
539+
joinWith [|1|] " " string_of_int = "1"
540+
]}
541+
*)
542+
527543
val someU: 'a array -> ('a -> bool [@bs]) -> bool
528544
val some: 'a array -> ('a -> bool) -> bool
529545
(** [some xs p]

lib/4.06.1/unstable/js_compiler.ml

+19-19
Large diffs are not rendered by default.

lib/4.06.1/unstable/js_refmt_compiler.ml

+19-19
Large diffs are not rendered by default.

lib/4.06.1/whole_compiler.ml

+19-19
Large diffs are not rendered by default.

lib/es6/belt_Array.js

+26
Original file line numberDiff line numberDiff line change
@@ -652,6 +652,30 @@ function unzip(a) {
652652
];
653653
}
654654

655+
function joinWithU(a, sep, toString) {
656+
var l = a.length;
657+
if (l === 0) {
658+
return "";
659+
}
660+
var lastIndex = l - 1 | 0;
661+
var _i = 0;
662+
var _res = "";
663+
while(true) {
664+
var res = _res;
665+
var i = _i;
666+
if (i === lastIndex) {
667+
return res + toString(a[i]);
668+
}
669+
_res = res + (toString(a[i]) + sep);
670+
_i = i + 1 | 0;
671+
continue ;
672+
};
673+
}
674+
675+
function joinWith(a, sep, toString) {
676+
return joinWithU(a, sep, Curry.__1(toString));
677+
}
678+
655679
export {
656680
get ,
657681
getExn ,
@@ -707,6 +731,8 @@ export {
707731
reduceReverse2 ,
708732
reduceWithIndexU ,
709733
reduceWithIndex ,
734+
joinWithU ,
735+
joinWith ,
710736
someU ,
711737
some ,
712738
everyU ,

lib/js/belt_Array.js

+26
Original file line numberDiff line numberDiff line change
@@ -652,6 +652,30 @@ function unzip(a) {
652652
];
653653
}
654654

655+
function joinWithU(a, sep, toString) {
656+
var l = a.length;
657+
if (l === 0) {
658+
return "";
659+
}
660+
var lastIndex = l - 1 | 0;
661+
var _i = 0;
662+
var _res = "";
663+
while(true) {
664+
var res = _res;
665+
var i = _i;
666+
if (i === lastIndex) {
667+
return res + toString(a[i]);
668+
}
669+
_res = res + (toString(a[i]) + sep);
670+
_i = i + 1 | 0;
671+
continue ;
672+
};
673+
}
674+
675+
function joinWith(a, sep, toString) {
676+
return joinWithU(a, sep, Curry.__1(toString));
677+
}
678+
655679
exports.get = get;
656680
exports.getExn = getExn;
657681
exports.set = set;
@@ -706,6 +730,8 @@ exports.reduceReverse2U = reduceReverse2U;
706730
exports.reduceReverse2 = reduceReverse2;
707731
exports.reduceWithIndexU = reduceWithIndexU;
708732
exports.reduceWithIndex = reduceWithIndex;
733+
exports.joinWithU = joinWithU;
734+
exports.joinWith = joinWith;
709735
exports.someU = someU;
710736
exports.some = some;
711737
exports.everyU = everyU;

0 commit comments

Comments
 (0)