Skip to content

Commit 1e57631

Browse files
committed
Fix equality, check in JS output & add tests
1 parent 8f117ff commit 1e57631

File tree

4 files changed

+42
-3
lines changed

4 files changed

+42
-3
lines changed

jscomp/others/belt_Array.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -269,7 +269,7 @@ let getByU a p =
269269
let l = length a in
270270
let i = ref 0 in
271271
let r = ref None in
272-
while !r == None && !i < l do
272+
while !r = None && !i < l do
273273
let v = (getUnsafe a !i) in
274274
if p v [@bs] then
275275
begin

jscomp/test/bs_array_test.js

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1542,7 +1542,23 @@ b("File \"bs_array_test.ml\", line 331, characters 4-11", Belt_Array.cmp(/* arra
15421542
3
15431543
], Caml_primitive.caml_int_compare) > 0);
15441544

1545-
Mt.from_pair_suites("File \"bs_array_test.ml\", line 334, characters 23-30", suites[0]);
1545+
eq("File \"bs_array_test.ml\", line 334, characters 5-12", Belt_Array.getBy(/* array */[
1546+
1,
1547+
2,
1548+
3
1549+
], (function (x) {
1550+
return x > 1;
1551+
})), 2);
1552+
1553+
eq("File \"bs_array_test.ml\", line 335, characters 5-12", Belt_Array.getBy(/* array */[
1554+
1,
1555+
2,
1556+
3
1557+
], (function (x) {
1558+
return x > 3;
1559+
})), undefined);
1560+
1561+
Mt.from_pair_suites("File \"bs_array_test.ml\", line 337, characters 23-30", suites[0]);
15461562

15471563
var A = 0;
15481564

jscomp/test/bs_array_test.ml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -328,7 +328,10 @@ let () =
328328
b __LOC__ (A.cmp [|0;1;2;3|] [|1;2;3|] compare > 0) ;
329329
b __LOC__ (A.cmp [|1;2;3|] [|0;1;2|] (fun x y -> compare x y) > 0);
330330
b __LOC__ (A.cmp [|1;2;3|] [|1;2;3|] (fun x y -> compare x y) = 0);
331-
b __LOC__ (A.cmp [|1;2;4|] [|1;2;3|] (fun x y -> compare x y) > 0);
331+
b __LOC__ (A.cmp [|1;2;4|] [|1;2;3|] (fun x y -> compare x y) > 0)
332332

333+
let () =
334+
eq __LOC__ (A.getBy [|1;2;3|] (fun x -> x > 1)) (Some 2);
335+
eq __LOC__ (A.getBy [|1;2;3|] (fun x -> x > 3)) None;
333336

334337
;; Mt.from_pair_suites __LOC__ !suites

lib/js/belt_Array.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -311,6 +311,24 @@ function map(a, f) {
311311
return mapU(a, Curry.__1(f));
312312
}
313313

314+
function getByU(a, p) {
315+
var l = a.length;
316+
var i = 0;
317+
var r = undefined;
318+
while(r === undefined && i < l) {
319+
var v = a[i];
320+
if (p(v)) {
321+
r = Caml_option.some(v);
322+
}
323+
i = i + 1 | 0;
324+
};
325+
return r;
326+
}
327+
328+
function getBy(a, p) {
329+
return getByU(a, Curry.__1(p));
330+
}
331+
314332
function keepU(a, f) {
315333
var l = a.length;
316334
var r = new Array(l);
@@ -657,6 +675,8 @@ exports.forEachU = forEachU;
657675
exports.forEach = forEach;
658676
exports.mapU = mapU;
659677
exports.map = map;
678+
exports.getByU = getByU;
679+
exports.getBy = getBy;
660680
exports.keepU = keepU;
661681
exports.keep = keep;
662682
exports.keepWithIndexU = keepWithIndexU;

0 commit comments

Comments
 (0)