decoders 2.0.0-beta8 → 2.0.0-beta9
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +11 -2
- package/README.md +234 -72
- package/_utils.js +1 -1
- package/_utils.js.flow +3 -3
- package/_utils.mjs +1 -1
- package/core/array.d.ts +3 -0
- package/core/array.js +12 -1
- package/core/array.js.flow +9 -2
- package/core/array.mjs +11 -2
- package/core/boolean.js +3 -3
- package/core/boolean.js.flow +2 -2
- package/core/boolean.mjs +2 -2
- package/core/composition.d.ts +5 -1
- package/core/composition.js +33 -5
- package/core/composition.js.flow +31 -5
- package/core/composition.mjs +31 -5
- package/core/date.js +3 -3
- package/core/date.js.flow +2 -2
- package/core/date.mjs +2 -2
- package/core/dispatch.d.ts +1 -1
- package/core/dispatch.js +8 -6
- package/core/dispatch.js.flow +9 -8
- package/core/dispatch.mjs +6 -5
- package/core/either.d.ts +63 -58
- package/core/either.js +30 -42
- package/core/either.js.flow +38 -84
- package/core/either.mjs +34 -34
- package/core/instanceOf.d.ts +1 -1
- package/core/json.js +3 -3
- package/core/json.js.flow +3 -3
- package/core/json.mjs +3 -3
- package/core/object.d.ts +5 -0
- package/core/object.js +59 -2
- package/core/object.js.flow +77 -21
- package/core/object.mjs +56 -3
- package/core/string.d.ts +3 -0
- package/core/string.js +15 -3
- package/core/string.js.flow +15 -2
- package/core/string.mjs +12 -3
- package/core/tuple.d.ts +28 -28
- package/core/tuple.js +28 -151
- package/core/tuple.js.flow +31 -191
- package/core/tuple.mjs +27 -141
- package/index.d.ts +17 -18
- package/index.js +33 -43
- package/index.js.flow +17 -18
- package/index.mjs +8 -9
- package/package.json +1 -1
- package/result.d.ts +2 -2
- package/result.js +9 -9
- package/result.js.flow +11 -11
- package/result.mjs +9 -9
- package/core/mapping.d.ts +0 -6
- package/core/mapping.js +0 -67
- package/core/mapping.js.flow +0 -62
- package/core/mapping.mjs +0 -58
package/index.mjs
CHANGED
|
@@ -17,21 +17,20 @@
|
|
|
17
17
|
*
|
|
18
18
|
*/
|
|
19
19
|
export { guard } from './_guard.mjs';
|
|
20
|
-
export { compose,
|
|
21
|
-
export { array, nonEmptyArray, poja } from './core/array.mjs';
|
|
20
|
+
export { compose, predicate, prep, transform } from './core/composition.mjs';
|
|
21
|
+
export { array, nonEmptyArray, poja, set } from './core/array.mjs';
|
|
22
22
|
export { boolean, numericBoolean, truthy } from './core/boolean.mjs';
|
|
23
23
|
export { constant, hardcoded, mixed, null_, undefined_, unknown } from './core/constants.mjs';
|
|
24
24
|
export { date, iso8601 } from './core/date.mjs';
|
|
25
25
|
export { describe } from './core/describe.mjs';
|
|
26
|
-
export {
|
|
27
|
-
export { either,
|
|
26
|
+
export { dict, exact, inexact, mapping, object, pojo } from './core/object.mjs';
|
|
27
|
+
export { either, oneOf } from './core/either.mjs';
|
|
28
|
+
export { email, httpsUrl, nonEmptyString, regex, string, url, uuid, uuidv1, uuidv4 } from './core/string.mjs';
|
|
28
29
|
export { fail } from './core/fail.mjs';
|
|
29
30
|
export { instanceOf } from './core/instanceOf.mjs';
|
|
31
|
+
export { integer, number, positiveInteger, positiveNumber } from './core/number.mjs';
|
|
30
32
|
export { json, jsonObject, jsonArray } from './core/json.mjs';
|
|
31
33
|
export { lazy } from './core/lazy.mjs';
|
|
32
|
-
export { mapping, dict } from './core/mapping.mjs';
|
|
33
|
-
export { integer, number, positiveInteger, positiveNumber } from './core/number.mjs';
|
|
34
|
-
export { exact, inexact, object, pojo } from './core/object.mjs';
|
|
35
34
|
export { maybe, nullable, optional } from './core/optional.mjs';
|
|
36
|
-
export {
|
|
37
|
-
export {
|
|
35
|
+
export { taggedUnion } from './core/dispatch.mjs';
|
|
36
|
+
export { tuple } from './core/tuple.mjs';
|
package/package.json
CHANGED
package/result.d.ts
CHANGED
package/result.js
CHANGED
|
@@ -22,7 +22,7 @@ exports.unwrap = unwrap;
|
|
|
22
22
|
*/
|
|
23
23
|
function ok(value) {
|
|
24
24
|
return {
|
|
25
|
-
|
|
25
|
+
ok: true,
|
|
26
26
|
value: value,
|
|
27
27
|
error: undefined
|
|
28
28
|
};
|
|
@@ -34,7 +34,7 @@ function ok(value) {
|
|
|
34
34
|
|
|
35
35
|
function err(error) {
|
|
36
36
|
return {
|
|
37
|
-
|
|
37
|
+
ok: false,
|
|
38
38
|
value: undefined,
|
|
39
39
|
error: error
|
|
40
40
|
};
|
|
@@ -46,7 +46,7 @@ function err(error) {
|
|
|
46
46
|
|
|
47
47
|
|
|
48
48
|
function unwrap(result) {
|
|
49
|
-
if (result.
|
|
49
|
+
if (result.ok) {
|
|
50
50
|
return result.value;
|
|
51
51
|
} else {
|
|
52
52
|
throw result.error;
|
|
@@ -54,7 +54,7 @@ function unwrap(result) {
|
|
|
54
54
|
}
|
|
55
55
|
|
|
56
56
|
function expect(result, message) {
|
|
57
|
-
if (result.
|
|
57
|
+
if (result.ok) {
|
|
58
58
|
return result.value;
|
|
59
59
|
} else {
|
|
60
60
|
throw message instanceof Error ? message : new Error(message);
|
|
@@ -62,7 +62,7 @@ function expect(result, message) {
|
|
|
62
62
|
}
|
|
63
63
|
|
|
64
64
|
function dispatch(result, okCallback, errCallback) {
|
|
65
|
-
return result.
|
|
65
|
+
return result.ok ? okCallback(result.value) : errCallback(result.error);
|
|
66
66
|
}
|
|
67
67
|
/**
|
|
68
68
|
* Like .and(), aka &&, but the second argument gets evaluated lazily only if
|
|
@@ -72,7 +72,7 @@ function dispatch(result, okCallback, errCallback) {
|
|
|
72
72
|
|
|
73
73
|
|
|
74
74
|
function andThen(result1, lazyResult2) {
|
|
75
|
-
return result1.
|
|
75
|
+
return result1.ok ? lazyResult2(result1.value) : result1;
|
|
76
76
|
}
|
|
77
77
|
/**
|
|
78
78
|
* Like .or(), aka ||, but the second argument gets evaluated lazily only if
|
|
@@ -82,7 +82,7 @@ function andThen(result1, lazyResult2) {
|
|
|
82
82
|
|
|
83
83
|
|
|
84
84
|
function orElse(result1, lazyResult2) {
|
|
85
|
-
return result1.
|
|
85
|
+
return result1.ok ? result1 : lazyResult2(result1.error);
|
|
86
86
|
}
|
|
87
87
|
/**
|
|
88
88
|
* Transform an Ok result. Will not touch Err results.
|
|
@@ -90,7 +90,7 @@ function orElse(result1, lazyResult2) {
|
|
|
90
90
|
|
|
91
91
|
|
|
92
92
|
function mapOk(result, mapper) {
|
|
93
|
-
return result.
|
|
93
|
+
return result.ok ? ok(mapper(result.value)) : result;
|
|
94
94
|
}
|
|
95
95
|
/**
|
|
96
96
|
* Transform an Err value. Will not touch Ok results.
|
|
@@ -98,5 +98,5 @@ function mapOk(result, mapper) {
|
|
|
98
98
|
|
|
99
99
|
|
|
100
100
|
function mapError(result, mapper) {
|
|
101
|
-
return result.
|
|
101
|
+
return result.ok ? result : err(mapper(result.error));
|
|
102
102
|
}
|
package/result.js.flow
CHANGED
|
@@ -6,8 +6,8 @@
|
|
|
6
6
|
* | Err <error>
|
|
7
7
|
*/
|
|
8
8
|
|
|
9
|
-
type Ok<+T> = {| +
|
|
10
|
-
type Err<+E> = {| +
|
|
9
|
+
type Ok<+T> = {| +ok: true, +value: T, +error: void |};
|
|
10
|
+
type Err<+E> = {| +ok: false, +value: void, +error: E |};
|
|
11
11
|
|
|
12
12
|
export type Result<+T, +E> = Ok<T> | Err<E>;
|
|
13
13
|
|
|
@@ -15,14 +15,14 @@ export type Result<+T, +E> = Ok<T> | Err<E>;
|
|
|
15
15
|
* Create a new Result instance representing a successful computation.
|
|
16
16
|
*/
|
|
17
17
|
export function ok<T>(value: T): Ok<T> {
|
|
18
|
-
return {
|
|
18
|
+
return { ok: true, value, error: undefined };
|
|
19
19
|
}
|
|
20
20
|
|
|
21
21
|
/**
|
|
22
22
|
* Create a new Result instance representing a failed computation.
|
|
23
23
|
*/
|
|
24
24
|
export function err<E>(error: E): Err<E> {
|
|
25
|
-
return {
|
|
25
|
+
return { ok: false, value: undefined, error };
|
|
26
26
|
}
|
|
27
27
|
|
|
28
28
|
/**
|
|
@@ -30,7 +30,7 @@ export function err<E>(error: E): Err<E> {
|
|
|
30
30
|
* Otherwise, will throw the "Err" error via a runtime exception.
|
|
31
31
|
*/
|
|
32
32
|
export function unwrap<T>(result: Result<T, mixed>): T {
|
|
33
|
-
if (result.
|
|
33
|
+
if (result.ok) {
|
|
34
34
|
return result.value;
|
|
35
35
|
} else {
|
|
36
36
|
throw result.error;
|
|
@@ -38,7 +38,7 @@ export function unwrap<T>(result: Result<T, mixed>): T {
|
|
|
38
38
|
}
|
|
39
39
|
|
|
40
40
|
export function expect<T>(result: Result<T, mixed>, message: string | Error): T {
|
|
41
|
-
if (result.
|
|
41
|
+
if (result.ok) {
|
|
42
42
|
return result.value;
|
|
43
43
|
} else {
|
|
44
44
|
throw message instanceof Error ? message : new Error(message);
|
|
@@ -50,7 +50,7 @@ export function dispatch<T, E, O>(
|
|
|
50
50
|
okCallback: (value: T) => O,
|
|
51
51
|
errCallback: (error: E) => O,
|
|
52
52
|
): O {
|
|
53
|
-
return result.
|
|
53
|
+
return result.ok ? okCallback(result.value) : errCallback(result.error);
|
|
54
54
|
}
|
|
55
55
|
|
|
56
56
|
/**
|
|
@@ -62,7 +62,7 @@ export function andThen<T, E, T2>(
|
|
|
62
62
|
result1: Result<T, E>,
|
|
63
63
|
lazyResult2: (value: T) => Result<T2, E>,
|
|
64
64
|
): Result<T2, E> {
|
|
65
|
-
return result1.
|
|
65
|
+
return result1.ok ? lazyResult2(result1.value) : result1;
|
|
66
66
|
}
|
|
67
67
|
|
|
68
68
|
/**
|
|
@@ -74,7 +74,7 @@ export function orElse<T, E, E2>(
|
|
|
74
74
|
result1: Result<T, E>,
|
|
75
75
|
lazyResult2: (errValue: E) => Result<T, E2>,
|
|
76
76
|
): Result<T, E2> {
|
|
77
|
-
return result1.
|
|
77
|
+
return result1.ok ? result1 : lazyResult2(result1.error);
|
|
78
78
|
}
|
|
79
79
|
|
|
80
80
|
/**
|
|
@@ -84,7 +84,7 @@ export function mapOk<T, E, T2>(
|
|
|
84
84
|
result: Result<T, E>,
|
|
85
85
|
mapper: (value: T) => T2,
|
|
86
86
|
): Result<T2, E> {
|
|
87
|
-
return result.
|
|
87
|
+
return result.ok ? ok(mapper(result.value)) : result;
|
|
88
88
|
}
|
|
89
89
|
|
|
90
90
|
/**
|
|
@@ -94,5 +94,5 @@ export function mapError<T, E, E2>(
|
|
|
94
94
|
result: Result<T, E>,
|
|
95
95
|
mapper: (error: E) => E2,
|
|
96
96
|
): Result<T, E2> {
|
|
97
|
-
return result.
|
|
97
|
+
return result.ok ? result : err(mapper(result.error));
|
|
98
98
|
}
|
package/result.mjs
CHANGED
|
@@ -9,7 +9,7 @@
|
|
|
9
9
|
*/
|
|
10
10
|
export function ok(value) {
|
|
11
11
|
return {
|
|
12
|
-
|
|
12
|
+
ok: true,
|
|
13
13
|
value: value,
|
|
14
14
|
error: undefined
|
|
15
15
|
};
|
|
@@ -20,7 +20,7 @@ export function ok(value) {
|
|
|
20
20
|
|
|
21
21
|
export function err(error) {
|
|
22
22
|
return {
|
|
23
|
-
|
|
23
|
+
ok: false,
|
|
24
24
|
value: undefined,
|
|
25
25
|
error: error
|
|
26
26
|
};
|
|
@@ -31,21 +31,21 @@ export function err(error) {
|
|
|
31
31
|
*/
|
|
32
32
|
|
|
33
33
|
export function unwrap(result) {
|
|
34
|
-
if (result.
|
|
34
|
+
if (result.ok) {
|
|
35
35
|
return result.value;
|
|
36
36
|
} else {
|
|
37
37
|
throw result.error;
|
|
38
38
|
}
|
|
39
39
|
}
|
|
40
40
|
export function expect(result, message) {
|
|
41
|
-
if (result.
|
|
41
|
+
if (result.ok) {
|
|
42
42
|
return result.value;
|
|
43
43
|
} else {
|
|
44
44
|
throw message instanceof Error ? message : new Error(message);
|
|
45
45
|
}
|
|
46
46
|
}
|
|
47
47
|
export function dispatch(result, okCallback, errCallback) {
|
|
48
|
-
return result.
|
|
48
|
+
return result.ok ? okCallback(result.value) : errCallback(result.error);
|
|
49
49
|
}
|
|
50
50
|
/**
|
|
51
51
|
* Like .and(), aka &&, but the second argument gets evaluated lazily only if
|
|
@@ -54,7 +54,7 @@ export function dispatch(result, okCallback, errCallback) {
|
|
|
54
54
|
*/
|
|
55
55
|
|
|
56
56
|
export function andThen(result1, lazyResult2) {
|
|
57
|
-
return result1.
|
|
57
|
+
return result1.ok ? lazyResult2(result1.value) : result1;
|
|
58
58
|
}
|
|
59
59
|
/**
|
|
60
60
|
* Like .or(), aka ||, but the second argument gets evaluated lazily only if
|
|
@@ -63,19 +63,19 @@ export function andThen(result1, lazyResult2) {
|
|
|
63
63
|
*/
|
|
64
64
|
|
|
65
65
|
export function orElse(result1, lazyResult2) {
|
|
66
|
-
return result1.
|
|
66
|
+
return result1.ok ? result1 : lazyResult2(result1.error);
|
|
67
67
|
}
|
|
68
68
|
/**
|
|
69
69
|
* Transform an Ok result. Will not touch Err results.
|
|
70
70
|
*/
|
|
71
71
|
|
|
72
72
|
export function mapOk(result, mapper) {
|
|
73
|
-
return result.
|
|
73
|
+
return result.ok ? ok(mapper(result.value)) : result;
|
|
74
74
|
}
|
|
75
75
|
/**
|
|
76
76
|
* Transform an Err value. Will not touch Ok results.
|
|
77
77
|
*/
|
|
78
78
|
|
|
79
79
|
export function mapError(result, mapper) {
|
|
80
|
-
return result.
|
|
80
|
+
return result.ok ? result : err(mapper(result.error));
|
|
81
81
|
}
|
package/core/mapping.d.ts
DELETED
package/core/mapping.js
DELETED
|
@@ -1,67 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
|
|
3
|
-
exports.__esModule = true;
|
|
4
|
-
exports.dict = dict;
|
|
5
|
-
exports.mapping = mapping;
|
|
6
|
-
|
|
7
|
-
var _annotate = require("../annotate");
|
|
8
|
-
|
|
9
|
-
var _composition = require("./composition");
|
|
10
|
-
|
|
11
|
-
var _result = require("../result");
|
|
12
|
-
|
|
13
|
-
var _object = require("./object");
|
|
14
|
-
|
|
15
|
-
/**
|
|
16
|
-
* Like mapping(), but returns an object rather than a Map instance.
|
|
17
|
-
*/
|
|
18
|
-
function dict(decoder) {
|
|
19
|
-
return (0, _composition.compose)(_object.pojo, function (blob) {
|
|
20
|
-
var rv = {};
|
|
21
|
-
var errors = null;
|
|
22
|
-
Object.keys(blob).forEach(function (key) {
|
|
23
|
-
var value = blob[key];
|
|
24
|
-
var result = decoder(value);
|
|
25
|
-
|
|
26
|
-
if (result.type === 'ok') {
|
|
27
|
-
if (errors === null) {
|
|
28
|
-
rv[key] = result.value;
|
|
29
|
-
}
|
|
30
|
-
} else {
|
|
31
|
-
rv = {}; // Clear the success value so it can get garbage collected early
|
|
32
|
-
|
|
33
|
-
if (errors === null) {
|
|
34
|
-
errors = {};
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
errors[key] = result.error;
|
|
38
|
-
}
|
|
39
|
-
});
|
|
40
|
-
|
|
41
|
-
if (errors !== null) {
|
|
42
|
-
return (0, _result.err)((0, _annotate.merge)((0, _annotate.annotateObject)(blob), errors));
|
|
43
|
-
} else {
|
|
44
|
-
return (0, _result.ok)(rv);
|
|
45
|
-
}
|
|
46
|
-
});
|
|
47
|
-
}
|
|
48
|
-
/**
|
|
49
|
-
* Given an object, will decode a Map of string keys to whatever values.
|
|
50
|
-
*
|
|
51
|
-
* For example, given a decoder for a Person, we can verify a Person lookup
|
|
52
|
-
* table structure (of type Map<string, Person>) like so:
|
|
53
|
-
*
|
|
54
|
-
* mapping(person)
|
|
55
|
-
*
|
|
56
|
-
*/
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
function mapping(decoder) {
|
|
60
|
-
return (0, _composition.map)(dict(decoder), function (obj) {
|
|
61
|
-
return new Map( // This is effectively Object.entries(obj), but in a way that Flow
|
|
62
|
-
// will know the types are okay
|
|
63
|
-
Object.keys(obj).map(function (key) {
|
|
64
|
-
return [key, obj[key]];
|
|
65
|
-
}));
|
|
66
|
-
});
|
|
67
|
-
}
|
package/core/mapping.js.flow
DELETED
|
@@ -1,62 +0,0 @@
|
|
|
1
|
-
// @flow strict
|
|
2
|
-
|
|
3
|
-
import { annotateObject } from '../annotate';
|
|
4
|
-
import { compose, map } from './composition';
|
|
5
|
-
import { err, ok } from '../result';
|
|
6
|
-
import { merge } from '../annotate';
|
|
7
|
-
import { pojo } from './object';
|
|
8
|
-
import type { Annotation } from '../annotate';
|
|
9
|
-
import type { Decoder } from '../_types';
|
|
10
|
-
|
|
11
|
-
/**
|
|
12
|
-
* Like mapping(), but returns an object rather than a Map instance.
|
|
13
|
-
*/
|
|
14
|
-
export function dict<T>(decoder: Decoder<T>): Decoder<{ [string]: T }> {
|
|
15
|
-
return compose(pojo, (blob: { +[key: string]: mixed }) => {
|
|
16
|
-
let rv: { [key: string]: T } = {};
|
|
17
|
-
let errors: { [key: string]: Annotation } | null = null;
|
|
18
|
-
|
|
19
|
-
Object.keys(blob).forEach((key: string) => {
|
|
20
|
-
const value = blob[key];
|
|
21
|
-
const result = decoder(value);
|
|
22
|
-
if (result.type === 'ok') {
|
|
23
|
-
if (errors === null) {
|
|
24
|
-
rv[key] = result.value;
|
|
25
|
-
}
|
|
26
|
-
} else {
|
|
27
|
-
rv = {}; // Clear the success value so it can get garbage collected early
|
|
28
|
-
if (errors === null) {
|
|
29
|
-
errors = {};
|
|
30
|
-
}
|
|
31
|
-
errors[key] = result.error;
|
|
32
|
-
}
|
|
33
|
-
});
|
|
34
|
-
|
|
35
|
-
if (errors !== null) {
|
|
36
|
-
return err(merge(annotateObject(blob), errors));
|
|
37
|
-
} else {
|
|
38
|
-
return ok(rv);
|
|
39
|
-
}
|
|
40
|
-
});
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
/**
|
|
44
|
-
* Given an object, will decode a Map of string keys to whatever values.
|
|
45
|
-
*
|
|
46
|
-
* For example, given a decoder for a Person, we can verify a Person lookup
|
|
47
|
-
* table structure (of type Map<string, Person>) like so:
|
|
48
|
-
*
|
|
49
|
-
* mapping(person)
|
|
50
|
-
*
|
|
51
|
-
*/
|
|
52
|
-
export function mapping<T>(decoder: Decoder<T>): Decoder<Map<string, T>> {
|
|
53
|
-
return map(
|
|
54
|
-
dict(decoder),
|
|
55
|
-
(obj) =>
|
|
56
|
-
new Map(
|
|
57
|
-
// This is effectively Object.entries(obj), but in a way that Flow
|
|
58
|
-
// will know the types are okay
|
|
59
|
-
Object.keys(obj).map((key) => [key, obj[key]]),
|
|
60
|
-
),
|
|
61
|
-
);
|
|
62
|
-
}
|
package/core/mapping.mjs
DELETED
|
@@ -1,58 +0,0 @@
|
|
|
1
|
-
import { annotateObject } from '../annotate.mjs';
|
|
2
|
-
import { compose, map } from './composition.mjs';
|
|
3
|
-
import { err, ok } from '../result.mjs';
|
|
4
|
-
import { merge } from '../annotate.mjs';
|
|
5
|
-
import { pojo } from './object.mjs';
|
|
6
|
-
|
|
7
|
-
/**
|
|
8
|
-
* Like mapping(), but returns an object rather than a Map instance.
|
|
9
|
-
*/
|
|
10
|
-
export function dict(decoder) {
|
|
11
|
-
return compose(pojo, function (blob) {
|
|
12
|
-
var rv = {};
|
|
13
|
-
var errors = null;
|
|
14
|
-
Object.keys(blob).forEach(function (key) {
|
|
15
|
-
var value = blob[key];
|
|
16
|
-
var result = decoder(value);
|
|
17
|
-
|
|
18
|
-
if (result.type === 'ok') {
|
|
19
|
-
if (errors === null) {
|
|
20
|
-
rv[key] = result.value;
|
|
21
|
-
}
|
|
22
|
-
} else {
|
|
23
|
-
rv = {}; // Clear the success value so it can get garbage collected early
|
|
24
|
-
|
|
25
|
-
if (errors === null) {
|
|
26
|
-
errors = {};
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
errors[key] = result.error;
|
|
30
|
-
}
|
|
31
|
-
});
|
|
32
|
-
|
|
33
|
-
if (errors !== null) {
|
|
34
|
-
return err(merge(annotateObject(blob), errors));
|
|
35
|
-
} else {
|
|
36
|
-
return ok(rv);
|
|
37
|
-
}
|
|
38
|
-
});
|
|
39
|
-
}
|
|
40
|
-
/**
|
|
41
|
-
* Given an object, will decode a Map of string keys to whatever values.
|
|
42
|
-
*
|
|
43
|
-
* For example, given a decoder for a Person, we can verify a Person lookup
|
|
44
|
-
* table structure (of type Map<string, Person>) like so:
|
|
45
|
-
*
|
|
46
|
-
* mapping(person)
|
|
47
|
-
*
|
|
48
|
-
*/
|
|
49
|
-
|
|
50
|
-
export function mapping(decoder) {
|
|
51
|
-
return map(dict(decoder), function (obj) {
|
|
52
|
-
return new Map( // This is effectively Object.entries(obj), but in a way that Flow
|
|
53
|
-
// will know the types are okay
|
|
54
|
-
Object.keys(obj).map(function (key) {
|
|
55
|
-
return [key, obj[key]];
|
|
56
|
-
}));
|
|
57
|
-
});
|
|
58
|
-
}
|