decoders 1.26.0-beta2 → 2.0.0-beta3
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 +21 -5
- package/_esm/_guard.js +15 -0
- package/_esm/_guard.js.flow +20 -0
- package/_esm/_types.js +0 -0
- package/_esm/_types.js.flow +20 -0
- package/_esm/_utils.js +93 -0
- package/_esm/_utils.js.flow +97 -0
- package/_esm/annotate.js +144 -0
- package/_esm/annotate.js.flow +218 -0
- package/_esm/core/array.js +91 -0
- package/{cjs → _esm/core}/array.js.flow +22 -25
- package/_esm/core/boolean.js +28 -0
- package/{cjs → _esm/core}/boolean.js.flow +8 -7
- package/_esm/core/composition.js +42 -0
- package/{cjs/utils.js.flow → _esm/core/composition.js.flow} +7 -22
- package/_esm/core/constants.js +46 -0
- package/{cjs → _esm/core}/constants.js.flow +13 -12
- package/_esm/core/date.js +28 -0
- package/{cjs → _esm/core}/date.js.flow +9 -7
- package/{cjs → _esm/core}/describe.js +5 -11
- package/{cjs → _esm/core}/describe.js.flow +4 -4
- package/{cjs → _esm/core}/dispatch.js +8 -15
- package/{cjs → _esm/core}/dispatch.js.flow +6 -5
- package/_esm/core/either.js +90 -0
- package/{cjs → _esm/core}/either.js.flow +55 -35
- package/_esm/core/fail.js +11 -0
- package/_esm/core/fail.js.flow +12 -0
- package/_esm/core/instanceOf.js +8 -0
- package/{cjs → _esm/core}/instanceOf.js.flow +7 -8
- package/_esm/core/json.js +15 -0
- package/{cjs → _esm/core}/json.js.flow +4 -4
- package/_esm/core/lazy.js +11 -0
- package/{cjs → _esm/core}/lazy.js.flow +1 -1
- package/_esm/core/mapping.js +54 -0
- package/_esm/core/mapping.js.flow +54 -0
- package/_esm/core/number.js +25 -0
- package/{cjs → _esm/core}/number.js.flow +10 -11
- package/_esm/core/object.js +175 -0
- package/{cjs → _esm/core}/object.js.flow +44 -52
- package/_esm/core/optional.js +38 -0
- package/{cjs → _esm/core}/optional.js.flow +6 -7
- package/{cjs → _esm/core}/string.js +18 -35
- package/{cjs → _esm/core}/string.js.flow +13 -12
- package/_esm/core/tuple.js +155 -0
- package/{cjs → _esm/core}/tuple.js.flow +62 -63
- package/_esm/format/index.js +2 -0
- package/_esm/format/index.js.flow +4 -0
- package/_esm/format/inline.js +137 -0
- package/_esm/format/inline.js.flow +122 -0
- package/_esm/format/short.js +4 -0
- package/_esm/format/short.js.flow +8 -0
- package/_esm/index.js +37 -0
- package/{cjs → _esm}/index.js.flow +31 -24
- package/_esm/result.js +139 -0
- package/_esm/result.js.flow +166 -0
- package/_guard.js +26 -0
- package/_guard.js.flow +20 -0
- package/_types.js +1 -0
- package/_types.js.flow +20 -0
- package/_utils.js +108 -0
- package/_utils.js.flow +97 -0
- package/annotate.js +161 -0
- package/annotate.js.flow +218 -0
- package/core/array.js +108 -0
- package/core/array.js.flow +103 -0
- package/core/boolean.js +44 -0
- package/core/boolean.js.flow +29 -0
- package/core/composition.js +56 -0
- package/core/composition.js.flow +43 -0
- package/core/constants.js +69 -0
- package/core/constants.js.flow +46 -0
- package/core/date.js +46 -0
- package/core/date.js.flow +40 -0
- package/core/describe.js +26 -0
- package/core/describe.js.flow +17 -0
- package/core/dispatch.js +62 -0
- package/core/dispatch.js.flow +58 -0
- package/core/either.js +117 -0
- package/core/either.js.flow +151 -0
- package/core/fail.js +21 -0
- package/core/fail.js.flow +12 -0
- package/core/instanceOf.js +19 -0
- package/core/instanceOf.js.flow +20 -0
- package/{cjs → core}/json.js +3 -5
- package/core/json.js.flow +28 -0
- package/{cjs → core}/lazy.js +1 -3
- package/core/lazy.js.flow +15 -0
- package/core/mapping.js +67 -0
- package/core/mapping.js.flow +54 -0
- package/core/number.js +40 -0
- package/core/number.js.flow +34 -0
- package/core/object.js +194 -0
- package/core/object.js.flow +203 -0
- package/core/optional.js +54 -0
- package/core/optional.js.flow +41 -0
- package/core/string.js +98 -0
- package/core/string.js.flow +82 -0
- package/core/tuple.js +173 -0
- package/core/tuple.js.flow +220 -0
- package/format/index.js +12 -0
- package/format/index.js.flow +4 -0
- package/format/inline.js +146 -0
- package/format/inline.js.flow +122 -0
- package/format/short.js +10 -0
- package/format/short.js.flow +8 -0
- package/index.js +120 -0
- package/index.js.flow +63 -0
- package/package.json +3 -11
- package/result.js +172 -0
- package/result.js.flow +166 -0
- package/cjs/array.js +0 -133
- package/cjs/boolean.js +0 -42
- package/cjs/constants.js +0 -67
- package/cjs/date.js +0 -42
- package/cjs/either.js +0 -85
- package/cjs/fail.js +0 -19
- package/cjs/fail.js.flow +0 -13
- package/cjs/guard.js +0 -30
- package/cjs/guard.js.flow +0 -36
- package/cjs/index.js +0 -397
- package/cjs/instanceOf.js +0 -17
- package/cjs/mapping.js +0 -113
- package/cjs/mapping.js.flow +0 -71
- package/cjs/number.js +0 -38
- package/cjs/object.js +0 -254
- package/cjs/optional.js +0 -52
- package/cjs/tuple.js +0 -199
- package/cjs/types.js +0 -3
- package/cjs/types.js.flow +0 -26
- package/cjs/utils.js +0 -70
- package/es/index.js +0 -1039
- 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/guard.d.ts +0 -7
- package/ts/helpers.d.ts +0 -79
- package/ts/index.d.ts +0 -38
- 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/string.d.ts +0 -7
- package/ts/tuple.d.ts +0 -30
- package/ts/types.d.ts +0 -18
- package/ts/utils.d.ts +0 -13
package/cjs/object.js
DELETED
|
@@ -1,254 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
|
|
3
|
-
Object.defineProperty(exports, "__esModule", {
|
|
4
|
-
value: true
|
|
5
|
-
});
|
|
6
|
-
exports.exact = exact;
|
|
7
|
-
exports.inexact = inexact;
|
|
8
|
-
exports.object = object;
|
|
9
|
-
exports.pojo = void 0;
|
|
10
|
-
|
|
11
|
-
var _debrief = require("debrief");
|
|
12
|
-
|
|
13
|
-
var _lemons = require("lemons");
|
|
14
|
-
|
|
15
|
-
var _utils = require("./utils");
|
|
16
|
-
|
|
17
|
-
function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) { symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); } keys.push.apply(keys, symbols); } return keys; }
|
|
18
|
-
|
|
19
|
-
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }
|
|
20
|
-
|
|
21
|
-
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
|
|
22
|
-
|
|
23
|
-
function _createForOfIteratorHelper(o, allowArrayLike) { var it = typeof Symbol !== "undefined" && o[Symbol.iterator] || o["@@iterator"]; if (!it) { if (Array.isArray(o) || (it = _unsupportedIterableToArray(o)) || allowArrayLike && o && typeof o.length === "number") { if (it) o = it; var i = 0; var F = function F() {}; return { s: F, n: function n() { if (i >= o.length) return { done: true }; return { done: false, value: o[i++] }; }, e: function e(_e) { throw _e; }, f: F }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } var normalCompletion = true, didErr = false, err; return { s: function s() { it = it.call(o); }, n: function n() { var step = it.next(); normalCompletion = step.done; return step; }, e: function e(_e2) { didErr = true; err = _e2; }, f: function f() { try { if (!normalCompletion && it["return"] != null) it["return"](); } finally { if (didErr) throw err; } } }; }
|
|
24
|
-
|
|
25
|
-
function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
|
|
26
|
-
|
|
27
|
-
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; }
|
|
28
|
-
|
|
29
|
-
function _typeof(obj) { "@babel/helpers - typeof"; if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }
|
|
30
|
-
|
|
31
|
-
function isPojo(o) {
|
|
32
|
-
return o !== null && o !== undefined && _typeof(o) === 'object' && // This still seems to be the only reliable way to determine whether
|
|
33
|
-
// something is a pojo... ¯\_(ツ)_/¯
|
|
34
|
-
// $FlowFixMe[method-unbinding]
|
|
35
|
-
Object.prototype.toString.call(o) === '[object Object]';
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
function subtract(xs, ys) {
|
|
39
|
-
var result = new Set();
|
|
40
|
-
|
|
41
|
-
var _iterator = _createForOfIteratorHelper(xs),
|
|
42
|
-
_step;
|
|
43
|
-
|
|
44
|
-
try {
|
|
45
|
-
for (_iterator.s(); !(_step = _iterator.n()).done;) {
|
|
46
|
-
var x = _step.value;
|
|
47
|
-
|
|
48
|
-
if (!ys.has(x)) {
|
|
49
|
-
result.add(x);
|
|
50
|
-
}
|
|
51
|
-
}
|
|
52
|
-
} catch (err) {
|
|
53
|
-
_iterator.e(err);
|
|
54
|
-
} finally {
|
|
55
|
-
_iterator.f();
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
return result;
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
var pojo = function pojo(blob) {
|
|
62
|
-
return isPojo(blob) ? (0, _lemons.Ok)( // NOTE:
|
|
63
|
-
// Since Flow 0.98, typeof o === 'object' refines to
|
|
64
|
-
// {| +[string]: mixed |}
|
|
65
|
-
// instead of
|
|
66
|
-
// {| [string]: mixed |}
|
|
67
|
-
//
|
|
68
|
-
// For rationale, see https://github.com/facebook/flow/issues/7685.
|
|
69
|
-
// In this case, we don't want to output a read-only version of
|
|
70
|
-
// the object because it's up to the user of decoders to
|
|
71
|
-
// determine what they want to do with the decoded output. If they
|
|
72
|
-
// want to write items into the array, that's fine! The fastest
|
|
73
|
-
// way to turn a read-only Object to a writeable one in ES6 seems
|
|
74
|
-
// to be to use object-spread. (Going off this benchmark:
|
|
75
|
-
// https://thecodebarbarian.com/object-assign-vs-object-spread.html)
|
|
76
|
-
_objectSpread({}, blob)) : (0, _lemons.Err)((0, _debrief.annotate)(blob, 'Must be an object'));
|
|
77
|
-
};
|
|
78
|
-
/**
|
|
79
|
-
* Given a mapping of fields-to-decoders, builds a decoder for an object type.
|
|
80
|
-
*
|
|
81
|
-
* For example, given decoders for a number and a string, we can construct an
|
|
82
|
-
* "object description" like so:
|
|
83
|
-
*
|
|
84
|
-
* { id: number, name: string }
|
|
85
|
-
*
|
|
86
|
-
* Which is of type:
|
|
87
|
-
*
|
|
88
|
-
* { id: Decoder<number>, name: Decoder<string> }
|
|
89
|
-
*
|
|
90
|
-
* Passing this to object() will produce the following return type:
|
|
91
|
-
*
|
|
92
|
-
* Decoder<{ id: number, name: string }>
|
|
93
|
-
*
|
|
94
|
-
* Put simply: it'll "peel off" all of the nested Decoders, puts them together
|
|
95
|
-
* in an object, and wraps it in a Decoder<...>.
|
|
96
|
-
*/
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
exports.pojo = pojo;
|
|
100
|
-
|
|
101
|
-
function object(mapping) {
|
|
102
|
-
var known = new Set(Object.keys(mapping));
|
|
103
|
-
return (0, _utils.compose)(pojo, function (blob) {
|
|
104
|
-
var actual = new Set(Object.keys(blob)); // At this point, "missing" will also include all fields that may
|
|
105
|
-
// validly be optional. We'll let the underlying decoder decide and
|
|
106
|
-
// remove the key from this missing set if the decoder accepts the
|
|
107
|
-
// value.
|
|
108
|
-
|
|
109
|
-
var missing = subtract(known, actual);
|
|
110
|
-
var record = {};
|
|
111
|
-
|
|
112
|
-
var fieldErrors = _objectSpread({}, null); // NOTE: We're using .keys() here over .entries(), since .entries()
|
|
113
|
-
// will type the value part as "mixed"
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
for (var _i = 0, _Object$keys = Object.keys(mapping); _i < _Object$keys.length; _i++) {
|
|
117
|
-
var _key = _Object$keys[_i];
|
|
118
|
-
var decoder = mapping[_key];
|
|
119
|
-
var rawValue = blob[_key];
|
|
120
|
-
var result = decoder(rawValue);
|
|
121
|
-
|
|
122
|
-
try {
|
|
123
|
-
var value = result.unwrap();
|
|
124
|
-
|
|
125
|
-
if (value !== undefined) {
|
|
126
|
-
record[_key] = value;
|
|
127
|
-
} // If this succeeded, remove the key from the missing keys
|
|
128
|
-
// tracker
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
missing["delete"](_key);
|
|
132
|
-
} catch (ann) {
|
|
133
|
-
/* istanbul ignore next */
|
|
134
|
-
if (!(0, _debrief.isAnnotation)(ann)) {
|
|
135
|
-
throw ann;
|
|
136
|
-
} // Keep track of the annotation, but don't return just yet. We
|
|
137
|
-
// want to collect more error information.
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
if (rawValue === undefined) {
|
|
141
|
-
// Explicitly add it to the missing set if the value is
|
|
142
|
-
// undefined. This covers explicit undefineds to be
|
|
143
|
-
// treated the same as implicit undefineds (aka missing
|
|
144
|
-
// keys).
|
|
145
|
-
missing.add(_key);
|
|
146
|
-
} else {
|
|
147
|
-
fieldErrors[_key] = ann;
|
|
148
|
-
}
|
|
149
|
-
}
|
|
150
|
-
} // Deal with errors now. There are two classes of errors we want to
|
|
151
|
-
// report. First of all, we want to report any inline errors in this
|
|
152
|
-
// object. Lastly, any fields that are missing should be annotated on
|
|
153
|
-
// the outer object itself.
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
var fieldsWithErrors = Object.keys(fieldErrors);
|
|
157
|
-
|
|
158
|
-
if (fieldsWithErrors.length > 0 || missing.size > 0) {
|
|
159
|
-
var err;
|
|
160
|
-
|
|
161
|
-
if (fieldsWithErrors.length > 0) {
|
|
162
|
-
var errorlist = fieldsWithErrors.map(function (k) {
|
|
163
|
-
return [k, fieldErrors[k]];
|
|
164
|
-
});
|
|
165
|
-
err = (0, _debrief.annotateFields)(blob, errorlist);
|
|
166
|
-
} else {
|
|
167
|
-
err = (0, _debrief.annotate)(blob);
|
|
168
|
-
}
|
|
169
|
-
|
|
170
|
-
if (missing.size > 0) {
|
|
171
|
-
var errMsg = Array.from(missing).map(function (key) {
|
|
172
|
-
return "\"".concat(key, "\"");
|
|
173
|
-
}).join(', ');
|
|
174
|
-
var pluralized = missing.size > 1 ? 'keys' : 'key';
|
|
175
|
-
err = (0, _debrief.annotate)(err, "Missing ".concat(pluralized, ": ").concat(errMsg));
|
|
176
|
-
}
|
|
177
|
-
|
|
178
|
-
return (0, _lemons.Err)(err);
|
|
179
|
-
}
|
|
180
|
-
|
|
181
|
-
return (0, _lemons.Ok)(record);
|
|
182
|
-
});
|
|
183
|
-
}
|
|
184
|
-
|
|
185
|
-
function exact(mapping) {
|
|
186
|
-
// Check the inputted object for any superfluous keys
|
|
187
|
-
var allowed = new Set(Object.keys(mapping));
|
|
188
|
-
var checked = (0, _utils.compose)(pojo, function (blob) {
|
|
189
|
-
var actual = new Set(Object.keys(blob));
|
|
190
|
-
var superfluous = subtract(actual, allowed);
|
|
191
|
-
|
|
192
|
-
if (superfluous.size > 0) {
|
|
193
|
-
return (0, _lemons.Err)((0, _debrief.annotate)(blob, "Superfluous keys: ".concat(Array.from(superfluous).join(', '))));
|
|
194
|
-
}
|
|
195
|
-
|
|
196
|
-
return (0, _lemons.Ok)(blob);
|
|
197
|
-
}); // Defer to the "object" decoder for doing the real decoding work. Since
|
|
198
|
-
// we made sure there are no superfluous keys in this structure, it's now
|
|
199
|
-
// safe to force-cast it to an $Exact<> type.
|
|
200
|
-
|
|
201
|
-
var decoder = object(mapping);
|
|
202
|
-
return (0, _utils.compose)(checked, decoder);
|
|
203
|
-
}
|
|
204
|
-
|
|
205
|
-
function inexact(mapping) {
|
|
206
|
-
return (0, _utils.compose)(pojo, function (blob) {
|
|
207
|
-
var allkeys = new Set(Object.keys(blob));
|
|
208
|
-
var decoder = (0, _utils.map)(object(mapping), function (safepart) {
|
|
209
|
-
var safekeys = new Set(Object.keys(mapping)); // To account for hard-coded keys that aren't part of the input
|
|
210
|
-
|
|
211
|
-
var _iterator2 = _createForOfIteratorHelper(safekeys),
|
|
212
|
-
_step2;
|
|
213
|
-
|
|
214
|
-
try {
|
|
215
|
-
for (_iterator2.s(); !(_step2 = _iterator2.n()).done;) {
|
|
216
|
-
var k = _step2.value;
|
|
217
|
-
allkeys.add(k);
|
|
218
|
-
}
|
|
219
|
-
} catch (err) {
|
|
220
|
-
_iterator2.e(err);
|
|
221
|
-
} finally {
|
|
222
|
-
_iterator2.f();
|
|
223
|
-
}
|
|
224
|
-
|
|
225
|
-
var rv = {};
|
|
226
|
-
|
|
227
|
-
var _iterator3 = _createForOfIteratorHelper(allkeys),
|
|
228
|
-
_step3;
|
|
229
|
-
|
|
230
|
-
try {
|
|
231
|
-
for (_iterator3.s(); !(_step3 = _iterator3.n()).done;) {
|
|
232
|
-
var _k = _step3.value;
|
|
233
|
-
|
|
234
|
-
if (safekeys.has(_k)) {
|
|
235
|
-
var value = safepart[_k];
|
|
236
|
-
|
|
237
|
-
if (value !== undefined) {
|
|
238
|
-
rv[_k] = value;
|
|
239
|
-
}
|
|
240
|
-
} else {
|
|
241
|
-
rv[_k] = blob[_k];
|
|
242
|
-
}
|
|
243
|
-
}
|
|
244
|
-
} catch (err) {
|
|
245
|
-
_iterator3.e(err);
|
|
246
|
-
} finally {
|
|
247
|
-
_iterator3.f();
|
|
248
|
-
}
|
|
249
|
-
|
|
250
|
-
return rv;
|
|
251
|
-
});
|
|
252
|
-
return decoder(blob);
|
|
253
|
-
});
|
|
254
|
-
}
|
package/cjs/optional.js
DELETED
|
@@ -1,52 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
|
|
3
|
-
Object.defineProperty(exports, "__esModule", {
|
|
4
|
-
value: true
|
|
5
|
-
});
|
|
6
|
-
exports.maybe = maybe;
|
|
7
|
-
exports.nullable = nullable;
|
|
8
|
-
exports.optional = optional;
|
|
9
|
-
|
|
10
|
-
var _debrief = require("debrief");
|
|
11
|
-
|
|
12
|
-
var _lemons = require("lemons");
|
|
13
|
-
|
|
14
|
-
var _constants = require("./constants");
|
|
15
|
-
|
|
16
|
-
var _either = require("./either");
|
|
17
|
-
|
|
18
|
-
/**
|
|
19
|
-
* Builds a Decoder that returns Ok for either `undefined` or `T` values,
|
|
20
|
-
* given a Decoder for `T`. Err otherwise.
|
|
21
|
-
*/
|
|
22
|
-
function optional(decoder) {
|
|
23
|
-
return (0, _either.either)(_constants.undefined_, decoder);
|
|
24
|
-
}
|
|
25
|
-
/**
|
|
26
|
-
* Builds a Decoder that returns Ok for either `null` or `T` values,
|
|
27
|
-
* given a Decoder for `T`. Err otherwise.
|
|
28
|
-
*/
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
function nullable(decoder) {
|
|
32
|
-
return (0, _either.either)(_constants.null_, decoder);
|
|
33
|
-
}
|
|
34
|
-
/**
|
|
35
|
-
* Decoder that only returns Ok for `null` or `undefined` inputs.
|
|
36
|
-
* This is effectively equivalent to either(null_, undefined_), but combines
|
|
37
|
-
* their error message output into a single line for convenience.
|
|
38
|
-
*/
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
var undefined_or_null = function undefined_or_null(blob) {
|
|
42
|
-
return blob === undefined || blob === null ? (0, _lemons.Ok)(blob) : // Combine error message into a single line
|
|
43
|
-
(0, _lemons.Err)((0, _debrief.annotate)(blob, 'Must be undefined or null'));
|
|
44
|
-
};
|
|
45
|
-
/**
|
|
46
|
-
* Decoder that only returns Ok for `null` or `undefined` inputs.
|
|
47
|
-
*/
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
function maybe(decoder) {
|
|
51
|
-
return (0, _either.either)(undefined_or_null, decoder);
|
|
52
|
-
}
|
package/cjs/tuple.js
DELETED
|
@@ -1,199 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
|
|
3
|
-
Object.defineProperty(exports, "__esModule", {
|
|
4
|
-
value: true
|
|
5
|
-
});
|
|
6
|
-
exports.tuple1 = tuple1;
|
|
7
|
-
exports.tuple2 = tuple2;
|
|
8
|
-
exports.tuple3 = tuple3;
|
|
9
|
-
exports.tuple4 = tuple4;
|
|
10
|
-
exports.tuple5 = tuple5;
|
|
11
|
-
exports.tuple6 = tuple6;
|
|
12
|
-
|
|
13
|
-
var _debrief = require("debrief");
|
|
14
|
-
|
|
15
|
-
var _lemons = require("lemons");
|
|
16
|
-
|
|
17
|
-
var _array = require("./array");
|
|
18
|
-
|
|
19
|
-
var _utils = require("./utils");
|
|
20
|
-
|
|
21
|
-
function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }
|
|
22
|
-
|
|
23
|
-
function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
|
|
24
|
-
|
|
25
|
-
function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
|
|
26
|
-
|
|
27
|
-
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; }
|
|
28
|
-
|
|
29
|
-
function _iterableToArrayLimit(arr, i) { var _i = arr == null ? null : typeof Symbol !== "undefined" && arr[Symbol.iterator] || arr["@@iterator"]; if (_i == null) return; var _arr = []; var _n = true; var _d = false; var _s, _e; try { for (_i = _i.call(arr); !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"] != null) _i["return"](); } finally { if (_d) throw _e; } } return _arr; }
|
|
30
|
-
|
|
31
|
-
function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
|
|
32
|
-
|
|
33
|
-
var ntuple = function ntuple(n) {
|
|
34
|
-
return (0, _utils.compose)(_array.poja, (0, _utils.predicate)(function (arr) {
|
|
35
|
-
return arr.length === n;
|
|
36
|
-
}, "Must be a ".concat(n, "-tuple")));
|
|
37
|
-
};
|
|
38
|
-
/**
|
|
39
|
-
* Builds a Decoder that returns Ok for 1-tuple of [T], given a Decoder for T.
|
|
40
|
-
* Err otherwise.
|
|
41
|
-
*/
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
function tuple1(decoder1) {
|
|
45
|
-
return (0, _utils.compose)(ntuple(1), function (blobs) {
|
|
46
|
-
var _blobs = _slicedToArray(blobs, 1),
|
|
47
|
-
blob1 = _blobs[0];
|
|
48
|
-
|
|
49
|
-
var result1 = decoder1(blob1);
|
|
50
|
-
|
|
51
|
-
try {
|
|
52
|
-
return (0, _lemons.Ok)([result1.unwrap()]);
|
|
53
|
-
} catch (e) {
|
|
54
|
-
// If a decoder error has happened while unwrapping all the
|
|
55
|
-
// results, try to construct a good error message
|
|
56
|
-
return (0, _lemons.Err)((0, _debrief.annotate)(result1.errValue()));
|
|
57
|
-
}
|
|
58
|
-
});
|
|
59
|
-
}
|
|
60
|
-
/**
|
|
61
|
-
* Builds a Decoder that returns Ok for 2-tuples of [T1, T2], given Decoders
|
|
62
|
-
* for T1 and T2. Err otherwise.
|
|
63
|
-
*/
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
function tuple2(decoder1, decoder2) {
|
|
67
|
-
return (0, _utils.compose)(ntuple(2), function (blobs) {
|
|
68
|
-
var _blobs2 = _slicedToArray(blobs, 2),
|
|
69
|
-
blob1 = _blobs2[0],
|
|
70
|
-
blob2 = _blobs2[1];
|
|
71
|
-
|
|
72
|
-
var result1 = decoder1(blob1);
|
|
73
|
-
var result2 = decoder2(blob2);
|
|
74
|
-
|
|
75
|
-
try {
|
|
76
|
-
return (0, _lemons.Ok)([result1.unwrap(), result2.unwrap()]);
|
|
77
|
-
} catch (e) {
|
|
78
|
-
// If a decoder error has happened while unwrapping all the
|
|
79
|
-
// results, try to construct a good error message
|
|
80
|
-
return (0, _lemons.Err)((0, _debrief.annotate)([result1.isErr() ? result1.errValue() : result1.value(), result2.isErr() ? result2.errValue() : result2.value()]));
|
|
81
|
-
}
|
|
82
|
-
});
|
|
83
|
-
}
|
|
84
|
-
/**
|
|
85
|
-
* Builds a Decoder that returns Ok for 3-tuples of [T1, T2, T3], given
|
|
86
|
-
* Decoders for T1, T2, and T3. Err otherwise.
|
|
87
|
-
*/
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
function tuple3(decoder1, decoder2, decoder3) {
|
|
91
|
-
return (0, _utils.compose)(ntuple(3), function (blobs) {
|
|
92
|
-
var _blobs3 = _slicedToArray(blobs, 3),
|
|
93
|
-
blob1 = _blobs3[0],
|
|
94
|
-
blob2 = _blobs3[1],
|
|
95
|
-
blob3 = _blobs3[2];
|
|
96
|
-
|
|
97
|
-
var result1 = decoder1(blob1);
|
|
98
|
-
var result2 = decoder2(blob2);
|
|
99
|
-
var result3 = decoder3(blob3);
|
|
100
|
-
|
|
101
|
-
try {
|
|
102
|
-
return (0, _lemons.Ok)([result1.unwrap(), result2.unwrap(), result3.unwrap()]);
|
|
103
|
-
} catch (e) {
|
|
104
|
-
// If a decoder error has happened while unwrapping all the
|
|
105
|
-
// results, try to construct a good error message
|
|
106
|
-
return (0, _lemons.Err)((0, _debrief.annotate)([result1.isErr() ? result1.errValue() : result1.value(), result2.isErr() ? result2.errValue() : result2.value(), result3.isErr() ? result3.errValue() : result3.value()]));
|
|
107
|
-
}
|
|
108
|
-
});
|
|
109
|
-
}
|
|
110
|
-
/**
|
|
111
|
-
* Builds a Decoder that returns Ok for 4-tuples of [T1, T2, T3, T4], given
|
|
112
|
-
* Decoders for T1, T2, T3, and T4. Err otherwise.
|
|
113
|
-
*/
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
function tuple4(decoder1, decoder2, decoder3, decoder4) {
|
|
117
|
-
return (0, _utils.compose)(ntuple(4), function (blobs) {
|
|
118
|
-
var _blobs4 = _slicedToArray(blobs, 4),
|
|
119
|
-
blob1 = _blobs4[0],
|
|
120
|
-
blob2 = _blobs4[1],
|
|
121
|
-
blob3 = _blobs4[2],
|
|
122
|
-
blob4 = _blobs4[3];
|
|
123
|
-
|
|
124
|
-
var result1 = decoder1(blob1);
|
|
125
|
-
var result2 = decoder2(blob2);
|
|
126
|
-
var result3 = decoder3(blob3);
|
|
127
|
-
var result4 = decoder4(blob4);
|
|
128
|
-
|
|
129
|
-
try {
|
|
130
|
-
return (0, _lemons.Ok)([result1.unwrap(), result2.unwrap(), result3.unwrap(), result4.unwrap()]);
|
|
131
|
-
} catch (e) {
|
|
132
|
-
// If a decoder error has happened while unwrapping all the
|
|
133
|
-
// results, try to construct a good error message
|
|
134
|
-
return (0, _lemons.Err)((0, _debrief.annotate)([result1.isErr() ? result1.errValue() : result1.value(), result2.isErr() ? result2.errValue() : result2.value(), result3.isErr() ? result3.errValue() : result3.value(), result4.isErr() ? result4.errValue() : result4.value()]));
|
|
135
|
-
}
|
|
136
|
-
});
|
|
137
|
-
}
|
|
138
|
-
/**
|
|
139
|
-
* Builds a Decoder that returns Ok for 5-tuples of [T1, T2, T3, T4, T5], given
|
|
140
|
-
* Decoders for T1, T2, T3, T4, and T5. Err otherwise.
|
|
141
|
-
*/
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
function tuple5(decoder1, decoder2, decoder3, decoder4, decoder5) {
|
|
145
|
-
return (0, _utils.compose)(ntuple(5), function (blobs) {
|
|
146
|
-
var _blobs5 = _slicedToArray(blobs, 5),
|
|
147
|
-
blob1 = _blobs5[0],
|
|
148
|
-
blob2 = _blobs5[1],
|
|
149
|
-
blob3 = _blobs5[2],
|
|
150
|
-
blob4 = _blobs5[3],
|
|
151
|
-
blob5 = _blobs5[4];
|
|
152
|
-
|
|
153
|
-
var result1 = decoder1(blob1);
|
|
154
|
-
var result2 = decoder2(blob2);
|
|
155
|
-
var result3 = decoder3(blob3);
|
|
156
|
-
var result4 = decoder4(blob4);
|
|
157
|
-
var result5 = decoder5(blob5);
|
|
158
|
-
|
|
159
|
-
try {
|
|
160
|
-
return (0, _lemons.Ok)([result1.unwrap(), result2.unwrap(), result3.unwrap(), result4.unwrap(), result5.unwrap()]);
|
|
161
|
-
} catch (e) {
|
|
162
|
-
// If a decoder error has happened while unwrapping all the
|
|
163
|
-
// results, try to construct a good error message
|
|
164
|
-
return (0, _lemons.Err)((0, _debrief.annotate)([result1.isErr() ? result1.errValue() : result1.value(), result2.isErr() ? result2.errValue() : result2.value(), result3.isErr() ? result3.errValue() : result3.value(), result4.isErr() ? result4.errValue() : result4.value(), result5.isErr() ? result5.errValue() : result5.value()]));
|
|
165
|
-
}
|
|
166
|
-
});
|
|
167
|
-
}
|
|
168
|
-
/**
|
|
169
|
-
* Builds a Decoder that returns Ok for 5-tuples of [T1, T2, T3, T4, T5], given
|
|
170
|
-
* Decoders for T1, T2, T3, T4, T5, and T6. Err otherwise.
|
|
171
|
-
*/
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
function tuple6(decoder1, decoder2, decoder3, decoder4, decoder5, decoder6) {
|
|
175
|
-
return (0, _utils.compose)(ntuple(6), function (blobs) {
|
|
176
|
-
var _blobs6 = _slicedToArray(blobs, 6),
|
|
177
|
-
blob1 = _blobs6[0],
|
|
178
|
-
blob2 = _blobs6[1],
|
|
179
|
-
blob3 = _blobs6[2],
|
|
180
|
-
blob4 = _blobs6[3],
|
|
181
|
-
blob5 = _blobs6[4],
|
|
182
|
-
blob6 = _blobs6[5];
|
|
183
|
-
|
|
184
|
-
var result1 = decoder1(blob1);
|
|
185
|
-
var result2 = decoder2(blob2);
|
|
186
|
-
var result3 = decoder3(blob3);
|
|
187
|
-
var result4 = decoder4(blob4);
|
|
188
|
-
var result5 = decoder5(blob5);
|
|
189
|
-
var result6 = decoder6(blob6);
|
|
190
|
-
|
|
191
|
-
try {
|
|
192
|
-
return (0, _lemons.Ok)([result1.unwrap(), result2.unwrap(), result3.unwrap(), result4.unwrap(), result5.unwrap(), result6.unwrap()]);
|
|
193
|
-
} catch (e) {
|
|
194
|
-
// If a decoder error has happened while unwrapping all the
|
|
195
|
-
// results, try to construct a good error message
|
|
196
|
-
return (0, _lemons.Err)((0, _debrief.annotate)([result1.isErr() ? result1.errValue() : result1.value(), result2.isErr() ? result2.errValue() : result2.value(), result3.isErr() ? result3.errValue() : result3.value(), result4.isErr() ? result4.errValue() : result4.value(), result5.isErr() ? result5.errValue() : result5.value(), result6.isErr() ? result6.errValue() : result6.value()]));
|
|
197
|
-
}
|
|
198
|
-
});
|
|
199
|
-
}
|
package/cjs/types.js
DELETED
package/cjs/types.js.flow
DELETED
|
@@ -1,26 +0,0 @@
|
|
|
1
|
-
// @flow strict
|
|
2
|
-
|
|
3
|
-
import type { Annotation } from 'debrief';
|
|
4
|
-
import { Result } from 'lemons';
|
|
5
|
-
|
|
6
|
-
export type Scalar = string | number | boolean | symbol | void | null;
|
|
7
|
-
|
|
8
|
-
// NOTE:
|
|
9
|
-
// Normally, we should not be discarding Flow warnings about the use of the
|
|
10
|
-
// "any" type. But in the case of decoders, it's the very purpose of the
|
|
11
|
-
// library to accept *anything* as input. To avoid suppressing the "any"
|
|
12
|
-
// warnings everywhere throughout this library, we'll suppress it here once,
|
|
13
|
-
// and use this re-aliased version of "any" elsewhere.
|
|
14
|
-
|
|
15
|
-
export type Guard<T> = (mixed) => T;
|
|
16
|
-
export type Predicate<T> = (T) => boolean;
|
|
17
|
-
export type DecodeResult<T> = Result<Annotation, T>;
|
|
18
|
-
export type Decoder<T, F = mixed> = (F) => DecodeResult<T>;
|
|
19
|
-
|
|
20
|
-
/**
|
|
21
|
-
* A "type function" which informs Flow about how a type will be modified at runtime.
|
|
22
|
-
* Read this as "given a Guard of type T, I can produce a value of type T". This
|
|
23
|
-
* definition helps construct $ObjMap types.
|
|
24
|
-
*/
|
|
25
|
-
export type $DecoderType = <T>(Decoder<T>) => T;
|
|
26
|
-
export type $GuardType = <T>(Guard<T>) => T;
|
package/cjs/utils.js
DELETED
|
@@ -1,70 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
|
|
3
|
-
Object.defineProperty(exports, "__esModule", {
|
|
4
|
-
value: true
|
|
5
|
-
});
|
|
6
|
-
exports.compose = compose;
|
|
7
|
-
exports.isDate = void 0;
|
|
8
|
-
exports.map = map;
|
|
9
|
-
exports.predicate = predicate;
|
|
10
|
-
|
|
11
|
-
var _debrief = require("debrief");
|
|
12
|
-
|
|
13
|
-
var _lemons = require("lemons");
|
|
14
|
-
|
|
15
|
-
/**
|
|
16
|
-
* `x instanceof Date` checks are unreliable across stack frames (that information
|
|
17
|
-
* might get lost by the JS runtime), so we'll have to reside to more runtime
|
|
18
|
-
* inspection checks.
|
|
19
|
-
*
|
|
20
|
-
* Taken from https://stackoverflow.com/a/44198641
|
|
21
|
-
*/
|
|
22
|
-
var isDate = function isDate(value) {
|
|
23
|
-
return value !== undefined && value !== null && // $FlowFixMe[method-unbinding]
|
|
24
|
-
Object.prototype.toString.call(value) === '[object Date]' && !isNaN(value);
|
|
25
|
-
};
|
|
26
|
-
/**
|
|
27
|
-
* Given a decoder T and a mapping function from T's to V's, returns a decoder
|
|
28
|
-
* for V's. This is useful to change the original input data.
|
|
29
|
-
*/
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
exports.isDate = isDate;
|
|
33
|
-
|
|
34
|
-
function map(decoder, mapper) {
|
|
35
|
-
return compose(decoder, function (x) {
|
|
36
|
-
try {
|
|
37
|
-
return (0, _lemons.Ok)(mapper(x));
|
|
38
|
-
} catch (e) {
|
|
39
|
-
return (0, _lemons.Err)((0, _debrief.annotate)(x, e instanceof Error ? e.message : String(e)));
|
|
40
|
-
}
|
|
41
|
-
});
|
|
42
|
-
}
|
|
43
|
-
/**
|
|
44
|
-
* Compose two decoders by passing the result of the first into the second.
|
|
45
|
-
* The second decoder may assume as its input type the output type of the first
|
|
46
|
-
* decoder (so it's not necessary to accept the typical "mixed"). This is
|
|
47
|
-
* useful for "narrowing down" the checks. For example, if you want to write
|
|
48
|
-
* a decoder for positive numbers, you can compose it from an existing decoder
|
|
49
|
-
* for any number, and a decoder that, assuming a number, checks if it's
|
|
50
|
-
* positive. Very often combined with the predicate() helper as the second
|
|
51
|
-
* argument.
|
|
52
|
-
*/
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
function compose(decoder, next) {
|
|
56
|
-
return function (blob) {
|
|
57
|
-
return decoder(blob).andThen(next);
|
|
58
|
-
};
|
|
59
|
-
}
|
|
60
|
-
/**
|
|
61
|
-
* Factory function returning a Decoder<T>, given a predicate function that
|
|
62
|
-
* accepts/rejects the input of type T.
|
|
63
|
-
*/
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
function predicate(predicate, msg) {
|
|
67
|
-
return function (value) {
|
|
68
|
-
return predicate(value) ? (0, _lemons.Ok)(value) : (0, _lemons.Err)((0, _debrief.annotate)(value, msg));
|
|
69
|
-
};
|
|
70
|
-
}
|