decoders 2.0.0-beta5 → 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 +19 -0
- package/NotSupportedTSVersion.d.ts +1 -0
- package/README.md +934 -387
- package/_guard.d.ts +7 -0
- package/_guard.js +2 -6
- package/_guard.js.flow +3 -3
- package/{_esm/_guard.js → _guard.mjs} +3 -3
- package/_types.d.ts +13 -0
- package/{_esm/_types.js → _types.mjs} +0 -0
- package/_utils.d.ts +10 -0
- package/_utils.js +1 -1
- package/_utils.js.flow +3 -3
- package/{_esm/_utils.js → _utils.mjs} +1 -1
- package/annotate.d.ts +62 -0
- package/{_esm/annotate.js → annotate.mjs} +0 -0
- package/core/_helpers.d.ts +79 -0
- package/core/array.d.ts +8 -0
- package/core/array.js +19 -12
- package/core/array.js.flow +15 -11
- package/{_esm/core/array.js → core/array.mjs} +19 -10
- package/core/boolean.d.ts +5 -0
- package/core/boolean.js +5 -9
- package/core/boolean.js.flow +5 -7
- package/{_esm/core/boolean.js → core/boolean.mjs} +7 -7
- package/core/composition.d.ts +18 -0
- package/core/composition.js +41 -15
- package/core/composition.js.flow +41 -10
- package/core/composition.mjs +70 -0
- package/core/constants.d.ts +11 -0
- package/core/constants.js +6 -10
- package/core/constants.js.flow +7 -9
- package/{_esm/core/constants.js → core/constants.mjs} +7 -7
- package/core/date.d.ts +4 -0
- package/core/date.js +5 -9
- package/core/date.js.flow +4 -6
- package/{_esm/core/date.js → core/date.mjs} +7 -7
- package/core/describe.d.ts +3 -0
- package/core/describe.js +2 -6
- package/core/describe.js.flow +2 -2
- package/{_esm/core/describe.js → core/describe.mjs} +3 -3
- package/core/dispatch.d.ts +8 -0
- package/core/dispatch.js +11 -13
- package/core/dispatch.js.flow +13 -12
- package/{_esm/core/dispatch.js → core/dispatch.mjs} +12 -11
- package/core/either.d.ts +66 -0
- package/core/either.js +34 -50
- package/core/either.js.flow +40 -86
- package/core/either.mjs +90 -0
- package/core/fail.d.ts +3 -0
- package/core/fail.js +2 -6
- package/core/fail.js.flow +2 -2
- package/{_esm/core/fail.js → core/fail.mjs} +3 -3
- package/core/instanceOf.d.ts +3 -0
- package/core/instanceOf.js +2 -6
- package/core/instanceOf.js.flow +3 -3
- package/core/instanceOf.mjs +8 -0
- package/core/json.d.ts +11 -0
- package/core/json.js +3 -3
- package/core/json.js.flow +3 -3
- package/core/json.mjs +15 -0
- package/core/lazy.d.ts +3 -0
- package/{_esm/core/lazy.js → core/lazy.mjs} +0 -0
- package/core/number.d.ts +6 -0
- package/core/number.js +9 -13
- package/core/number.js.flow +18 -12
- package/core/number.mjs +25 -0
- package/core/object.d.ts +38 -0
- package/core/object.js +66 -13
- package/core/object.js.flow +84 -28
- package/{_esm/core/object.js → core/object.mjs} +64 -11
- package/core/optional.d.ts +5 -0
- package/core/optional.js +4 -8
- package/core/optional.js.flow +3 -3
- package/{_esm/core/optional.js → core/optional.mjs} +6 -6
- package/core/string.d.ts +13 -0
- package/core/string.js +31 -49
- package/core/string.js.flow +29 -39
- package/core/string.mjs +58 -0
- package/core/tuple.d.ts +30 -0
- package/core/tuple.js +30 -149
- package/core/tuple.js.flow +33 -197
- package/core/tuple.mjs +45 -0
- package/format.d.ts +4 -0
- package/{format/inline.js → format.js} +6 -1
- package/{_esm/format/inline.js.flow → format.js.flow} +6 -2
- package/{_esm/format/inline.js → format.mjs} +4 -1
- package/index.d.ts +42 -0
- package/index.js +33 -42
- package/index.js.flow +17 -18
- package/{_esm/index.js → index.mjs} +18 -19
- package/package.json +15 -3
- package/result.d.ts +39 -0
- package/result.js +9 -90
- package/result.js.flow +11 -87
- package/result.mjs +81 -0
- package/_esm/_guard.js.flow +0 -20
- package/_esm/_types.js.flow +0 -20
- package/_esm/_utils.js.flow +0 -97
- package/_esm/annotate.js.flow +0 -218
- package/_esm/core/array.js.flow +0 -103
- package/_esm/core/boolean.js.flow +0 -29
- package/_esm/core/composition.js +0 -42
- package/_esm/core/composition.js.flow +0 -43
- package/_esm/core/constants.js.flow +0 -46
- package/_esm/core/date.js.flow +0 -40
- package/_esm/core/describe.js.flow +0 -17
- package/_esm/core/dispatch.js.flow +0 -58
- package/_esm/core/either.js +0 -90
- package/_esm/core/either.js.flow +0 -151
- package/_esm/core/fail.js.flow +0 -12
- package/_esm/core/instanceOf.js +0 -8
- package/_esm/core/instanceOf.js.flow +0 -20
- package/_esm/core/json.js +0 -15
- package/_esm/core/json.js.flow +0 -28
- package/_esm/core/lazy.js.flow +0 -15
- package/_esm/core/mapping.js +0 -54
- package/_esm/core/mapping.js.flow +0 -54
- package/_esm/core/number.js +0 -25
- package/_esm/core/number.js.flow +0 -34
- package/_esm/core/object.js.flow +0 -203
- package/_esm/core/optional.js.flow +0 -41
- package/_esm/core/string.js +0 -76
- package/_esm/core/string.js.flow +0 -82
- package/_esm/core/tuple.js +0 -155
- package/_esm/core/tuple.js.flow +0 -215
- package/_esm/format/index.js +0 -2
- package/_esm/format/index.js.flow +0 -4
- package/_esm/format/short.js +0 -4
- package/_esm/format/short.js.flow +0 -8
- package/_esm/index.js.flow +0 -63
- package/_esm/result.js +0 -148
- package/_esm/result.js.flow +0 -174
- package/core/mapping.js +0 -67
- package/core/mapping.js.flow +0 -54
- package/format/index.js +0 -12
- package/format/index.js.flow +0 -4
- package/format/inline.js.flow +0 -122
- package/format/short.js +0 -10
- package/format/short.js.flow +0 -8
package/core/mapping.js.flow
DELETED
|
@@ -1,54 +0,0 @@
|
|
|
1
|
-
// @flow strict
|
|
2
|
-
|
|
3
|
-
import * as Result from '../result';
|
|
4
|
-
import { annotateObject } from '../annotate';
|
|
5
|
-
import { compose, map } from './composition';
|
|
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
|
-
* Given an object, will decode a Map of string keys to whatever values.
|
|
13
|
-
*
|
|
14
|
-
* For example, given a decoder for a Person, we can verify a Person lookup
|
|
15
|
-
* table structure (of type Map<string, Person>) like so:
|
|
16
|
-
*
|
|
17
|
-
* mapping(person)
|
|
18
|
-
*
|
|
19
|
-
*/
|
|
20
|
-
export function mapping<T>(decoder: Decoder<T>): Decoder<Map<string, T>> {
|
|
21
|
-
return compose(pojo, (blob: { +[key: string]: mixed }) => {
|
|
22
|
-
let tuples: Array<[string, T]> = [];
|
|
23
|
-
let errors: { [key: string]: Annotation } | null = null;
|
|
24
|
-
|
|
25
|
-
Object.keys(blob).forEach((key: string) => {
|
|
26
|
-
const value = blob[key];
|
|
27
|
-
const result = decoder(value);
|
|
28
|
-
if (result.type === 'ok') {
|
|
29
|
-
if (errors === null) {
|
|
30
|
-
tuples.push([key, result.value]);
|
|
31
|
-
}
|
|
32
|
-
} else {
|
|
33
|
-
tuples.length = 0; // Clear the tuples array
|
|
34
|
-
if (errors === null) {
|
|
35
|
-
errors = {};
|
|
36
|
-
}
|
|
37
|
-
errors[key] = result.error;
|
|
38
|
-
}
|
|
39
|
-
});
|
|
40
|
-
|
|
41
|
-
if (errors !== null) {
|
|
42
|
-
return Result.err(merge(annotateObject(blob), errors));
|
|
43
|
-
} else {
|
|
44
|
-
return Result.ok(new Map(tuples));
|
|
45
|
-
}
|
|
46
|
-
});
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
/**
|
|
50
|
-
* Like mapping(), but returns an object rather than a Map instance.
|
|
51
|
-
*/
|
|
52
|
-
export function dict<T>(decoder: Decoder<T>): Decoder<{ [string]: T }> {
|
|
53
|
-
return map(mapping(decoder), (m) => Object.fromEntries(m));
|
|
54
|
-
}
|
package/format/index.js
DELETED
|
@@ -1,12 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
|
|
3
|
-
exports.__esModule = true;
|
|
4
|
-
exports.formatShort = exports.formatInline = void 0;
|
|
5
|
-
|
|
6
|
-
var _inline = require("./inline");
|
|
7
|
-
|
|
8
|
-
exports.formatInline = _inline.formatInline;
|
|
9
|
-
|
|
10
|
-
var _short = require("./short");
|
|
11
|
-
|
|
12
|
-
exports.formatShort = _short.formatShort;
|
package/format/index.js.flow
DELETED
package/format/inline.js.flow
DELETED
|
@@ -1,122 +0,0 @@
|
|
|
1
|
-
// @flow strict
|
|
2
|
-
|
|
3
|
-
import { asDate, indent, INDENT, isMultiline } from '../_utils';
|
|
4
|
-
import type { Annotation, ArrayAnnotation, ObjectAnnotation } from '../annotate';
|
|
5
|
-
|
|
6
|
-
function serializeString(s: string, width: number = 80): string {
|
|
7
|
-
// Full string
|
|
8
|
-
// Abbreviated to $maxlen i.e. "Vincent Driess..." [truncated]
|
|
9
|
-
let ser = JSON.stringify(s);
|
|
10
|
-
if (ser.length <= width) {
|
|
11
|
-
return ser;
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
// Cut off a bit
|
|
15
|
-
const truncated = s.substring(0, width - 15) + '...';
|
|
16
|
-
ser = JSON.stringify(truncated) + ' [truncated]';
|
|
17
|
-
return ser;
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
function serializeArray(annotation: ArrayAnnotation, prefix: string): string {
|
|
21
|
-
const { items } = annotation;
|
|
22
|
-
if (items.length === 0) {
|
|
23
|
-
return '[]';
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
const result = [];
|
|
27
|
-
items.forEach((item) => {
|
|
28
|
-
const [ser, ann] = serializeAnnotation(item, prefix + INDENT);
|
|
29
|
-
result.push(prefix + INDENT + ser + ',');
|
|
30
|
-
if (ann !== undefined) {
|
|
31
|
-
result.push(indent(ann, prefix + INDENT));
|
|
32
|
-
}
|
|
33
|
-
});
|
|
34
|
-
return ['[', ...result, prefix + ']'].join('\n');
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
function serializeObject(annotation: ObjectAnnotation, prefix: string): string {
|
|
38
|
-
const { fields } = annotation;
|
|
39
|
-
|
|
40
|
-
const fieldNames = Object.keys(fields);
|
|
41
|
-
if (fieldNames.length === 0) {
|
|
42
|
-
return '{}';
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
const result = [];
|
|
46
|
-
fieldNames.forEach((key) => {
|
|
47
|
-
const valueAnnotation = fields[key];
|
|
48
|
-
const kser = serializeValue(key);
|
|
49
|
-
|
|
50
|
-
const valPrefix = prefix + INDENT + ' '.repeat(kser.length + 2);
|
|
51
|
-
const [vser, vann] = serializeAnnotation(valueAnnotation, prefix + INDENT);
|
|
52
|
-
|
|
53
|
-
result.push(prefix + INDENT + kser + ': ' + vser + ',');
|
|
54
|
-
if (vann !== undefined) {
|
|
55
|
-
result.push(indent(vann, valPrefix));
|
|
56
|
-
}
|
|
57
|
-
});
|
|
58
|
-
return ['{', ...result, prefix + '}'].join('\n');
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
export function serializeValue(value: mixed): string {
|
|
62
|
-
// istanbul ignore else
|
|
63
|
-
if (typeof value === 'string') {
|
|
64
|
-
return serializeString(value);
|
|
65
|
-
} else if (typeof value === 'number' || typeof value === 'boolean') {
|
|
66
|
-
return value.toString();
|
|
67
|
-
} else if (value === null) {
|
|
68
|
-
return 'null';
|
|
69
|
-
} else if (value === undefined) {
|
|
70
|
-
return 'undefined';
|
|
71
|
-
} else {
|
|
72
|
-
const valueAsDate = asDate(value);
|
|
73
|
-
if (valueAsDate !== null) {
|
|
74
|
-
return `new Date(${JSON.stringify(valueAsDate.toISOString())})`;
|
|
75
|
-
} else if (value instanceof Date) {
|
|
76
|
-
// NOTE: Using `instanceof Date` is unreliable way of checking dates.
|
|
77
|
-
// If this case occurs (and it didn't pass the prior isDate())
|
|
78
|
-
// check, then this must be the case where it's an invalid date.
|
|
79
|
-
return '(Invalid Date)';
|
|
80
|
-
} else {
|
|
81
|
-
return '(unserializable)';
|
|
82
|
-
}
|
|
83
|
-
}
|
|
84
|
-
}
|
|
85
|
-
|
|
86
|
-
export function serializeAnnotation(
|
|
87
|
-
ann: Annotation,
|
|
88
|
-
prefix: string = '',
|
|
89
|
-
): [string, string | void] {
|
|
90
|
-
// The serialized data (the input object echoed back)
|
|
91
|
-
let serialized;
|
|
92
|
-
if (ann.type === 'array') {
|
|
93
|
-
serialized = serializeArray(ann, prefix);
|
|
94
|
-
} else if (ann.type === 'object') {
|
|
95
|
-
serialized = serializeObject(ann, prefix);
|
|
96
|
-
} else if (ann.type === 'function') {
|
|
97
|
-
serialized = '<function>';
|
|
98
|
-
} else if (ann.type === 'circular-ref') {
|
|
99
|
-
serialized = '<circular ref>';
|
|
100
|
-
} else if (ann.type === 'unknown') {
|
|
101
|
-
serialized = '???';
|
|
102
|
-
} else {
|
|
103
|
-
serialized = serializeValue(ann.value);
|
|
104
|
-
}
|
|
105
|
-
|
|
106
|
-
const text = ann.text;
|
|
107
|
-
if (text !== undefined) {
|
|
108
|
-
const sep = '^'.repeat(isMultiline(serialized) ? 1 : serialized.length);
|
|
109
|
-
return [serialized, [sep, text].join(isMultiline(text) ? '\n' : ' ')];
|
|
110
|
-
} else {
|
|
111
|
-
return [serialized, undefined];
|
|
112
|
-
}
|
|
113
|
-
}
|
|
114
|
-
|
|
115
|
-
export function formatInline(ann: Annotation): string {
|
|
116
|
-
const [serialized, annotation] = serializeAnnotation(ann);
|
|
117
|
-
if (annotation !== undefined) {
|
|
118
|
-
return serialized + '\n' + annotation;
|
|
119
|
-
} else {
|
|
120
|
-
return serialized;
|
|
121
|
-
}
|
|
122
|
-
}
|
package/format/short.js
DELETED