decoders 1.25.4 → 2.0.0-beta1
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 +30 -3
- package/cjs/_guard.js +26 -0
- package/cjs/_guard.js.flow +20 -0
- package/cjs/_types.js +1 -0
- package/cjs/_types.js.flow +20 -0
- package/cjs/_utils.js +108 -0
- package/cjs/_utils.js.flow +97 -0
- package/cjs/annotate.js +161 -0
- package/cjs/annotate.js.flow +218 -0
- package/cjs/format/index.js +12 -0
- package/cjs/format/index.js.flow +4 -0
- package/cjs/format/inline.js +146 -0
- package/cjs/format/inline.js.flow +122 -0
- package/cjs/format/short.js +10 -0
- package/cjs/format/short.js.flow +8 -0
- package/cjs/index.js +120 -0
- package/{index.js.flow → cjs/index.js.flow} +31 -24
- package/cjs/result.js +172 -0
- package/cjs/result.js.flow +166 -0
- package/cjs/stdlib/array.js +108 -0
- package/{array.js.flow → cjs/stdlib/array.js.flow} +22 -25
- package/cjs/stdlib/boolean.js +44 -0
- package/{boolean.js.flow → cjs/stdlib/boolean.js.flow} +8 -7
- package/cjs/stdlib/composition.js +56 -0
- package/{utils.js.flow → cjs/stdlib/composition.js.flow} +7 -22
- package/cjs/stdlib/constants.js +69 -0
- package/{constants.js.flow → cjs/stdlib/constants.js.flow} +13 -12
- package/cjs/stdlib/date.js +46 -0
- package/{date.js.flow → cjs/stdlib/date.js.flow} +9 -7
- package/cjs/stdlib/describe.js +26 -0
- package/{describe.js.flow → cjs/stdlib/describe.js.flow} +4 -4
- package/cjs/stdlib/dispatch.js +62 -0
- package/{dispatch.js.flow → cjs/stdlib/dispatch.js.flow} +6 -5
- package/cjs/stdlib/either.js +117 -0
- package/{either.js.flow → cjs/stdlib/either.js.flow} +55 -35
- package/cjs/stdlib/fail.js +21 -0
- package/cjs/stdlib/fail.js.flow +12 -0
- package/cjs/stdlib/instanceOf.js +19 -0
- package/{instanceOf.js.flow → cjs/stdlib/instanceOf.js.flow} +7 -8
- package/{json.js → cjs/stdlib/json.js} +3 -5
- package/{json.js.flow → cjs/stdlib/json.js.flow} +4 -4
- package/{lazy.js → cjs/stdlib/lazy.js} +1 -3
- package/{lazy.js.flow → cjs/stdlib/lazy.js.flow} +1 -1
- package/cjs/stdlib/mapping.js +67 -0
- package/cjs/stdlib/mapping.js.flow +54 -0
- package/cjs/stdlib/number.js +40 -0
- package/{number.js.flow → cjs/stdlib/number.js.flow} +10 -11
- package/cjs/stdlib/object.js +194 -0
- package/{object.js.flow → cjs/stdlib/object.js.flow} +44 -52
- package/cjs/stdlib/optional.js +54 -0
- package/{optional.js.flow → cjs/stdlib/optional.js.flow} +6 -7
- package/cjs/stdlib/string.js +98 -0
- package/{string.js.flow → cjs/stdlib/string.js.flow} +13 -12
- package/cjs/stdlib/tuple.js +173 -0
- package/{tuple.js.flow → cjs/stdlib/tuple.js.flow} +62 -63
- package/es/_guard.js +15 -0
- package/es/_types.js +0 -0
- package/es/_utils.js +93 -0
- package/es/annotate.js +144 -0
- package/es/format/index.js +2 -0
- package/es/format/inline.js +137 -0
- package/es/format/short.js +4 -0
- package/es/index.js +37 -0
- package/es/result.js +139 -0
- package/es/stdlib/array.js +91 -0
- package/es/stdlib/boolean.js +28 -0
- package/es/stdlib/composition.js +42 -0
- package/es/stdlib/constants.js +46 -0
- package/es/stdlib/date.js +28 -0
- package/{describe.js → es/stdlib/describe.js} +5 -11
- package/{dispatch.js → es/stdlib/dispatch.js} +8 -15
- package/es/stdlib/either.js +90 -0
- package/es/stdlib/fail.js +11 -0
- package/es/stdlib/instanceOf.js +8 -0
- package/es/stdlib/json.js +15 -0
- package/es/stdlib/lazy.js +11 -0
- package/es/stdlib/mapping.js +54 -0
- package/es/stdlib/number.js +25 -0
- package/es/stdlib/object.js +175 -0
- package/es/stdlib/optional.js +38 -0
- package/{string.js → es/stdlib/string.js} +18 -35
- package/es/stdlib/tuple.js +155 -0
- package/package.json +12 -7
- package/ts/_guard.d.ts +7 -0
- package/ts/_helpers.d.ts +79 -0
- package/{types.d.ts → ts/_types.d.ts} +3 -5
- package/{utils.d.ts → ts/_utils.d.ts} +3 -3
- package/ts/annotate.d.ts +58 -0
- package/{array.d.ts → ts/array.d.ts} +1 -1
- package/{boolean.d.ts → ts/boolean.d.ts} +1 -1
- package/{constants.d.ts → ts/constants.d.ts} +1 -1
- package/{date.d.ts → ts/date.d.ts} +1 -1
- package/{describe.d.ts → ts/describe.d.ts} +1 -1
- package/{dispatch.d.ts → ts/dispatch.d.ts} +3 -3
- package/{either.d.ts → ts/either.d.ts} +8 -8
- package/{fail.d.ts → ts/fail.d.ts} +1 -1
- package/ts/index.d.ts +42 -0
- package/ts/inline.d.ts +3 -0
- package/{instanceOf.d.ts → ts/instanceOf.d.ts} +1 -1
- package/{json.d.ts → ts/json.d.ts} +1 -1
- package/{lazy.d.ts → ts/lazy.d.ts} +1 -1
- package/{mapping.d.ts → ts/mapping.d.ts} +1 -1
- package/{number.d.ts → ts/number.d.ts} +1 -1
- package/{object.d.ts → ts/object.d.ts} +8 -8
- package/{optional.d.ts → ts/optional.d.ts} +1 -1
- package/ts/result.d.ts +39 -0
- package/ts/short.d.ts +3 -0
- package/{string.d.ts → ts/string.d.ts} +1 -1
- package/{tuple.d.ts → ts/tuple.d.ts} +5 -5
- package/array.js +0 -133
- package/boolean.js +0 -42
- package/constants.js +0 -67
- package/date.js +0 -42
- package/either.js +0 -85
- package/fail.js +0 -19
- package/fail.js.flow +0 -13
- package/guard.d.ts +0 -7
- package/guard.js +0 -30
- package/guard.js.flow +0 -36
- package/helpers.d.ts +0 -62
- package/index.d.ts +0 -38
- package/index.js +0 -397
- package/instanceOf.js +0 -17
- package/mapping.js +0 -113
- package/mapping.js.flow +0 -71
- package/number.js +0 -38
- package/object.js +0 -254
- package/optional.js +0 -52
- package/tuple.js +0 -199
- package/types.js +0 -5
- package/types.js.flow +0 -26
- package/utils.js +0 -70
|
@@ -1,16 +1,15 @@
|
|
|
1
1
|
// @flow strict
|
|
2
2
|
|
|
3
|
-
import { annotate } from '
|
|
4
|
-
import {
|
|
5
|
-
|
|
3
|
+
import { annotate } from '../annotate';
|
|
4
|
+
import { compose, predicate } from './composition';
|
|
5
|
+
import { err, errValue, isErr, ok, unwrap, value } from '../result';
|
|
6
6
|
import { poja } from './array';
|
|
7
|
-
import type { Decoder } from '
|
|
8
|
-
import { compose, predicate } from './utils';
|
|
7
|
+
import type { Decoder } from '../_types';
|
|
9
8
|
|
|
10
9
|
const ntuple = (n: number) =>
|
|
11
10
|
compose(
|
|
12
11
|
poja,
|
|
13
|
-
predicate((arr) => arr.length === n, `Must be a ${n}-tuple`)
|
|
12
|
+
predicate((arr) => arr.length === n, `Must be a ${n}-tuple`),
|
|
14
13
|
);
|
|
15
14
|
|
|
16
15
|
/**
|
|
@@ -23,11 +22,11 @@ export function tuple1<T>(decoder1: Decoder<T>): Decoder<[T]> {
|
|
|
23
22
|
|
|
24
23
|
const result1 = decoder1(blob1);
|
|
25
24
|
try {
|
|
26
|
-
return
|
|
25
|
+
return ok([unwrap(result1)]);
|
|
27
26
|
} catch (e) {
|
|
28
27
|
// If a decoder error has happened while unwrapping all the
|
|
29
28
|
// results, try to construct a good error message
|
|
30
|
-
return
|
|
29
|
+
return err(annotate(errValue(result1)));
|
|
31
30
|
}
|
|
32
31
|
});
|
|
33
32
|
}
|
|
@@ -38,7 +37,7 @@ export function tuple1<T>(decoder1: Decoder<T>): Decoder<[T]> {
|
|
|
38
37
|
*/
|
|
39
38
|
export function tuple2<T1, T2>(
|
|
40
39
|
decoder1: Decoder<T1>,
|
|
41
|
-
decoder2: Decoder<T2
|
|
40
|
+
decoder2: Decoder<T2>,
|
|
42
41
|
): Decoder<[T1, T2]> {
|
|
43
42
|
return compose(ntuple(2), (blobs: $ReadOnlyArray<mixed>) => {
|
|
44
43
|
const [blob1, blob2] = blobs;
|
|
@@ -46,15 +45,15 @@ export function tuple2<T1, T2>(
|
|
|
46
45
|
const result1 = decoder1(blob1);
|
|
47
46
|
const result2 = decoder2(blob2);
|
|
48
47
|
try {
|
|
49
|
-
return
|
|
48
|
+
return ok([unwrap(result1), unwrap(result2)]);
|
|
50
49
|
} catch (e) {
|
|
51
50
|
// If a decoder error has happened while unwrapping all the
|
|
52
51
|
// results, try to construct a good error message
|
|
53
|
-
return
|
|
52
|
+
return err(
|
|
54
53
|
annotate([
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
])
|
|
54
|
+
isErr(result1) ? errValue(result1) : value(result1),
|
|
55
|
+
isErr(result2) ? errValue(result2) : value(result2),
|
|
56
|
+
]),
|
|
58
57
|
);
|
|
59
58
|
}
|
|
60
59
|
});
|
|
@@ -67,7 +66,7 @@ export function tuple2<T1, T2>(
|
|
|
67
66
|
export function tuple3<T1, T2, T3>(
|
|
68
67
|
decoder1: Decoder<T1>,
|
|
69
68
|
decoder2: Decoder<T2>,
|
|
70
|
-
decoder3: Decoder<T3
|
|
69
|
+
decoder3: Decoder<T3>,
|
|
71
70
|
): Decoder<[T1, T2, T3]> {
|
|
72
71
|
return compose(ntuple(3), (blobs: $ReadOnlyArray<mixed>) => {
|
|
73
72
|
const [blob1, blob2, blob3] = blobs;
|
|
@@ -76,16 +75,16 @@ export function tuple3<T1, T2, T3>(
|
|
|
76
75
|
const result2 = decoder2(blob2);
|
|
77
76
|
const result3 = decoder3(blob3);
|
|
78
77
|
try {
|
|
79
|
-
return
|
|
78
|
+
return ok([unwrap(result1), unwrap(result2), unwrap(result3)]);
|
|
80
79
|
} catch (e) {
|
|
81
80
|
// If a decoder error has happened while unwrapping all the
|
|
82
81
|
// results, try to construct a good error message
|
|
83
|
-
return
|
|
82
|
+
return err(
|
|
84
83
|
annotate([
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
])
|
|
84
|
+
isErr(result1) ? errValue(result1) : value(result1),
|
|
85
|
+
isErr(result2) ? errValue(result2) : value(result2),
|
|
86
|
+
isErr(result3) ? errValue(result3) : value(result3),
|
|
87
|
+
]),
|
|
89
88
|
);
|
|
90
89
|
}
|
|
91
90
|
});
|
|
@@ -99,7 +98,7 @@ export function tuple4<T1, T2, T3, T4>(
|
|
|
99
98
|
decoder1: Decoder<T1>,
|
|
100
99
|
decoder2: Decoder<T2>,
|
|
101
100
|
decoder3: Decoder<T3>,
|
|
102
|
-
decoder4: Decoder<T4
|
|
101
|
+
decoder4: Decoder<T4>,
|
|
103
102
|
): Decoder<[T1, T2, T3, T4]> {
|
|
104
103
|
return compose(ntuple(4), (blobs: $ReadOnlyArray<mixed>) => {
|
|
105
104
|
const [blob1, blob2, blob3, blob4] = blobs;
|
|
@@ -109,22 +108,22 @@ export function tuple4<T1, T2, T3, T4>(
|
|
|
109
108
|
const result3 = decoder3(blob3);
|
|
110
109
|
const result4 = decoder4(blob4);
|
|
111
110
|
try {
|
|
112
|
-
return
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
111
|
+
return ok([
|
|
112
|
+
unwrap(result1),
|
|
113
|
+
unwrap(result2),
|
|
114
|
+
unwrap(result3),
|
|
115
|
+
unwrap(result4),
|
|
117
116
|
]);
|
|
118
117
|
} catch (e) {
|
|
119
118
|
// If a decoder error has happened while unwrapping all the
|
|
120
119
|
// results, try to construct a good error message
|
|
121
|
-
return
|
|
120
|
+
return err(
|
|
122
121
|
annotate([
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
])
|
|
122
|
+
isErr(result1) ? errValue(result1) : value(result1),
|
|
123
|
+
isErr(result2) ? errValue(result2) : value(result2),
|
|
124
|
+
isErr(result3) ? errValue(result3) : value(result3),
|
|
125
|
+
isErr(result4) ? errValue(result4) : value(result4),
|
|
126
|
+
]),
|
|
128
127
|
);
|
|
129
128
|
}
|
|
130
129
|
});
|
|
@@ -139,7 +138,7 @@ export function tuple5<T1, T2, T3, T4, T5>(
|
|
|
139
138
|
decoder2: Decoder<T2>,
|
|
140
139
|
decoder3: Decoder<T3>,
|
|
141
140
|
decoder4: Decoder<T4>,
|
|
142
|
-
decoder5: Decoder<T5
|
|
141
|
+
decoder5: Decoder<T5>,
|
|
143
142
|
): Decoder<[T1, T2, T3, T4, T5]> {
|
|
144
143
|
return compose(ntuple(5), (blobs: $ReadOnlyArray<mixed>) => {
|
|
145
144
|
const [blob1, blob2, blob3, blob4, blob5] = blobs;
|
|
@@ -150,24 +149,24 @@ export function tuple5<T1, T2, T3, T4, T5>(
|
|
|
150
149
|
const result4 = decoder4(blob4);
|
|
151
150
|
const result5 = decoder5(blob5);
|
|
152
151
|
try {
|
|
153
|
-
return
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
152
|
+
return ok([
|
|
153
|
+
unwrap(result1),
|
|
154
|
+
unwrap(result2),
|
|
155
|
+
unwrap(result3),
|
|
156
|
+
unwrap(result4),
|
|
157
|
+
unwrap(result5),
|
|
159
158
|
]);
|
|
160
159
|
} catch (e) {
|
|
161
160
|
// If a decoder error has happened while unwrapping all the
|
|
162
161
|
// results, try to construct a good error message
|
|
163
|
-
return
|
|
162
|
+
return err(
|
|
164
163
|
annotate([
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
])
|
|
164
|
+
isErr(result1) ? errValue(result1) : value(result1),
|
|
165
|
+
isErr(result2) ? errValue(result2) : value(result2),
|
|
166
|
+
isErr(result3) ? errValue(result3) : value(result3),
|
|
167
|
+
isErr(result4) ? errValue(result4) : value(result4),
|
|
168
|
+
isErr(result5) ? errValue(result5) : value(result5),
|
|
169
|
+
]),
|
|
171
170
|
);
|
|
172
171
|
}
|
|
173
172
|
});
|
|
@@ -183,7 +182,7 @@ export function tuple6<T1, T2, T3, T4, T5, T6>(
|
|
|
183
182
|
decoder3: Decoder<T3>,
|
|
184
183
|
decoder4: Decoder<T4>,
|
|
185
184
|
decoder5: Decoder<T5>,
|
|
186
|
-
decoder6: Decoder<T6
|
|
185
|
+
decoder6: Decoder<T6>,
|
|
187
186
|
): Decoder<[T1, T2, T3, T4, T5, T6]> {
|
|
188
187
|
return compose(ntuple(6), (blobs: $ReadOnlyArray<mixed>) => {
|
|
189
188
|
const [blob1, blob2, blob3, blob4, blob5, blob6] = blobs;
|
|
@@ -195,26 +194,26 @@ export function tuple6<T1, T2, T3, T4, T5, T6>(
|
|
|
195
194
|
const result5 = decoder5(blob5);
|
|
196
195
|
const result6 = decoder6(blob6);
|
|
197
196
|
try {
|
|
198
|
-
return
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
197
|
+
return ok([
|
|
198
|
+
unwrap(result1),
|
|
199
|
+
unwrap(result2),
|
|
200
|
+
unwrap(result3),
|
|
201
|
+
unwrap(result4),
|
|
202
|
+
unwrap(result5),
|
|
203
|
+
unwrap(result6),
|
|
205
204
|
]);
|
|
206
205
|
} catch (e) {
|
|
207
206
|
// If a decoder error has happened while unwrapping all the
|
|
208
207
|
// results, try to construct a good error message
|
|
209
|
-
return
|
|
208
|
+
return err(
|
|
210
209
|
annotate([
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
])
|
|
210
|
+
isErr(result1) ? errValue(result1) : value(result1),
|
|
211
|
+
isErr(result2) ? errValue(result2) : value(result2),
|
|
212
|
+
isErr(result3) ? errValue(result3) : value(result3),
|
|
213
|
+
isErr(result4) ? errValue(result4) : value(result4),
|
|
214
|
+
isErr(result5) ? errValue(result5) : value(result5),
|
|
215
|
+
isErr(result6) ? errValue(result6) : value(result6),
|
|
216
|
+
]),
|
|
218
217
|
);
|
|
219
218
|
}
|
|
220
219
|
});
|
package/es/_guard.js
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import * as Result from './result';
|
|
2
|
+
import { formatInline } from './format';
|
|
3
|
+
export function guard(decoder, formatter) {
|
|
4
|
+
if (formatter === void 0) {
|
|
5
|
+
formatter = formatInline;
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
return function (blob) {
|
|
9
|
+
return Result.unwrap(Result.mapError(decoder(blob), function (annotation) {
|
|
10
|
+
var err = new Error('\n' + formatter(annotation));
|
|
11
|
+
err.name = 'Decoding error';
|
|
12
|
+
return err;
|
|
13
|
+
}));
|
|
14
|
+
};
|
|
15
|
+
}
|
package/es/_types.js
ADDED
|
File without changes
|
package/es/_utils.js
ADDED
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
// $FlowFixMe[unclear-type] - deliberate casting
|
|
2
|
+
// Two spaces of indentation
|
|
3
|
+
export var INDENT = ' ';
|
|
4
|
+
/**
|
|
5
|
+
* `x instanceof Date` checks are unreliable across stack frames (that information
|
|
6
|
+
* might get lost by the JS runtime), so we'll have to reside to more runtime
|
|
7
|
+
* inspection checks.
|
|
8
|
+
*
|
|
9
|
+
* Taken from https://stackoverflow.com/a/44198641
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
export function isDate(value) {
|
|
13
|
+
return !!value && // $FlowFixMe[method-unbinding]
|
|
14
|
+
Object.prototype.toString.call(value) === '[object Date]' && !isNaN(value);
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* Is value is a valid Date instance, then return that. If not, then return
|
|
18
|
+
* null.
|
|
19
|
+
*/
|
|
20
|
+
|
|
21
|
+
export function asDate(value) {
|
|
22
|
+
return isDate(value) ? value : null;
|
|
23
|
+
}
|
|
24
|
+
export function isMultiline(s) {
|
|
25
|
+
return s.indexOf('\n') >= 0;
|
|
26
|
+
}
|
|
27
|
+
export function indent(s, prefix) {
|
|
28
|
+
if (prefix === void 0) {
|
|
29
|
+
prefix = INDENT;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
if (isMultiline(s)) {
|
|
33
|
+
return s.split('\n').map(function (line) {
|
|
34
|
+
return prefix + line;
|
|
35
|
+
}).join('\n');
|
|
36
|
+
} else {
|
|
37
|
+
return prefix + s;
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* Walks the annotation tree and emits the annotation's key path within the
|
|
42
|
+
* object tree, and the message as a series of messages (array of strings).
|
|
43
|
+
*/
|
|
44
|
+
|
|
45
|
+
export function summarize(ann, keypath) {
|
|
46
|
+
if (keypath === void 0) {
|
|
47
|
+
keypath = [];
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
var result = [];
|
|
51
|
+
|
|
52
|
+
if (ann.type === 'array') {
|
|
53
|
+
var items = ann.items;
|
|
54
|
+
var index = 0;
|
|
55
|
+
items.forEach(function (ann) {
|
|
56
|
+
summarize(ann, [].concat(keypath, [index++])).forEach(function (item) {
|
|
57
|
+
return (// Collect to results
|
|
58
|
+
result.push(item)
|
|
59
|
+
);
|
|
60
|
+
});
|
|
61
|
+
});
|
|
62
|
+
} else if (ann.type === 'object') {
|
|
63
|
+
var fields = ann.fields;
|
|
64
|
+
Object.keys(fields).forEach(function (key) {
|
|
65
|
+
var value = fields[key];
|
|
66
|
+
summarize(value, [].concat(keypath, [key])).forEach(function (item) {
|
|
67
|
+
return (// Collect to results
|
|
68
|
+
result.push(item)
|
|
69
|
+
);
|
|
70
|
+
});
|
|
71
|
+
});
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
var text = ann.text;
|
|
75
|
+
|
|
76
|
+
if (!text) {
|
|
77
|
+
return result;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
var prefix;
|
|
81
|
+
|
|
82
|
+
if (keypath.length === 0) {
|
|
83
|
+
prefix = '';
|
|
84
|
+
} else if (keypath.length === 1) {
|
|
85
|
+
prefix = typeof keypath[0] === 'number' ? "Value at index " + keypath[0] + ": " : "Value at key " + JSON.stringify(keypath[0]) + ": ";
|
|
86
|
+
} else {
|
|
87
|
+
prefix = "Value at keypath " + keypath.map(function (x) {
|
|
88
|
+
return x.toString();
|
|
89
|
+
}).join('.') + ": ";
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
return [].concat(result, [prefix + text]);
|
|
93
|
+
}
|
package/es/annotate.js
ADDED
|
@@ -0,0 +1,144 @@
|
|
|
1
|
+
function _extends() { _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }
|
|
2
|
+
|
|
3
|
+
var _register = new WeakSet();
|
|
4
|
+
|
|
5
|
+
function brand(ann) {
|
|
6
|
+
_register.add(ann);
|
|
7
|
+
|
|
8
|
+
return ann;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export function object(fields, text) {
|
|
12
|
+
return brand({
|
|
13
|
+
type: 'object',
|
|
14
|
+
fields: fields,
|
|
15
|
+
text: text
|
|
16
|
+
});
|
|
17
|
+
}
|
|
18
|
+
export function array(items, text) {
|
|
19
|
+
return brand({
|
|
20
|
+
type: 'array',
|
|
21
|
+
items: items,
|
|
22
|
+
text: text
|
|
23
|
+
});
|
|
24
|
+
}
|
|
25
|
+
export function func(text) {
|
|
26
|
+
return brand({
|
|
27
|
+
type: 'function',
|
|
28
|
+
text: text
|
|
29
|
+
});
|
|
30
|
+
}
|
|
31
|
+
export function unknown(value, text) {
|
|
32
|
+
return brand({
|
|
33
|
+
type: 'unknown',
|
|
34
|
+
value: value,
|
|
35
|
+
text: text
|
|
36
|
+
});
|
|
37
|
+
}
|
|
38
|
+
export function scalar(value, text) {
|
|
39
|
+
return brand({
|
|
40
|
+
type: 'scalar',
|
|
41
|
+
value: value,
|
|
42
|
+
text: text
|
|
43
|
+
});
|
|
44
|
+
}
|
|
45
|
+
export function circularRef(text) {
|
|
46
|
+
return brand({
|
|
47
|
+
type: 'circular-ref',
|
|
48
|
+
text: text
|
|
49
|
+
});
|
|
50
|
+
}
|
|
51
|
+
/**
|
|
52
|
+
* Given an existing Annotation, set the annotation's text to a new value.
|
|
53
|
+
*/
|
|
54
|
+
|
|
55
|
+
export function updateText(annotation, text) {
|
|
56
|
+
if (text !== undefined) {
|
|
57
|
+
return brand(_extends({}, annotation, {
|
|
58
|
+
text: text
|
|
59
|
+
}));
|
|
60
|
+
} else {
|
|
61
|
+
return annotation;
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
/**
|
|
65
|
+
* Given an existing ObjectAnnotation, merges new Annotations in there.
|
|
66
|
+
*/
|
|
67
|
+
|
|
68
|
+
export function merge(objAnnotation, fields) {
|
|
69
|
+
var newFields = _extends({}, objAnnotation.fields, fields);
|
|
70
|
+
|
|
71
|
+
return object(newFields, objAnnotation.text);
|
|
72
|
+
}
|
|
73
|
+
export function asAnnotation(thing) {
|
|
74
|
+
return typeof thing === 'object' && thing !== null && _register.has(thing) ? thing : undefined;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
function annotateArray(value, text, seen) {
|
|
78
|
+
seen.add(value);
|
|
79
|
+
var items = value.map(function (v) {
|
|
80
|
+
return annotate(v, undefined, seen);
|
|
81
|
+
});
|
|
82
|
+
return array(items, text);
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
function annotateObject(obj, text, seen) {
|
|
86
|
+
seen.add(obj);
|
|
87
|
+
var fields = {};
|
|
88
|
+
Object.keys(obj).forEach(function (key) {
|
|
89
|
+
var value = obj[key];
|
|
90
|
+
fields[key] = annotate(value, undefined, seen);
|
|
91
|
+
});
|
|
92
|
+
return object(fields, text);
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
function annotate(value, text, seen) {
|
|
96
|
+
if (value === null || value === undefined || typeof value === 'string' || typeof value === 'number' || typeof value === 'boolean' || typeof value === 'symbol' || typeof value.getMonth === 'function') {
|
|
97
|
+
return scalar(value, text);
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
var ann = asAnnotation(value);
|
|
101
|
+
|
|
102
|
+
if (ann) {
|
|
103
|
+
return updateText(ann, text);
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
if (Array.isArray(value)) {
|
|
107
|
+
// "Circular references" can only exist in objects or arrays
|
|
108
|
+
if (seen.has(value)) {
|
|
109
|
+
return circularRef(text);
|
|
110
|
+
} else {
|
|
111
|
+
return annotateArray(value, text, seen);
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
if (typeof value === 'object') {
|
|
116
|
+
// "Circular references" can only exist in objects or arrays
|
|
117
|
+
if (seen.has(value)) {
|
|
118
|
+
return circularRef(text);
|
|
119
|
+
} else {
|
|
120
|
+
return annotateObject(value, text, seen);
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
if (typeof value === 'function') {
|
|
125
|
+
return func(text);
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
return unknown(value, text);
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
function public_annotate(value, text) {
|
|
132
|
+
return annotate(value, text, new WeakSet());
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
function public_annotateObject(obj, text) {
|
|
136
|
+
return annotateObject(obj, text, new WeakSet());
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
export { // This construct just ensures the "seen" weakmap (used for circular
|
|
140
|
+
// reference detection) isn't made part of the public API.
|
|
141
|
+
public_annotate as annotate, public_annotateObject as annotateObject, //
|
|
142
|
+
// NOTE: Don't acces theses private APIs directly. They are only exported here
|
|
143
|
+
// to better enable unit testing.
|
|
144
|
+
annotate as __private_annotate };
|
|
@@ -0,0 +1,137 @@
|
|
|
1
|
+
import { asDate, indent, INDENT, isMultiline } from '../_utils';
|
|
2
|
+
|
|
3
|
+
function serializeString(s, width) {
|
|
4
|
+
if (width === void 0) {
|
|
5
|
+
width = 80;
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
// Full string
|
|
9
|
+
// Abbreviated to $maxlen i.e. "Vincent Driess..." [truncated]
|
|
10
|
+
var ser = JSON.stringify(s);
|
|
11
|
+
|
|
12
|
+
if (ser.length <= width) {
|
|
13
|
+
return ser;
|
|
14
|
+
} // Cut off a bit
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
var truncated = s.substring(0, width - 15) + '...';
|
|
18
|
+
ser = JSON.stringify(truncated) + ' [truncated]';
|
|
19
|
+
return ser;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
function serializeArray(annotation, prefix) {
|
|
23
|
+
var items = annotation.items;
|
|
24
|
+
|
|
25
|
+
if (items.length === 0) {
|
|
26
|
+
return '[]';
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
var result = [];
|
|
30
|
+
items.forEach(function (item) {
|
|
31
|
+
var _serializeAnnotation = serializeAnnotation(item, prefix + INDENT),
|
|
32
|
+
ser = _serializeAnnotation[0],
|
|
33
|
+
ann = _serializeAnnotation[1];
|
|
34
|
+
|
|
35
|
+
result.push(prefix + INDENT + ser + ',');
|
|
36
|
+
|
|
37
|
+
if (ann !== undefined) {
|
|
38
|
+
result.push(indent(ann, prefix + INDENT));
|
|
39
|
+
}
|
|
40
|
+
});
|
|
41
|
+
return ['['].concat(result, [prefix + ']']).join('\n');
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
function serializeObject(annotation, prefix) {
|
|
45
|
+
var fields = annotation.fields;
|
|
46
|
+
var fieldNames = Object.keys(fields);
|
|
47
|
+
|
|
48
|
+
if (fieldNames.length === 0) {
|
|
49
|
+
return '{}';
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
var result = [];
|
|
53
|
+
fieldNames.forEach(function (key) {
|
|
54
|
+
var valueAnnotation = fields[key];
|
|
55
|
+
var kser = serializeValue(key);
|
|
56
|
+
var valPrefix = prefix + INDENT + ' '.repeat(kser.length + 2);
|
|
57
|
+
|
|
58
|
+
var _serializeAnnotation2 = serializeAnnotation(valueAnnotation, prefix + INDENT),
|
|
59
|
+
vser = _serializeAnnotation2[0],
|
|
60
|
+
vann = _serializeAnnotation2[1];
|
|
61
|
+
|
|
62
|
+
result.push(prefix + INDENT + kser + ': ' + vser + ',');
|
|
63
|
+
|
|
64
|
+
if (vann !== undefined) {
|
|
65
|
+
result.push(indent(vann, valPrefix));
|
|
66
|
+
}
|
|
67
|
+
});
|
|
68
|
+
return ['{'].concat(result, [prefix + '}']).join('\n');
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
export function serializeValue(value) {
|
|
72
|
+
// istanbul ignore else
|
|
73
|
+
if (typeof value === 'string') {
|
|
74
|
+
return serializeString(value);
|
|
75
|
+
} else if (typeof value === 'number' || typeof value === 'boolean') {
|
|
76
|
+
return value.toString();
|
|
77
|
+
} else if (value === null) {
|
|
78
|
+
return 'null';
|
|
79
|
+
} else if (value === undefined) {
|
|
80
|
+
return 'undefined';
|
|
81
|
+
} else {
|
|
82
|
+
var valueAsDate = asDate(value);
|
|
83
|
+
|
|
84
|
+
if (valueAsDate !== null) {
|
|
85
|
+
return "new Date(" + JSON.stringify(valueAsDate.toISOString()) + ")";
|
|
86
|
+
} else if (value instanceof Date) {
|
|
87
|
+
// NOTE: Using `instanceof Date` is unreliable way of checking dates.
|
|
88
|
+
// If this case occurs (and it didn't pass the prior isDate())
|
|
89
|
+
// check, then this must be the case where it's an invalid date.
|
|
90
|
+
return '(Invalid Date)';
|
|
91
|
+
} else {
|
|
92
|
+
return '(unserializable)';
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
export function serializeAnnotation(ann, prefix) {
|
|
97
|
+
if (prefix === void 0) {
|
|
98
|
+
prefix = '';
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
// The serialized data (the input object echoed back)
|
|
102
|
+
var serialized;
|
|
103
|
+
|
|
104
|
+
if (ann.type === 'array') {
|
|
105
|
+
serialized = serializeArray(ann, prefix);
|
|
106
|
+
} else if (ann.type === 'object') {
|
|
107
|
+
serialized = serializeObject(ann, prefix);
|
|
108
|
+
} else if (ann.type === 'function') {
|
|
109
|
+
serialized = '<function>';
|
|
110
|
+
} else if (ann.type === 'circular-ref') {
|
|
111
|
+
serialized = '<circular ref>';
|
|
112
|
+
} else if (ann.type === 'unknown') {
|
|
113
|
+
serialized = '???';
|
|
114
|
+
} else {
|
|
115
|
+
serialized = serializeValue(ann.value);
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
var text = ann.text;
|
|
119
|
+
|
|
120
|
+
if (text !== undefined) {
|
|
121
|
+
var sep = '^'.repeat(isMultiline(serialized) ? 1 : serialized.length);
|
|
122
|
+
return [serialized, [sep, text].join(isMultiline(text) ? '\n' : ' ')];
|
|
123
|
+
} else {
|
|
124
|
+
return [serialized, undefined];
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
export function formatInline(ann) {
|
|
128
|
+
var _serializeAnnotation3 = serializeAnnotation(ann),
|
|
129
|
+
serialized = _serializeAnnotation3[0],
|
|
130
|
+
annotation = _serializeAnnotation3[1];
|
|
131
|
+
|
|
132
|
+
if (annotation !== undefined) {
|
|
133
|
+
return serialized + '\n' + annotation;
|
|
134
|
+
} else {
|
|
135
|
+
return serialized;
|
|
136
|
+
}
|
|
137
|
+
}
|
package/es/index.js
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Elm-like JSON decoders, for use with Flow.
|
|
3
|
+
* See http://elmplayground.com/decoding-json-in-elm-1 for an introduction.
|
|
4
|
+
*
|
|
5
|
+
* Why? All JSON responses coming from our API endpoints are just that: free-form
|
|
6
|
+
* JSON data. To Flow, the only type classification possilbe is "any" -- effectively
|
|
7
|
+
* turning off all type checks for anything related to JSON. To the receiving end
|
|
8
|
+
* (our frontend), the structure of that data is completely opaque to any type
|
|
9
|
+
* checkers since JSON values can be anything: an object, an array, null, a string,
|
|
10
|
+
* a bool, etc. Our type system is not a runtime type system, so we need a way of
|
|
11
|
+
* "converting" an any-type JSON value into a type that we want to work with in our
|
|
12
|
+
* frontend code base.
|
|
13
|
+
*
|
|
14
|
+
* Elm's solution to this problem is to define composable decoders: functions that
|
|
15
|
+
* take anything and either fail with an error, or guarantee to return the expected
|
|
16
|
+
* type. In our case, it's fine to fail with a runtime error.
|
|
17
|
+
*
|
|
18
|
+
*/
|
|
19
|
+
export { guard } from './_guard';
|
|
20
|
+
export { compose, map, predicate } from './stdlib/composition';
|
|
21
|
+
export { array, nonEmptyArray, poja } from './stdlib/array';
|
|
22
|
+
export { boolean, numericBoolean, truthy } from './stdlib/boolean';
|
|
23
|
+
export { constant, hardcoded, mixed, null_, undefined_, unknown } from './stdlib/constants';
|
|
24
|
+
export { date, iso8601 } from './stdlib/date';
|
|
25
|
+
export { describe } from './stdlib/describe';
|
|
26
|
+
export { dispatch } from './stdlib/dispatch';
|
|
27
|
+
export { either, either3, either4, either5, either6, either7, either8, either9, oneOf } from './stdlib/either';
|
|
28
|
+
export { fail } from './stdlib/fail';
|
|
29
|
+
export { instanceOf } from './stdlib/instanceOf';
|
|
30
|
+
export { json, jsonObject, jsonArray } from './stdlib/json';
|
|
31
|
+
export { lazy } from './stdlib/lazy';
|
|
32
|
+
export { mapping, dict } from './stdlib/mapping';
|
|
33
|
+
export { integer, number, positiveInteger, positiveNumber } from './stdlib/number';
|
|
34
|
+
export { exact, inexact, object, pojo } from './stdlib/object';
|
|
35
|
+
export { maybe, nullable, optional } from './stdlib/optional';
|
|
36
|
+
export { email, nonEmptyString, regex, string, url } from './stdlib/string';
|
|
37
|
+
export { tuple1, tuple2, tuple3, tuple4, tuple5, tuple6 } from './stdlib/tuple';
|