Skip to content

Commit c2941b0

Browse files
committed
Merge remote-tracking branch 'upstream/main' into Object-docs
2 parents edb1798 + 3b84757 commit c2941b0

File tree

9 files changed

+119
-5
lines changed

9 files changed

+119
-5
lines changed

src/Core__Object.res

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,7 @@ y->Object.get("fruit") // Some("banana")
5757
@val
5858
external create: {..} => {..} = "Object.create"
5959

60-
@val
61-
external createWithProperties: ({..}, {..}) => {..} = "Object.create"
60+
@val external createWithProperties: ({..}, {..}) => {..} = "Object.create"
6261
@val external createWithNull: (@as(json`null`) _, unit) => {..} = "Object.create"
6362
@val external createWithNullAndProperties: (@as(json`null`) _, {..}) => {..} = "Object.create"
6463

@@ -80,15 +79,15 @@ Object.assign({"a": 1}, {"a": null}) // {"a": null}
8079
@val
8180
external assign: ({..}, {..}) => {..} = "Object.assign"
8281

83-
@variadic
84-
@val
8582
/**
8683
`assignMany(target, sources)` copies enumerable own properties from each source to the target, overwriting properties with the same name. Later sources' properties overwrite earlier ones. It returns the modified target object. A deep clone is not created; properties are copied by reference.
8784
8885
**Note:** ReScript provides [first-class support for immutable objects](https://rescript-lang.org/docs/manual/latest/object), including spreading one object into another. This is often more convenient than using `assign` or `assignMany`.
8986
9087
See [Object.assign on MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/assign) or [ECMAScript Language Specification](https://tc39.es/ecma262/multipage/fundamental-objects.html#sec-object.assign).
9188
*/
89+
@variadic
90+
@val
9291
external assignMany: ({..}, array<{..}>) => {..} = "Object.assign"
9392

9493
@val external copy: (@as(json`{}`) _, {..} as 'a) => 'a = "Object.assign"
@@ -123,6 +122,7 @@ x->Object.getSymbol(fruit) // Some("banana")
123122
*/
124123
@get_index
125124
external getSymbol: ({..}, Core__Symbol.t) => option<'a> = ""
125+
126126
@get_index external getSymbolUnsafe: ({..}, Core__Symbol.t) => 'a = ""
127127

128128
/**

src/Core__Result.mjs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,13 @@ function cmp(a, b, f) {
102102
}
103103
}
104104

105+
function forEach(r, f) {
106+
if (r.TAG === /* Ok */0) {
107+
return Curry._1(f, r._0);
108+
}
109+
110+
}
111+
105112
export {
106113
getExn ,
107114
mapWithDefault ,
@@ -112,5 +119,6 @@ export {
112119
isError ,
113120
eq ,
114121
cmp ,
122+
forEach ,
115123
}
116124
/* No side effect */

src/Core__Result.res

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,3 +91,9 @@ let cmpU = (a, b, f) =>
9191
}
9292

9393
let cmp = (a, b, f) => cmpU(a, b, (. x, y) => f(x, y))
94+
95+
let forEach = (r, f) =>
96+
switch r {
97+
| Ok(ok) => f(ok)
98+
| Error(_) => ()
99+
}

