Skip to content

Commit 3ca735a

Browse files
authored
Add Option.all* & Result.all* helpers (#7181)
* Add Option.all* & Result.all* helpers * Add changelog * Apply formatting * Simplify Option.all code and check tests in
1 parent 233db91 commit 3ca735a

11 files changed

+1173
-0
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
1313
# 12.0.0-alpha.6 (Unreleased)
1414
- Fix exponential notation syntax. https://github.com/rescript-lang/rescript/pull/7174
15+
- Add `Option.all` & `Result.all` helpers. https://github.com/rescript-lang/rescript/pull/7181
1516

1617
#### :bug: Bug fix
1718
- Fix bug where a ref assignment is moved ouside a conditional. https://github.com/rescript-lang/rescript/pull/7176

lib/es6/Option.js

+106
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,106 @@ function compare(a, b, cmp) {
9797
}
9898
}
9999

100+
function all(options) {
101+
let acc = [];
102+
let hasNone = false;
103+
let index = 0;
104+
while (hasNone === false && index < options.length) {
105+
let value = options[index];
106+
if (value !== undefined) {
107+
acc.push(Primitive_option.valFromOption(value));
108+
index = index + 1 | 0;
109+
} else {
110+
hasNone = true;
111+
}
112+
};
113+
if (hasNone) {
114+
return;
115+
} else {
116+
return acc;
117+
}
118+
}
119+
120+
function all2(param) {
121+
let b = param[1];
122+
let a = param[0];
123+
if (a !== undefined && b !== undefined) {
124+
return [
125+
Primitive_option.valFromOption(a),
126+
Primitive_option.valFromOption(b)
127+
];
128+
}
129+
130+
}
131+
132+
function all3(param) {
133+
let c = param[2];
134+
let b = param[1];
135+
let a = param[0];
136+
if (a !== undefined && b !== undefined && c !== undefined) {
137+
return [
138+
Primitive_option.valFromOption(a),
139+
Primitive_option.valFromOption(b),
140+
Primitive_option.valFromOption(c)
141+
];
142+
}
143+
144+
}
145+
146+
function all4(param) {
147+
let d = param[3];
148+
let c = param[2];
149+
let b = param[1];
150+
let a = param[0];
151+
if (a !== undefined && b !== undefined && c !== undefined && d !== undefined) {
152+
return [
153+
Primitive_option.valFromOption(a),
154+
Primitive_option.valFromOption(b),
155+
Primitive_option.valFromOption(c),
156+
Primitive_option.valFromOption(d)
157+
];
158+
}
159+
160+
}
161+
162+
function all5(param) {
163+
let e = param[4];
164+
let d = param[3];
165+
let c = param[2];
166+
let b = param[1];
167+
let a = param[0];
168+
if (a !== undefined && b !== undefined && c !== undefined && d !== undefined && e !== undefined) {
169+
return [
170+
Primitive_option.valFromOption(a),
171+
Primitive_option.valFromOption(b),
172+
Primitive_option.valFromOption(c),
173+
Primitive_option.valFromOption(d),
174+
Primitive_option.valFromOption(e)
175+
];
176+
}
177+
178+
}
179+
180+
function all6(param) {
181+
let f = param[5];
182+
let e = param[4];
183+
let d = param[3];
184+
let c = param[2];
185+
let b = param[1];
186+
let a = param[0];
187+
if (a !== undefined && b !== undefined && c !== undefined && d !== undefined && e !== undefined && f !== undefined) {
188+
return [
189+
Primitive_option.valFromOption(a),
190+
Primitive_option.valFromOption(b),
191+
Primitive_option.valFromOption(c),
192+
Primitive_option.valFromOption(d),
193+
Primitive_option.valFromOption(e),
194+
Primitive_option.valFromOption(f)
195+
];
196+
}
197+
198+
}
199+
100200
let mapWithDefault = mapOr;
101201

102202
let getWithDefault = getOr;
@@ -116,5 +216,11 @@ export {
116216
isNone,
117217
equal,
118218
compare,
219+
all,
220+
all2,
221+
all3,
222+
all4,
223+
all5,
224+
all6,
119225
}
120226
/* No side effect */

lib/es6/Result.js

+250
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,250 @@ function mapError(r, f) {
9898
}
9999
}
100100

