decoders 1.25.5 → 2.0.0-beta10
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 +53 -3
- package/Decoder.d.ts +27 -0
- package/Decoder.js +199 -0
- package/Decoder.js.flow +224 -0
- package/Decoder.mjs +192 -0
- package/NotSupportedTSVersion.d.ts +1 -0
- package/README.md +105 -961
- package/_utils.d.ts +9 -0
- package/_utils.js +102 -0
- package/_utils.js.flow +93 -0
- package/_utils.mjs +89 -0
- package/annotate.d.ts +62 -0
- package/annotate.js +161 -0
- package/annotate.js.flow +218 -0
- package/annotate.mjs +144 -0
- package/format.d.ts +4 -0
- package/format.js +151 -0
- package/format.js.flow +126 -0
- package/format.mjs +140 -0
- package/index.d.ts +38 -36
- package/index.js +69 -376
- package/index.js.flow +39 -51
- package/index.mjs +11 -0
- package/{helpers.d.ts → lib/_helpers.d.ts} +0 -0
- package/lib/arrays.d.ts +37 -0
- package/lib/arrays.js +138 -0
- package/lib/arrays.js.flow +138 -0
- package/lib/arrays.mjs +123 -0
- package/lib/basics.d.ts +14 -0
- package/lib/basics.js +109 -0
- package/lib/basics.js.flow +85 -0
- package/lib/basics.mjs +85 -0
- package/{boolean.d.ts → lib/booleans.d.ts} +1 -1
- package/lib/booleans.js +35 -0
- package/lib/booleans.js.flow +22 -0
- package/lib/booleans.mjs +25 -0
- package/{date.d.ts → lib/dates.d.ts} +1 -1
- package/lib/dates.js +44 -0
- package/lib/dates.js.flow +40 -0
- package/lib/dates.mjs +34 -0
- package/{json.d.ts → lib/json.d.ts} +1 -1
- package/lib/json.js +55 -0
- package/lib/json.js.flow +50 -0
- package/lib/json.mjs +40 -0
- package/{number.d.ts → lib/numbers.d.ts} +2 -1
- package/lib/numbers.js +52 -0
- package/lib/numbers.js.flow +49 -0
- package/lib/numbers.mjs +42 -0
- package/{object.d.ts → lib/objects.d.ts} +11 -6
- package/lib/objects.js +240 -0
- package/lib/objects.js.flow +246 -0
- package/lib/objects.mjs +223 -0
- package/lib/strings.d.ts +13 -0
- package/lib/strings.js +101 -0
- package/lib/strings.js.flow +90 -0
- package/lib/strings.mjs +82 -0
- package/lib/unions.d.ts +78 -0
- package/lib/unions.js +161 -0
- package/lib/unions.js.flow +158 -0
- package/lib/unions.mjs +145 -0
- package/lib/utilities.d.ts +10 -0
- package/lib/utilities.js +94 -0
- package/lib/utilities.js.flow +84 -0
- package/lib/utilities.mjs +79 -0
- package/package.json +78 -23
- package/result.d.ts +16 -0
- package/result.js +34 -0
- package/result.js.flow +26 -0
- package/result.mjs +27 -0
- package/array.d.ts +0 -5
- package/array.js +0 -133
- package/array.js.flow +0 -106
- package/boolean.js +0 -42
- package/boolean.js.flow +0 -28
- package/constants.d.ts +0 -11
- package/constants.js +0 -67
- package/constants.js.flow +0 -45
- package/date.js +0 -42
- package/date.js.flow +0 -38
- package/describe.d.ts +0 -3
- package/describe.js +0 -22
- package/describe.js.flow +0 -17
- package/dispatch.d.ts +0 -8
- package/dispatch.js +0 -58
- package/dispatch.js.flow +0 -57
- package/either.d.ts +0 -61
- package/either.js +0 -85
- package/either.js.flow +0 -131
- package/fail.d.ts +0 -3
- 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/instanceOf.d.ts +0 -3
- package/instanceOf.js +0 -17
- package/instanceOf.js.flow +0 -21
- package/json.js +0 -33
- package/json.js.flow +0 -28
- package/lazy.d.ts +0 -3
- package/lazy.js +0 -18
- package/lazy.js.flow +0 -15
- package/mapping.d.ts +0 -4
- package/mapping.js +0 -113
- package/mapping.js.flow +0 -71
- package/number.js +0 -38
- package/number.js.flow +0 -35
- package/object.js +0 -254
- package/object.js.flow +0 -211
- package/optional.d.ts +0 -5
- package/optional.js +0 -52
- package/optional.js.flow +0 -42
- package/string.d.ts +0 -7
- package/string.js +0 -93
- package/string.js.flow +0 -81
- package/tuple.d.ts +0 -30
- package/tuple.js +0 -199
- package/tuple.js.flow +0 -221
- package/types.d.ts +0 -18
- package/types.js +0 -5
- package/types.js.flow +0 -26
- package/utils.d.ts +0 -13
- package/utils.js +0 -70
- package/utils.js.flow +0 -58
package/Decoder.mjs
ADDED
|
@@ -0,0 +1,192 @@
|
|
|
1
|
+
import { annotate } from './annotate.mjs';
|
|
2
|
+
import { formatInline } from './format.mjs';
|
|
3
|
+
import { err as makeErr, ok as makeOk } from './result.mjs';
|
|
4
|
+
|
|
5
|
+
function noThrow(fn) {
|
|
6
|
+
return function (t) {
|
|
7
|
+
try {
|
|
8
|
+
var v = fn(t);
|
|
9
|
+
return makeOk(v);
|
|
10
|
+
} catch (e) {
|
|
11
|
+
return makeErr(annotate(t, e instanceof Error ? e.message : String(e)));
|
|
12
|
+
}
|
|
13
|
+
};
|
|
14
|
+
}
|
|
15
|
+
/**
|
|
16
|
+
* Defines a new `Decoder<T>`, by implementing a custom acceptance function.
|
|
17
|
+
* The function receives three arguments:
|
|
18
|
+
*
|
|
19
|
+
* 1. `blob` - the raw/unknown input (aka your external data)
|
|
20
|
+
* 2. `ok` - Call `ok(value)` to accept the input and return `value`
|
|
21
|
+
* 3. `err` - Call `err(message)` to reject the input and use "message" in the
|
|
22
|
+
* annotation
|
|
23
|
+
*
|
|
24
|
+
* The expected return value should be a `DecodeResult<T>`, which can be
|
|
25
|
+
* obtained by returning the result from the provided `ok` or `err` helper
|
|
26
|
+
* functions.
|
|
27
|
+
*/
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
export function define(decodeFn) {
|
|
31
|
+
/**
|
|
32
|
+
* Validates the raw/untrusted/unknown input and either accepts or rejects
|
|
33
|
+
* it.
|
|
34
|
+
*
|
|
35
|
+
* Contrasted with `.verify()`, calls to `.decode()` will never fail and
|
|
36
|
+
* instead return a result type.
|
|
37
|
+
*/
|
|
38
|
+
function decode(blob) {
|
|
39
|
+
return decodeFn(blob, makeOk, function (msg) {
|
|
40
|
+
return makeErr(typeof msg === 'string' ? annotate(blob, msg) : msg);
|
|
41
|
+
});
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* Verified the (raw/untrusted/unknown) input and either accepts or rejects
|
|
45
|
+
* it. When accepted, returns the decoded `T` value directly. Otherwise
|
|
46
|
+
* fail with a runtime error.
|
|
47
|
+
*/
|
|
48
|
+
|
|
49
|
+
|
|
50
|
+
function verify(blob, formatter) {
|
|
51
|
+
if (formatter === void 0) {
|
|
52
|
+
formatter = formatInline;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
var result = decode(blob);
|
|
56
|
+
|
|
57
|
+
if (result.ok) {
|
|
58
|
+
return result.value;
|
|
59
|
+
} else {
|
|
60
|
+
var _err = new Error('\n' + formatter(result.error));
|
|
61
|
+
|
|
62
|
+
_err.name = 'Decoding error';
|
|
63
|
+
throw _err;
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
/**
|
|
67
|
+
* Accepts any value the given decoder accepts, and on success, will call
|
|
68
|
+
* the given function **on the decoded result**. If the transformation
|
|
69
|
+
* function throws an error, the whole decoder will fail using the error
|
|
70
|
+
* message as the failure reason.
|
|
71
|
+
*/
|
|
72
|
+
|
|
73
|
+
|
|
74
|
+
function transform(transformFn) {
|
|
75
|
+
return then(noThrow(transformFn));
|
|
76
|
+
}
|
|
77
|
+
/**
|
|
78
|
+
* Adds an extra predicate to a decoder. The new decoder is like the
|
|
79
|
+
* original decoder, but only accepts values that also meet the
|
|
80
|
+
* predicate.
|
|
81
|
+
*/
|
|
82
|
+
|
|
83
|
+
|
|
84
|
+
function refine(predicateFn, errmsg) {
|
|
85
|
+
return reject(function (value) {
|
|
86
|
+
return predicateFn(value) ? // Don't reject
|
|
87
|
+
null : // Reject with the given error message
|
|
88
|
+
errmsg;
|
|
89
|
+
});
|
|
90
|
+
}
|
|
91
|
+
/**
|
|
92
|
+
* Chain together the current decoder with another.
|
|
93
|
+
*
|
|
94
|
+
* First, the current decoder must accept the input. If so, it will pass
|
|
95
|
+
* the successfully decoded result to the given ``next`` function to
|
|
96
|
+
* further decide whether or not the value should get accepted or rejected.
|
|
97
|
+
*
|
|
98
|
+
* The argument to `.then()` is a decoding function, just like one you
|
|
99
|
+
* would pass to `define()`. The key difference with `define()` is that
|
|
100
|
+
* `define()` must always assume an ``unknown`` input, whereas with
|
|
101
|
+
* a `.then()` call the provided ``next`` function will receive a ``T`` as
|
|
102
|
+
* its input. This will allow the function to make a stronger assumption
|
|
103
|
+
* about its input.
|
|
104
|
+
*
|
|
105
|
+
* If it helps, you can think of `define(nextFn)` as equivalent to
|
|
106
|
+
* `unknown.then(nextFn)`.
|
|
107
|
+
*
|
|
108
|
+
* This is an advanced, low-level, decoder. It's not recommended to reach
|
|
109
|
+
* for this low-level construct when implementing custom decoders. Most
|
|
110
|
+
* cases can be covered by `.transform()` or `.refine()`.
|
|
111
|
+
*/
|
|
112
|
+
|
|
113
|
+
|
|
114
|
+
function then(next) {
|
|
115
|
+
return define(function (blob, ok, err) {
|
|
116
|
+
var result = decode(blob);
|
|
117
|
+
return result.ok ? next(result.value, ok, err) : result;
|
|
118
|
+
});
|
|
119
|
+
}
|
|
120
|
+
/**
|
|
121
|
+
* Adds an extra predicate to a decoder. The new decoder is like the
|
|
122
|
+
* original decoder, but only accepts values that aren't rejected by the
|
|
123
|
+
* given function.
|
|
124
|
+
*
|
|
125
|
+
* The given function can return `null` to accept the decoded value, or
|
|
126
|
+
* return a specific error message to reject.
|
|
127
|
+
*
|
|
128
|
+
* Unlike `.refine()`, you can use this function to return a dynamic error
|
|
129
|
+
* message.
|
|
130
|
+
*/
|
|
131
|
+
|
|
132
|
+
|
|
133
|
+
function reject(rejectFn) {
|
|
134
|
+
return then(function (value, ok, err) {
|
|
135
|
+
var errmsg = rejectFn(value);
|
|
136
|
+
return errmsg === null ? ok(value) : err(typeof errmsg === 'string' ? annotate(value, errmsg) : errmsg);
|
|
137
|
+
});
|
|
138
|
+
}
|
|
139
|
+
/**
|
|
140
|
+
* Uses the given decoder, but will use an alternative error message in
|
|
141
|
+
* case it rejects. This can be used to simplify or shorten otherwise
|
|
142
|
+
* long or low-level/technical errors.
|
|
143
|
+
*/
|
|
144
|
+
|
|
145
|
+
|
|
146
|
+
function describe(message) {
|
|
147
|
+
return define(function (blob, _, err) {
|
|
148
|
+
// Decode using the given decoder...
|
|
149
|
+
var result = decode(blob);
|
|
150
|
+
|
|
151
|
+
if (result.ok) {
|
|
152
|
+
return result;
|
|
153
|
+
} else {
|
|
154
|
+
// ...but in case of error, annotate this with the custom given
|
|
155
|
+
// message instead
|
|
156
|
+
return err(annotate(result.error, message));
|
|
157
|
+
}
|
|
158
|
+
});
|
|
159
|
+
}
|
|
160
|
+
/**
|
|
161
|
+
* WARNING: This is an EXPERIMENTAL API that will likely change in the
|
|
162
|
+
* future. Please DO NOT rely on it.
|
|
163
|
+
*
|
|
164
|
+
* Chain together the current decoder with another, but also pass along
|
|
165
|
+
* the original input.
|
|
166
|
+
*
|
|
167
|
+
* This is like `.then()`, but instead of this function receiving just
|
|
168
|
+
* the decoded result ``T``, it also receives the original input.
|
|
169
|
+
*
|
|
170
|
+
* This is an advanced, low-level, decoder.
|
|
171
|
+
*/
|
|
172
|
+
|
|
173
|
+
|
|
174
|
+
function peek_UNSTABLE(next) {
|
|
175
|
+
return define(function (blob, ok, err) {
|
|
176
|
+
var result = decode(blob);
|
|
177
|
+
return result.ok ? next([blob, result.value], ok, err) : result;
|
|
178
|
+
});
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
return Object.freeze({
|
|
182
|
+
verify: verify,
|
|
183
|
+
decode: decode,
|
|
184
|
+
transform: transform,
|
|
185
|
+
refine: refine,
|
|
186
|
+
reject: reject,
|
|
187
|
+
describe: describe,
|
|
188
|
+
then: then,
|
|
189
|
+
// EXPERIMENTAL - please DO NOT rely on this method
|
|
190
|
+
peek_UNSTABLE: peek_UNSTABLE
|
|
191
|
+
});
|
|
192
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
"Package `decoders` requires TypeScript >= 4.1.0"
|