src/Core__Result.resi

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,3 +197,15 @@ let eq: (t<'a, 'c>, t<'b, 'd>, ('a, 'b) => bool) => bool
197197
```
198198
*/
199199
let cmp: (t<'a, 'c>, t<'b, 'd>, ('a, 'b) => int) => int
200+
201+
/**
202+
`forEach(res, f)` runs the provided function `f` on the `Ok` value. If `res` is `Error`, nothing happens.
203+
204+
## Examples
205+
206+
```rescript
207+
Result.forEach(Ok(3), Console.log) // Logs "3", returns ()
208+
Result.forEach(Error("x"), Console.log) // Does nothing, returns ()
209+
```
210+
*/
211+
let forEach: (t<'a, 'b>, 'a => unit) => unit

test/ObjectTests.res

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -177,4 +177,3 @@ Test.run(
177177
eq,
178178
None,
179179
)
180-

test/ResultTests.mjs

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
// Generated by ReScript, PLEASE EDIT WITH CARE
2+
3+
import * as Test from "./Test.mjs";
4+
import * as Caml_obj from "rescript/lib/es6/caml_obj.js";
5+
import * as Core__Result from "../src/Core__Result.mjs";
6+
7+
var eq = Caml_obj.equal;
8+
9+
function forEachIfOkCallFunction(param) {
10+
var called = {
11+
contents: []
12+
};
13+
Core__Result.forEach({
14+
TAG: /* Ok */0,
15+
_0: 3
16+
}, (function (i) {
17+
called.contents.push(i);
18+
}));
19+
Test.run([
20+
[
21+
"ResultTests.res",
22+
12,
23+
22,
24+
72
25+
],
26+
"forEach: if ok, call function with ok value once"
27+
], called.contents, eq, [3]);
28+
}
29+
30+
forEachIfOkCallFunction(undefined);
31+
32+
function forEachIfErrorDoNotCallFunction(param) {
33+
var called = {
34+
contents: []
35+
};
36+
Core__Result.forEach({
37+
TAG: /* Error */1,
38+
_0: 3
39+
}, (function (i) {
40+
called.contents.push(i);
41+
}));
42+
Test.run([
43+
[
44+
"ResultTests.res",
45+
19,
46+
22,
47+
63
48+
],
49+
"forEach: if error, do not call function"
50+
], called.contents, eq, []);
51+
}
52+
53+
forEachIfErrorDoNotCallFunction(undefined);
54+
55+
export {
56+
eq ,
57+
forEachIfOkCallFunction ,
58+
forEachIfErrorDoNotCallFunction ,
59+
}
60+
/* Not a pure module */

test/ResultTests.res

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
open RescriptCore
2+
3+
let eq = (a, b) => a == b
4+
5+
// =======
6+
// forEach
7+
// =======
8+
9+
let forEachIfOkCallFunction = () => {
10+
let called = ref([])
11+
Ok(3)->Result.forEach(i => called.contents->Array.push(i))
12+
Test.run(__POS_OF__("forEach: if ok, call function with ok value once"), called.contents, eq, [3])
13+
}
14+
forEachIfOkCallFunction()
15+
16+
let forEachIfErrorDoNotCallFunction = () => {
17+
let called = ref([])
18+
Error(3)->Result.forEach(i => called.contents->Array.push(i))
19+
Test.run(__POS_OF__("forEach: if error, do not call function"), called.contents, eq, [])
20+
}
21+
forEachIfErrorDoNotCallFunction()

test/TestSuite.mjs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import * as ArrayTests from "./ArrayTests.mjs";
66
import * as ErrorTests from "./ErrorTests.mjs";
77
import * as ObjectTests from "./ObjectTests.mjs";
88
import * as PromiseTest from "./PromiseTest.mjs";
9+
import * as ResultTests from "./ResultTests.mjs";
910

1011
var bign = TestTests.bign;
1112

@@ -29,6 +30,10 @@ var panicTest = ErrorTests.panicTest;
2930

3031
var $$catch = IntTests.$$catch;
3132

33+
var forEachIfOkCallFunction = ResultTests.forEachIfOkCallFunction;
34+
35+
var forEachIfErrorDoNotCallFunction = ResultTests.forEachIfErrorDoNotCallFunction;
36+
3237
var eq = ObjectTests.eq;
3338

3439
var nums = ObjectTests.nums;
@@ -57,6 +62,8 @@ export {
5762
Concurrently ,
5863
panicTest ,
5964
$$catch ,
65+
forEachIfOkCallFunction ,
66+
forEachIfErrorDoNotCallFunction ,
6067
eq ,
6168
nums ,
6269
d ,

test/TestSuite.res

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,5 @@ include PromiseTest
33
include ErrorTests
44
include ArrayTests
55
include IntTests
6+
include ResultTests
67
include ObjectTests

0 commit comments

Comments
 (0)