decoders 2.0.0-beta1 → 2.0.0-beta2
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/{es → _esm}/_guard.js +0 -0
- package/{cjs → _esm}/_guard.js.flow +0 -0
- package/{es → _esm}/_types.js +0 -0
- package/{cjs → _esm}/_types.js.flow +0 -0
- package/{es → _esm}/_utils.js +0 -0
- package/{cjs → _esm}/_utils.js.flow +0 -0
- package/{es → _esm}/annotate.js +0 -0
- package/{cjs → _esm}/annotate.js.flow +0 -0
- package/{es/stdlib → _esm/core}/array.js +0 -0
- package/{cjs/stdlib → _esm/core}/array.js.flow +0 -0
- package/{es/stdlib → _esm/core}/boolean.js +0 -0
- package/{cjs/stdlib → _esm/core}/boolean.js.flow +0 -0
- package/{es/stdlib → _esm/core}/composition.js +0 -0
- package/{cjs/stdlib → _esm/core}/composition.js.flow +0 -0
- package/{es/stdlib → _esm/core}/constants.js +0 -0
- package/{cjs/stdlib → _esm/core}/constants.js.flow +0 -0
- package/{es/stdlib → _esm/core}/date.js +0 -0
- package/{cjs/stdlib → _esm/core}/date.js.flow +0 -0
- package/{es/stdlib → _esm/core}/describe.js +0 -0
- package/{cjs/stdlib → _esm/core}/describe.js.flow +0 -0
- package/{es/stdlib → _esm/core}/dispatch.js +0 -0
- package/{cjs/stdlib → _esm/core}/dispatch.js.flow +0 -0
- package/{es/stdlib → _esm/core}/either.js +0 -0
- package/{cjs/stdlib → _esm/core}/either.js.flow +0 -0
- package/{es/stdlib → _esm/core}/fail.js +0 -0
- package/{cjs/stdlib → _esm/core}/fail.js.flow +0 -0
- package/{es/stdlib → _esm/core}/instanceOf.js +0 -0
- package/{cjs/stdlib → _esm/core}/instanceOf.js.flow +0 -0
- package/{es/stdlib → _esm/core}/json.js +0 -0
- package/{cjs/stdlib → _esm/core}/json.js.flow +0 -0
- package/{es/stdlib → _esm/core}/lazy.js +0 -0
- package/{cjs/stdlib → _esm/core}/lazy.js.flow +0 -0
- package/{es/stdlib → _esm/core}/mapping.js +0 -0
- package/{cjs/stdlib → _esm/core}/mapping.js.flow +0 -0
- package/{es/stdlib → _esm/core}/number.js +0 -0
- package/{cjs/stdlib → _esm/core}/number.js.flow +0 -0
- package/{es/stdlib → _esm/core}/object.js +0 -0
- package/{cjs/stdlib → _esm/core}/object.js.flow +0 -0
- package/{es/stdlib → _esm/core}/optional.js +0 -0
- package/{cjs/stdlib → _esm/core}/optional.js.flow +0 -0
- package/{es/stdlib → _esm/core}/string.js +0 -0
- package/{cjs/stdlib → _esm/core}/string.js.flow +0 -0
- package/{es/stdlib → _esm/core}/tuple.js +0 -0
- package/{cjs/stdlib → _esm/core}/tuple.js.flow +0 -0
- package/{es → _esm}/format/index.js +0 -0
- package/{cjs → _esm}/format/index.js.flow +0 -0
- package/{es → _esm}/format/inline.js +0 -0
- package/{cjs → _esm}/format/inline.js.flow +0 -0
- package/{es → _esm}/format/short.js +0 -0
- package/{cjs → _esm}/format/short.js.flow +0 -0
- package/{es → _esm}/index.js +18 -18
- package/{cjs → _esm}/index.js.flow +19 -26
- package/{es → _esm}/result.js +0 -0
- package/{cjs → _esm}/result.js.flow +0 -0
- package/{cjs/_guard.js → _guard.js} +0 -0
- package/_guard.js.flow +20 -0
- package/{cjs/_types.js → _types.js} +0 -0
- package/_types.js.flow +20 -0
- package/{cjs/_utils.js → _utils.js} +0 -0
- package/_utils.js.flow +97 -0
- package/{cjs/annotate.js → annotate.js} +0 -0
- package/annotate.js.flow +218 -0
- package/{cjs/stdlib → core}/array.js +0 -0
- package/core/array.js.flow +103 -0
- package/{cjs/stdlib → core}/boolean.js +0 -0
- package/core/boolean.js.flow +29 -0
- package/{cjs/stdlib → core}/composition.js +0 -0
- package/core/composition.js.flow +43 -0
- package/{cjs/stdlib → core}/constants.js +0 -0
- package/core/constants.js.flow +46 -0
- package/{cjs/stdlib → core}/date.js +0 -0
- package/core/date.js.flow +40 -0
- package/{cjs/stdlib → core}/describe.js +0 -0
- package/core/describe.js.flow +17 -0
- package/{cjs/stdlib → core}/dispatch.js +0 -0
- package/core/dispatch.js.flow +58 -0
- package/{cjs/stdlib → core}/either.js +0 -0
- package/core/either.js.flow +151 -0
- package/{cjs/stdlib → core}/fail.js +0 -0
- package/core/fail.js.flow +12 -0
- package/{cjs/stdlib → core}/instanceOf.js +0 -0
- package/core/instanceOf.js.flow +20 -0
- package/{cjs/stdlib → core}/json.js +0 -0
- package/core/json.js.flow +28 -0
- package/{cjs/stdlib → core}/lazy.js +0 -0
- package/core/lazy.js.flow +15 -0
- package/{cjs/stdlib → core}/mapping.js +0 -0
- package/core/mapping.js.flow +54 -0
- package/{cjs/stdlib → core}/number.js +0 -0
- package/core/number.js.flow +34 -0
- package/{cjs/stdlib → core}/object.js +0 -0
- package/core/object.js.flow +203 -0
- package/{cjs/stdlib → core}/optional.js +0 -0
- package/core/optional.js.flow +41 -0
- package/{cjs/stdlib → core}/string.js +0 -0
- package/core/string.js.flow +82 -0
- package/{cjs/stdlib → core}/tuple.js +0 -0
- package/core/tuple.js.flow +220 -0
- package/{cjs/format → format}/index.js +0 -0
- package/format/index.js.flow +4 -0
- package/{cjs/format → format}/inline.js +0 -0
- package/format/inline.js.flow +122 -0
- package/{cjs/format → format}/short.js +0 -0
- package/format/short.js.flow +8 -0
- package/{cjs/index.js → index.js} +18 -18
- package/index.js.flow +56 -0
- package/package.json +3 -12
- package/{cjs/result.js → result.js} +0 -0
- package/result.js.flow +166 -0
- package/ts/_guard.d.ts +0 -7
- package/ts/_helpers.d.ts +0 -79
- package/ts/_types.d.ts +0 -16
- package/ts/_utils.d.ts +0 -13
- package/ts/annotate.d.ts +0 -58
- package/ts/array.d.ts +0 -5
- package/ts/boolean.d.ts +0 -5
- package/ts/constants.d.ts +0 -11
- package/ts/date.d.ts +0 -4
- package/ts/describe.d.ts +0 -3
- package/ts/dispatch.d.ts +0 -8
- package/ts/either.d.ts +0 -61
- package/ts/fail.d.ts +0 -3
- package/ts/index.d.ts +0 -42
- package/ts/inline.d.ts +0 -3
- package/ts/instanceOf.d.ts +0 -3
- package/ts/json.d.ts +0 -11
- package/ts/lazy.d.ts +0 -3
- package/ts/mapping.d.ts +0 -4
- package/ts/number.d.ts +0 -6
- package/ts/object.d.ts +0 -33
- package/ts/optional.d.ts +0 -5
- package/ts/result.d.ts +0 -39
- package/ts/short.d.ts +0 -3
- package/ts/string.d.ts +0 -7
- package/ts/tuple.d.ts +0 -30
|
@@ -0,0 +1,122 @@
|
|
|
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
|
+
}
|
|
File without changes
|
|
@@ -7,25 +7,25 @@ var _guard = require("./_guard");
|
|
|
7
7
|
|
|
8
8
|
exports.guard = _guard.guard;
|
|
9
9
|
|
|
10
|
-
var _composition = require("./
|
|
10
|
+
var _composition = require("./core/composition");
|
|
11
11
|
|
|
12
12
|
exports.compose = _composition.compose;
|
|
13
13
|
exports.map = _composition.map;
|
|
14
14
|
exports.predicate = _composition.predicate;
|
|
15
15
|
|
|
16
|
-
var _array = require("./
|
|
16
|
+
var _array = require("./core/array");
|
|
17
17
|
|
|
18
18
|
exports.array = _array.array;
|
|
19
19
|
exports.nonEmptyArray = _array.nonEmptyArray;
|
|
20
20
|
exports.poja = _array.poja;
|
|
21
21
|
|
|
22
|
-
var _boolean = require("./
|
|
22
|
+
var _boolean = require("./core/boolean");
|
|
23
23
|
|
|
24
24
|
exports["boolean"] = _boolean["boolean"];
|
|
25
25
|
exports.numericBoolean = _boolean.numericBoolean;
|
|
26
26
|
exports.truthy = _boolean.truthy;
|
|
27
27
|
|
|
28
|
-
var _constants = require("./
|
|
28
|
+
var _constants = require("./core/constants");
|
|
29
29
|
|
|
30
30
|
exports.constant = _constants.constant;
|
|
31
31
|
exports.hardcoded = _constants.hardcoded;
|
|
@@ -34,20 +34,20 @@ exports.null_ = _constants.null_;
|
|
|
34
34
|
exports.undefined_ = _constants.undefined_;
|
|
35
35
|
exports.unknown = _constants.unknown;
|
|
36
36
|
|
|
37
|
-
var _date = require("./
|
|
37
|
+
var _date = require("./core/date");
|
|
38
38
|
|
|
39
39
|
exports.date = _date.date;
|
|
40
40
|
exports.iso8601 = _date.iso8601;
|
|
41
41
|
|
|
42
|
-
var _describe = require("./
|
|
42
|
+
var _describe = require("./core/describe");
|
|
43
43
|
|
|
44
44
|
exports.describe = _describe.describe;
|
|
45
45
|
|
|
46
|
-
var _dispatch = require("./
|
|
46
|
+
var _dispatch = require("./core/dispatch");
|
|
47
47
|
|
|
48
48
|
exports.dispatch = _dispatch.dispatch;
|
|
49
49
|
|
|
50
|
-
var _either = require("./
|
|
50
|
+
var _either = require("./core/either");
|
|
51
51
|
|
|
52
52
|
exports.either = _either.either;
|
|
53
53
|
exports.either3 = _either.either3;
|
|
@@ -59,50 +59,50 @@ exports.either8 = _either.either8;
|
|
|
59
59
|
exports.either9 = _either.either9;
|
|
60
60
|
exports.oneOf = _either.oneOf;
|
|
61
61
|
|
|
62
|
-
var _fail = require("./
|
|
62
|
+
var _fail = require("./core/fail");
|
|
63
63
|
|
|
64
64
|
exports.fail = _fail.fail;
|
|
65
65
|
|
|
66
|
-
var _instanceOf = require("./
|
|
66
|
+
var _instanceOf = require("./core/instanceOf");
|
|
67
67
|
|
|
68
68
|
exports.instanceOf = _instanceOf.instanceOf;
|
|
69
69
|
|
|
70
|
-
var _json = require("./
|
|
70
|
+
var _json = require("./core/json");
|
|
71
71
|
|
|
72
72
|
exports.json = _json.json;
|
|
73
73
|
exports.jsonObject = _json.jsonObject;
|
|
74
74
|
exports.jsonArray = _json.jsonArray;
|
|
75
75
|
|
|
76
|
-
var _lazy = require("./
|
|
76
|
+
var _lazy = require("./core/lazy");
|
|
77
77
|
|
|
78
78
|
exports.lazy = _lazy.lazy;
|
|
79
79
|
|
|
80
|
-
var _mapping = require("./
|
|
80
|
+
var _mapping = require("./core/mapping");
|
|
81
81
|
|
|
82
82
|
exports.mapping = _mapping.mapping;
|
|
83
83
|
exports.dict = _mapping.dict;
|
|
84
84
|
|
|
85
|
-
var _number = require("./
|
|
85
|
+
var _number = require("./core/number");
|
|
86
86
|
|
|
87
87
|
exports.integer = _number.integer;
|
|
88
88
|
exports.number = _number.number;
|
|
89
89
|
exports.positiveInteger = _number.positiveInteger;
|
|
90
90
|
exports.positiveNumber = _number.positiveNumber;
|
|
91
91
|
|
|
92
|
-
var _object = require("./
|
|
92
|
+
var _object = require("./core/object");
|
|
93
93
|
|
|
94
94
|
exports.exact = _object.exact;
|
|
95
95
|
exports.inexact = _object.inexact;
|
|
96
96
|
exports.object = _object.object;
|
|
97
97
|
exports.pojo = _object.pojo;
|
|
98
98
|
|
|
99
|
-
var _optional = require("./
|
|
99
|
+
var _optional = require("./core/optional");
|
|
100
100
|
|
|
101
101
|
exports.maybe = _optional.maybe;
|
|
102
102
|
exports.nullable = _optional.nullable;
|
|
103
103
|
exports.optional = _optional.optional;
|
|
104
104
|
|
|
105
|
-
var _string = require("./
|
|
105
|
+
var _string = require("./core/string");
|
|
106
106
|
|
|
107
107
|
exports.email = _string.email;
|
|
108
108
|
exports.nonEmptyString = _string.nonEmptyString;
|
|
@@ -110,7 +110,7 @@ exports.regex = _string.regex;
|
|
|
110
110
|
exports.string = _string.string;
|
|
111
111
|
exports.url = _string.url;
|
|
112
112
|
|
|
113
|
-
var _tuple = require("./
|
|
113
|
+
var _tuple = require("./core/tuple");
|
|
114
114
|
|
|
115
115
|
exports.tuple1 = _tuple.tuple1;
|
|
116
116
|
exports.tuple2 = _tuple.tuple2;
|
package/index.js.flow
ADDED
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
// @flow strict
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Elm-like JSON decoders, for use with Flow.
|
|
5
|
+
* See http://elmplayground.com/decoding-json-in-elm-1 for an introduction.
|
|
6
|
+
*
|
|
7
|
+
* Why? All JSON responses coming from our API endpoints are just that: free-form
|
|
8
|
+
* JSON data. To Flow, the only type classification possilbe is "any" -- effectively
|
|
9
|
+
* turning off all type checks for anything related to JSON. To the receiving end
|
|
10
|
+
* (our frontend), the structure of that data is completely opaque to any type
|
|
11
|
+
* checkers since JSON values can be anything: an object, an array, null, a string,
|
|
12
|
+
* a bool, etc. Our type system is not a runtime type system, so we need a way of
|
|
13
|
+
* "converting" an any-type JSON value into a type that we want to work with in our
|
|
14
|
+
* frontend code base.
|
|
15
|
+
*
|
|
16
|
+
* Elm's solution to this problem is to define composable decoders: functions that
|
|
17
|
+
* take anything and either fail with an error, or guarantee to return the expected
|
|
18
|
+
* type. In our case, it's fine to fail with a runtime error.
|
|
19
|
+
*
|
|
20
|
+
*/
|
|
21
|
+
export type { Decoder, Guard } from './_types';
|
|
22
|
+
export type { DecoderType, GuardType } from './_types';
|
|
23
|
+
|
|
24
|
+
export type { JSONValue, JSONObject, JSONArray } from './core/json';
|
|
25
|
+
|
|
26
|
+
export { guard } from './_guard';
|
|
27
|
+
|
|
28
|
+
export { compose, map, predicate } from './core/composition';
|
|
29
|
+
|
|
30
|
+
export { array, nonEmptyArray, poja } from './core/array';
|
|
31
|
+
export { boolean, numericBoolean, truthy } from './core/boolean';
|
|
32
|
+
export { constant, hardcoded, mixed, null_, undefined_, unknown } from './core/constants';
|
|
33
|
+
export { date, iso8601 } from './core/date';
|
|
34
|
+
export { describe } from './core/describe';
|
|
35
|
+
export { dispatch } from './core/dispatch';
|
|
36
|
+
export {
|
|
37
|
+
either,
|
|
38
|
+
either3,
|
|
39
|
+
either4,
|
|
40
|
+
either5,
|
|
41
|
+
either6,
|
|
42
|
+
either7,
|
|
43
|
+
either8,
|
|
44
|
+
either9,
|
|
45
|
+
oneOf,
|
|
46
|
+
} from './core/either';
|
|
47
|
+
export { fail } from './core/fail';
|
|
48
|
+
export { instanceOf } from './core/instanceOf';
|
|
49
|
+
export { json, jsonObject, jsonArray } from './core/json';
|
|
50
|
+
export { lazy } from './core/lazy';
|
|
51
|
+
export { mapping, dict } from './core/mapping';
|
|
52
|
+
export { integer, number, positiveInteger, positiveNumber } from './core/number';
|
|
53
|
+
export { exact, inexact, object, pojo } from './core/object';
|
|
54
|
+
export { maybe, nullable, optional } from './core/optional';
|
|
55
|
+
export { email, nonEmptyString, regex, string, url } from './core/string';
|
|
56
|
+
export { tuple1, tuple2, tuple3, tuple4, tuple5, tuple6 } from './core/tuple';
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "decoders",
|
|
3
|
-
"version": "2.0.0-
|
|
3
|
+
"version": "2.0.0-beta2",
|
|
4
4
|
"description": "Elegant and battle-tested validation library for type-safe input data (for TypeScript and Flow)",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"repository": {
|
|
@@ -12,16 +12,8 @@
|
|
|
12
12
|
"bugs": {
|
|
13
13
|
"url": "https://github.com/nvie/decoders/issues"
|
|
14
14
|
},
|
|
15
|
-
"main": "./
|
|
16
|
-
"module": "./
|
|
17
|
-
"type": "module",
|
|
18
|
-
"exports": {
|
|
19
|
-
"require": "./cjs/index.js",
|
|
20
|
-
"import": "./es/index.js"
|
|
21
|
-
},
|
|
22
|
-
"engines": {
|
|
23
|
-
"node": ">=12"
|
|
24
|
-
},
|
|
15
|
+
"main": "./index.js",
|
|
16
|
+
"module": "./_esm/index.js",
|
|
25
17
|
"keywords": [
|
|
26
18
|
"decoder",
|
|
27
19
|
"decoders",
|
|
@@ -48,7 +40,6 @@
|
|
|
48
40
|
"map",
|
|
49
41
|
"predicate"
|
|
50
42
|
],
|
|
51
|
-
"types": "./ts/index.d.ts",
|
|
52
43
|
"githubUrl": "https://github.com/nvie/decoders",
|
|
53
44
|
"sideEffects": false
|
|
54
45
|
}
|
|
File without changes
|
package/result.js.flow
ADDED
|
@@ -0,0 +1,166 @@
|
|
|
1
|
+
// @flow strict
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Result <value> <error>
|
|
5
|
+
* = Ok <value>
|
|
6
|
+
* | Err <error>
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
type Ok<+T> = {| +type: 'ok', +value: T |};
|
|
10
|
+
type Err<+E> = {| +type: 'err', +error: E |};
|
|
11
|
+
|
|
12
|
+
export type Result<+T, +E> = Ok<T> | Err<E>;
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* Create a new Result instance representing a successful computation.
|
|
16
|
+
*/
|
|
17
|
+
export function ok<T>(value: T): Ok<T> {
|
|
18
|
+
return { type: 'ok', value };
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* Create a new Result instance representing a failed computation.
|
|
23
|
+
*/
|
|
24
|
+
export function err<E>(error: E): Err<E> {
|
|
25
|
+
return { type: 'err', error };
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
export function toString(result: Result<mixed, mixed>): string {
|
|
29
|
+
return result.type === 'ok'
|
|
30
|
+
? `Ok(${String(result.value)})`
|
|
31
|
+
: `Err(${String(result.error)})`;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
export function isOk(result: Result<mixed, mixed>): boolean {
|
|
35
|
+
return result.type === 'ok';
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
export function isErr(result: Result<mixed, mixed>): boolean {
|
|
39
|
+
return result.type === 'err';
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
export function withDefault<T>(result: Result<T, mixed>, defaultValue: T): T {
|
|
43
|
+
return result.type === 'ok' ? result.value : defaultValue;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
export function value<T>(result: Result<T, mixed>): void | T {
|
|
47
|
+
return result.type === 'ok' ? result.value : undefined;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
export function errValue<E>(result: Result<mixed, E>): void | E {
|
|
51
|
+
return result.type === 'err' ? result.error : undefined;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* Unwrap the value from this Result instance if this is an "Ok" result.
|
|
56
|
+
* Otherwise, will throw the "Err" error via a runtime exception.
|
|
57
|
+
*/
|
|
58
|
+
export function unwrap<T>(result: Result<T, mixed>): T {
|
|
59
|
+
if (result.type === 'ok') {
|
|
60
|
+
return result.value;
|
|
61
|
+
} else {
|
|
62
|
+
throw result.error;
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
export function expect<T>(result: Result<T, mixed>, message: string | Error): T {
|
|
67
|
+
if (result.type === 'ok') {
|
|
68
|
+
return result.value;
|
|
69
|
+
} else {
|
|
70
|
+
throw message instanceof Error ? message : new Error(message);
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
export function dispatch<T, E, O>(
|
|
75
|
+
result: Result<T, E>,
|
|
76
|
+
okCallback: (value: T) => O,
|
|
77
|
+
errCallback: (error: E) => O,
|
|
78
|
+
): O {
|
|
79
|
+
return result.type === 'ok' ? okCallback(result.value) : errCallback(result.error);
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
/**
|
|
83
|
+
* If the given result is OK, defers to the other result. Otherwise returns the
|
|
84
|
+
* error result.
|
|
85
|
+
*
|
|
86
|
+
* It's like saying A && B, but on Result.
|
|
87
|
+
*
|
|
88
|
+
* Examples:
|
|
89
|
+
*
|
|
90
|
+
* Result.ok(42) && Result.ok('hi') // => Ok('hi')
|
|
91
|
+
* Result.err('boo') && Result.ok('hi') // => Err('boo')
|
|
92
|
+
* Result.ok(42) && Result.err('boo') // => Err('boo')
|
|
93
|
+
* Result.err('boo') && Result.err('boo') // => Err('boo')
|
|
94
|
+
*
|
|
95
|
+
*/
|
|
96
|
+
// export function and<T, E, T2>(
|
|
97
|
+
// result1: Result<T, E>,
|
|
98
|
+
// result2: Result<T2, E>,
|
|
99
|
+
// ): Result<T2, E> {
|
|
100
|
+
// return result1.type === 'ok' ? result2 : result1;
|
|
101
|
+
// }
|
|
102
|
+
|
|
103
|
+
/**
|
|
104
|
+
* If the given result is OK, return that result. Otherwise, defers to the
|
|
105
|
+
* other result.
|
|
106
|
+
*
|
|
107
|
+
* It's like saying A || B, but on Result.
|
|
108
|
+
*
|
|
109
|
+
* Examples:
|
|
110
|
+
*
|
|
111
|
+
* Result.ok(42) || Result.ok('hi') // => Ok(42)
|
|
112
|
+
* Result.err('boo') || Result.ok('hi') // => Ok('hi')
|
|
113
|
+
* Result.ok(42) || Result.err('boo') // => Ok(42)
|
|
114
|
+
* Result.err('bleh') || Result.err('boo') // => Err('boo')
|
|
115
|
+
*
|
|
116
|
+
*/
|
|
117
|
+
// export function or<T, E, E2>(
|
|
118
|
+
// result1: Result<T, E>,
|
|
119
|
+
// result2: Result<T, E2>,
|
|
120
|
+
// ): Result<T, E2> {
|
|
121
|
+
// return result1.type === 'ok' ? result1 : result2;
|
|
122
|
+
// }
|
|
123
|
+
|
|
124
|
+
/**
|
|
125
|
+
* Like .and(), aka &&, but the second argument gets evaluated lazily only if
|
|
126
|
+
* the first result is an Ok result. If so, it has access to the Ok value from
|
|
127
|
+
* the first argument.
|
|
128
|
+
*/
|
|
129
|
+
export function andThen<T, E, T2>(
|
|
130
|
+
result1: Result<T, E>,
|
|
131
|
+
lazyResult2: (value: T) => Result<T2, E>,
|
|
132
|
+
): Result<T2, E> {
|
|
133
|
+
return result1.type === 'ok' ? lazyResult2(result1.value) : result1;
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
/**
|
|
137
|
+
* Like .or(), aka ||, but the second argument gets evaluated lazily only if
|
|
138
|
+
* the first result is an Err result. If so, it has access to the Err value
|
|
139
|
+
* from the first argument.
|
|
140
|
+
*/
|
|
141
|
+
export function orElse<T, E, E2>(
|
|
142
|
+
result1: Result<T, E>,
|
|
143
|
+
lazyResult2: (errValue: E) => Result<T, E2>,
|
|
144
|
+
): Result<T, E2> {
|
|
145
|
+
return result1.type === 'ok' ? result1 : lazyResult2(result1.error);
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
/**
|
|
149
|
+
* Transform an Ok result. Will not touch Err results.
|
|
150
|
+
*/
|
|
151
|
+
export function map<T, E, T2>(
|
|
152
|
+
result: Result<T, E>,
|
|
153
|
+
mapper: (value: T) => T2,
|
|
154
|
+
): Result<T2, E> {
|
|
155
|
+
return result.type === 'ok' ? ok(mapper(result.value)) : result;
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
/**
|
|
159
|
+
* Transform an Err value. Will not touch Ok results.
|
|
160
|
+
*/
|
|
161
|
+
export function mapError<T, E, E2>(
|
|
162
|
+
result: Result<T, E>,
|
|
163
|
+
mapper: (error: E) => E2,
|
|
164
|
+
): Result<T, E2> {
|
|
165
|
+
return result.type === 'ok' ? result : err(mapper(result.error));
|
|
166
|
+
}
|
package/ts/_guard.d.ts
DELETED
package/ts/_helpers.d.ts
DELETED
|
@@ -1,79 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Given a type like:
|
|
3
|
-
*
|
|
4
|
-
* {
|
|
5
|
-
* a: string;
|
|
6
|
-
* b: number | undefined;
|
|
7
|
-
* c: null | undefined;
|
|
8
|
-
* d: null;
|
|
9
|
-
* e: undefined;
|
|
10
|
-
* }
|
|
11
|
-
*
|
|
12
|
-
* Will drop all the "undefined" types. In this case, only "e":
|
|
13
|
-
*
|
|
14
|
-
* {
|
|
15
|
-
* a: string;
|
|
16
|
-
* b: number | undefined;
|
|
17
|
-
* c: null | undefined;
|
|
18
|
-
* d: null;
|
|
19
|
-
* }
|
|
20
|
-
*
|
|
21
|
-
*/
|
|
22
|
-
type Compact<T> = { [K in IsDefined<T, keyof T>]: T[K] };
|
|
23
|
-
|
|
24
|
-
type IsDefined<T, K extends keyof T> = K extends any
|
|
25
|
-
? T[K] extends undefined
|
|
26
|
-
? never
|
|
27
|
-
: K
|
|
28
|
-
: never;
|
|
29
|
-
|
|
30
|
-
//
|
|
31
|
-
// HACK:
|
|
32
|
-
// These weird conditionals test whether TypeScript is configured with the
|
|
33
|
-
// `strictNullChecks` compiler option. We use these definitions to influence
|
|
34
|
-
// what's considered a "required" vs an "optional" key for the AllowImplicit
|
|
35
|
-
// type.
|
|
36
|
-
//
|
|
37
|
-
// If strictNullChecks is false, then we should not be emitting any `?` fields
|
|
38
|
-
// and consider all fields "required" because everything is optional by default
|
|
39
|
-
// in that mode anyway.
|
|
40
|
-
//
|
|
41
|
-
type NoStrictNullChecks = undefined extends string ? 1 : undefined;
|
|
42
|
-
// ^^^^^^^^^^^^^^^^^^^^^^^^
|
|
43
|
-
type StrictNullChecks = undefined extends string ? undefined : 1;
|
|
44
|
-
// ^^^^^^^^^^^^^^^^^^^^^^^^
|
|
45
|
-
|
|
46
|
-
export type RequiredKeys<T> = keyof Compact<{
|
|
47
|
-
[K in keyof T]: undefined extends T[K] ? NoStrictNullChecks : 1;
|
|
48
|
-
}>;
|
|
49
|
-
|
|
50
|
-
export type OptionalKeys<T> = keyof Compact<{
|
|
51
|
-
[K in keyof T]: undefined extends T[K] ? 1 : StrictNullChecks;
|
|
52
|
-
}>;
|
|
53
|
-
|
|
54
|
-
/**
|
|
55
|
-
* Transforms an object type, by marking all fields that contain "undefined"
|
|
56
|
-
* with a question mark, i.e. allowing implicit-undefineds when
|
|
57
|
-
* explicit-undefined are also allowed.
|
|
58
|
-
*
|
|
59
|
-
* For example, if:
|
|
60
|
-
*
|
|
61
|
-
* type User = {
|
|
62
|
-
* name: string;
|
|
63
|
-
* age: number | null | undefined;
|
|
64
|
-
* }
|
|
65
|
-
*
|
|
66
|
-
* Then AllowImplicit<User> will become equivalent to:
|
|
67
|
-
*
|
|
68
|
-
* {
|
|
69
|
-
* name: string;
|
|
70
|
-
* age?: number | null;
|
|
71
|
-
* ^
|
|
72
|
-
* Note the question mark
|
|
73
|
-
* }
|
|
74
|
-
*/
|
|
75
|
-
type AllowImplicit<T> = { [K in RequiredKeys<T>]-?: T[K] } & {
|
|
76
|
-
[K in OptionalKeys<T>]+?: Exclude<T[K], undefined>;
|
|
77
|
-
};
|
|
78
|
-
|
|
79
|
-
export { AllowImplicit };
|
package/ts/_types.d.ts
DELETED
|
@@ -1,16 +0,0 @@
|
|
|
1
|
-
import { Annotation } from './annotate';
|
|
2
|
-
import { Result } from './result';
|
|
3
|
-
|
|
4
|
-
export type Scalar = string | number | boolean | symbol | undefined | null;
|
|
5
|
-
|
|
6
|
-
export interface Guard<T> {
|
|
7
|
-
(blob: unknown): T;
|
|
8
|
-
}
|
|
9
|
-
export type Predicate<T> = (value: T) => boolean;
|
|
10
|
-
export type DecodeResult<T> = Result<T, Annotation>;
|
|
11
|
-
export interface Decoder<T, F = unknown> {
|
|
12
|
-
(blob: F): DecodeResult<T>;
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
export type DecoderType<T> = T extends Decoder<infer V> ? V : never;
|
|
16
|
-
export type GuardType<T> = T extends Guard<infer V> ? V : never;
|
package/ts/_utils.d.ts
DELETED
|
@@ -1,13 +0,0 @@
|
|
|
1
|
-
import { Decoder } from './_types';
|
|
2
|
-
|
|
3
|
-
export function isDate(value: unknown): boolean;
|
|
4
|
-
export function map<T, V>(decoder: Decoder<T>, mapper: (value: T) => V): Decoder<V>;
|
|
5
|
-
export function compose<T, V>(decoder: Decoder<T>, next: Decoder<V, T>): Decoder<V>;
|
|
6
|
-
export function predicate<T extends F, F = unknown>(
|
|
7
|
-
predicate: (value: F) => value is T,
|
|
8
|
-
msg: string,
|
|
9
|
-
): Decoder<T, F>;
|
|
10
|
-
export function predicate<T>(
|
|
11
|
-
predicate: (value: T) => boolean,
|
|
12
|
-
msg: string,
|
|
13
|
-
): Decoder<T, T>;
|