-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtuple.js
57 lines (57 loc) · 1.74 KB
/
tuple.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
import { bad, good, sure } from './core.js';
/**
* @deprecated
*
* It's possible to add a spread operator.
* But it's seems that it would add a lot of complexity and will have to link the library too much.
*
* A tuple would have to know about both the spread and an array.
*
* It made sense for `object` and `optional`, since they're linked.
* But for tuples it can be more clearly implemented as a separate user-land function.
*/
export function spread_NOT_IMPLEMENTED(schema) {
// IMPORTANT: It's important to pass a new function here
const val = sure(value => schema(value), {
parent: spread_NOT_IMPLEMENTED,
schema,
});
// @ts-expect-error - this is fine
return val;
}
export function tuple(arr) {
const struct = sure(value => {
if (!Array.isArray(value)) {
return bad([]);
}
let atLeastOneBad = false;
let bads = [];
let goods = [];
for (let i = 0; i < arr.length; i++) {
// @ts-expect-error
const elem = arr[i];
const [good, unsure] = elem(value[i]);
if (good) {
goods.push(unsure);
// This is necessary in order to maintain the same length
bads.push(undefined);
}
else {
bads.push(unsure);
// Since the `bads` array can containe `undefined` values, it's more clear to
// have an imperative boolean to make the check
atLeastOneBad = true;
}
}
if (atLeastOneBad) {
return bad(bads);
}
return good(goods);
}, {
parent: tuple,
initial: arr,
});
struct.meta;
// @ts-expect-error
return struct;
}