101+
function all(results) {
102+
let acc = [];
103+
let returnValue;
104+
let index = 0;
105+
while (returnValue === undefined && index < results.length) {
106+
let err = results[index];
107+
if (err.TAG === "Ok") {
108+
acc.push(err._0);
109+
index = index + 1 | 0;
110+
} else {
111+
returnValue = err;
112+
}
113+
};
114+
let error = returnValue;
115+
if (error !== undefined) {
116+
return error;
117+
} else {
118+
return {
119+
TAG: "Ok",
120+
_0: acc
121+
};
122+
}
123+
}
124+
125+
function all2(param) {
126+
let b = param[1];
127+
let a = param[0];
128+
if (a.TAG === "Ok") {
129+
if (b.TAG === "Ok") {
130+
return {
131+
TAG: "Ok",
132+
_0: [
133+
a._0,
134+
b._0
135+
]
136+
};
137+
} else {
138+
return {
139+
TAG: "Error",
140+
_0: b._0
141+
};
142+
}
143+
} else {
144+
return {
145+
TAG: "Error",
146+
_0: a._0
147+
};
148+
}
149+
}
150+
151+
function all3(param) {
152+
let c = param[2];
153+
let b = param[1];
154+
let a = param[0];
155+
if (a.TAG === "Ok") {
156+
if (b.TAG === "Ok") {
157+
if (c.TAG === "Ok") {
158+
return {
159+
TAG: "Ok",
160+
_0: [
161+
a._0,
162+
b._0,
163+
c._0
164+
]
165+
};
166+
} else {
167+
return {
168+
TAG: "Error",
169+
_0: c._0
170+
};
171+
}
172+
} else {
173+
return {
174+
TAG: "Error",
175+
_0: b._0
176+
};
177+
}
178+
} else {
179+
return {
180+
TAG: "Error",
181+
_0: a._0
182+
};
183+
}
184+
}
185+
186+
function all4(param) {
187+
let d = param[3];
188+
let c = param[2];
189+
let b = param[1];
190+
let a = param[0];
191+
if (a.TAG === "Ok") {
192+
if (b.TAG === "Ok") {
193+
if (c.TAG === "Ok") {
194+
if (d.TAG === "Ok") {
195+
return {
196+
TAG: "Ok",
197+
_0: [
198+
a._0,
199+
b._0,
200+
c._0,
201+
d._0
202+
]
203+
};
204+
} else {
205+
return {
206+
TAG: "Error",
207+
_0: d._0
208+
};
209+
}
210+
} else {
211+
return {
212+
TAG: "Error",
213+
_0: c._0
214+
};
215+
}
216+
} else {
217+
return {
218+
TAG: "Error",
219+
_0: b._0
220+
};
221+
}
222+
} else {
223+
return {
224+
TAG: "Error",
225+
_0: a._0
226+
};
227+
}
228+
}
229+
230+
function all5(param) {
231+
let e = param[4];
232+
let d = param[3];
233+
let c = param[2];
234+
let b = param[1];
235+
let a = param[0];
236+
if (a.TAG === "Ok") {
237+
if (b.TAG === "Ok") {
238+
if (c.TAG === "Ok") {
239+
if (d.TAG === "Ok") {
240+
if (e.TAG === "Ok") {
241+
return {
242+
TAG: "Ok",
243+
_0: [
244+
a._0,
245+
b._0,
246+
c._0,
247+
d._0,
248+
e._0
249+
]
250+
};
251+
} else {
252+
return {
253+
TAG: "Error",
254+
_0: e._0
255+
};
256+
}
257+
} else {
258+
return {
259+
TAG: "Error",
260+
_0: d._0
261+
};
262+
}
263+
} else {
264+
return {
265+
TAG: "Error",
266+
_0: c._0
267+
};
268+
}
269+
} else {
270+
return {
271+
TAG: "Error",
272+
_0: b._0
273+
};
274+
}
275+
} else {
276+
return {
277+
TAG: "Error",
278+
_0: a._0
279+
};
280+
}
281+
}
282+
283+
function all6(param) {
284+
let f = param[5];
285+
let e = param[4];
286+
let d = param[3];
287+
let c = param[2];
288+
let b = param[1];
289+
let a = param[0];
290+
if (a.TAG === "Ok") {
291+
if (b.TAG === "Ok") {
292+
if (c.TAG === "Ok") {
293+
if (d.TAG === "Ok") {
294+
if (e.TAG === "Ok") {
295+
if (f.TAG === "Ok") {
296+
return {
297+
TAG: "Ok",
298+
_0: [
299+
a._0,
300+
b._0,
301+
c._0,
302+
d._0,
303+
e._0,
304+
f._0
305+
]
306+
};
307+
} else {
308+
return {
309+
TAG: "Error",
310+
_0: f._0
311+
};
312+
}
313+
} else {
314+
return {
315+
TAG: "Error",
316+
_0: e._0
317+
};
318+
}
319+
} else {
320+
return {
321+
TAG: "Error",
322+
_0: d._0
323+
};
324+
}
325+
} else {
326+
return {
327+
TAG: "Error",
328+
_0: c._0
329+
};
330+
}
331+
} else {
332+
return {
333+
TAG: "Error",
334+
_0: b._0
335+
};
336+
}
337+
} else {
338+
return {
339+
TAG: "Error",
340+
_0: a._0
341+
};
342+
}
343+
}
344+
101345
let mapWithDefault = mapOr;
102346

103347
let getWithDefault = getOr;
@@ -116,5 +360,11 @@ export {
116360
compare,
117361
forEach,
118362
mapError,
363+
all,
364+
all2,
365+
all3,
366+
all4,
367+
all5,
368+
all6,
119369
}
120370
/* No side effect */

0 commit comments

Comments
 (0)