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
package/mapping.js
DELETED
|
@@ -1,113 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
|
|
3
|
-
Object.defineProperty(exports, "__esModule", {
|
|
4
|
-
value: true
|
|
5
|
-
});
|
|
6
|
-
exports.dict = dict;
|
|
7
|
-
exports.mapping = mapping;
|
|
8
|
-
|
|
9
|
-
var _debrief = require("debrief");
|
|
10
|
-
|
|
11
|
-
var _Result = require("lemons/Result");
|
|
12
|
-
|
|
13
|
-
var _object = require("./object");
|
|
14
|
-
|
|
15
|
-
var _utils = require("./utils");
|
|
16
|
-
|
|
17
|
-
function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }
|
|
18
|
-
|
|
19
|
-
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."); }
|
|
20
|
-
|
|
21
|
-
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; }
|
|
22
|
-
|
|
23
|
-
function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
|
|
24
|
-
|
|
25
|
-
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(_e2) { throw _e2; }, 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(_e3) { didErr = true; err = _e3; }, f: function f() { try { if (!normalCompletion && it["return"] != null) it["return"](); } finally { if (didErr) throw err; } } }; }
|
|
26
|
-
|
|
27
|
-
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); }
|
|
28
|
-
|
|
29
|
-
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; }
|
|
30
|
-
|
|
31
|
-
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; }
|
|
32
|
-
|
|
33
|
-
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; }
|
|
34
|
-
|
|
35
|
-
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; }
|
|
36
|
-
|
|
37
|
-
/**
|
|
38
|
-
* Given an object, will decode a Map of string keys to whatever values.
|
|
39
|
-
*
|
|
40
|
-
* For example, given a decoder for a Person, we can verify a Person lookup
|
|
41
|
-
* table structure (of type Map<string, Person>) like so:
|
|
42
|
-
*
|
|
43
|
-
* mapping(person)
|
|
44
|
-
*
|
|
45
|
-
*/
|
|
46
|
-
function mapping(decoder) {
|
|
47
|
-
return (0, _utils.compose)(_object.pojo, // $FlowFixMe[unclear-type] (not really an issue) - deliberate use of Object here
|
|
48
|
-
function (blob) {
|
|
49
|
-
var tuples = [];
|
|
50
|
-
var errors = [];
|
|
51
|
-
Object.keys(blob).forEach(function (key) {
|
|
52
|
-
var value = blob[key];
|
|
53
|
-
var result = decoder(value);
|
|
54
|
-
|
|
55
|
-
try {
|
|
56
|
-
var okValue = result.unwrap();
|
|
57
|
-
|
|
58
|
-
if (errors.length === 0) {
|
|
59
|
-
tuples.push([key, okValue]);
|
|
60
|
-
}
|
|
61
|
-
} catch (e) {
|
|
62
|
-
/* istanbul ignore else */
|
|
63
|
-
if ((0, _debrief.isAnnotation)(e)) {
|
|
64
|
-
tuples.length = 0; // Clear the tuples array
|
|
65
|
-
|
|
66
|
-
errors.push([key, e]);
|
|
67
|
-
} else {
|
|
68
|
-
// Otherwise, simply rethrow it
|
|
69
|
-
|
|
70
|
-
/* istanbul ignore next */
|
|
71
|
-
throw e;
|
|
72
|
-
}
|
|
73
|
-
}
|
|
74
|
-
});
|
|
75
|
-
|
|
76
|
-
if (errors.length > 0) {
|
|
77
|
-
return (0, _Result.Err)((0, _debrief.annotateFields)(blob, errors));
|
|
78
|
-
} else {
|
|
79
|
-
return (0, _Result.Ok)(new Map(tuples));
|
|
80
|
-
}
|
|
81
|
-
});
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
function mapToObject(mapping) {
|
|
85
|
-
var result = _objectSpread({}, null);
|
|
86
|
-
|
|
87
|
-
var _iterator = _createForOfIteratorHelper(mapping.entries()),
|
|
88
|
-
_step;
|
|
89
|
-
|
|
90
|
-
try {
|
|
91
|
-
for (_iterator.s(); !(_step = _iterator.n()).done;) {
|
|
92
|
-
var _step$value = _slicedToArray(_step.value, 2),
|
|
93
|
-
k = _step$value[0],
|
|
94
|
-
v = _step$value[1];
|
|
95
|
-
|
|
96
|
-
result[k] = v;
|
|
97
|
-
}
|
|
98
|
-
} catch (err) {
|
|
99
|
-
_iterator.e(err);
|
|
100
|
-
} finally {
|
|
101
|
-
_iterator.f();
|
|
102
|
-
}
|
|
103
|
-
|
|
104
|
-
return result;
|
|
105
|
-
}
|
|
106
|
-
/**
|
|
107
|
-
* Like mapping(), but returns an object rather than a Map instance.
|
|
108
|
-
*/
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
function dict(decoder) {
|
|
112
|
-
return (0, _utils.map)(mapping(decoder), mapToObject);
|
|
113
|
-
}
|
package/mapping.js.flow
DELETED
|
@@ -1,71 +0,0 @@
|
|
|
1
|
-
// @flow strict
|
|
2
|
-
|
|
3
|
-
import type { Annotation } from 'debrief';
|
|
4
|
-
import { annotateFields, isAnnotation } from 'debrief';
|
|
5
|
-
import { Err, Ok } from 'lemons/Result';
|
|
6
|
-
|
|
7
|
-
import { pojo } from './object';
|
|
8
|
-
import type { Decoder } from './types';
|
|
9
|
-
import { compose, map } from './utils';
|
|
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(
|
|
22
|
-
pojo,
|
|
23
|
-
// $FlowFixMe[unclear-type] (not really an issue) - deliberate use of Object here
|
|
24
|
-
(blob: Object) => {
|
|
25
|
-
let tuples: Array<[string, T]> = [];
|
|
26
|
-
let errors: Array<[string, string | Annotation]> = [];
|
|
27
|
-
|
|
28
|
-
Object.keys(blob).forEach((key: string) => {
|
|
29
|
-
const value: T = blob[key];
|
|
30
|
-
const result = decoder(value);
|
|
31
|
-
try {
|
|
32
|
-
const okValue = result.unwrap();
|
|
33
|
-
if (errors.length === 0) {
|
|
34
|
-
tuples.push([key, okValue]);
|
|
35
|
-
}
|
|
36
|
-
} catch (e) {
|
|
37
|
-
/* istanbul ignore else */
|
|
38
|
-
if (isAnnotation(e)) {
|
|
39
|
-
tuples.length = 0; // Clear the tuples array
|
|
40
|
-
errors.push([key, e]);
|
|
41
|
-
} else {
|
|
42
|
-
// Otherwise, simply rethrow it
|
|
43
|
-
/* istanbul ignore next */
|
|
44
|
-
throw e;
|
|
45
|
-
}
|
|
46
|
-
}
|
|
47
|
-
});
|
|
48
|
-
|
|
49
|
-
if (errors.length > 0) {
|
|
50
|
-
return Err(annotateFields(blob, errors));
|
|
51
|
-
} else {
|
|
52
|
-
return Ok(new Map(tuples));
|
|
53
|
-
}
|
|
54
|
-
}
|
|
55
|
-
);
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
function mapToObject<T>(mapping: Map<string, T>): { [string]: T } {
|
|
59
|
-
const result: { [string]: T } = { ...null };
|
|
60
|
-
for (const [k, v] of mapping.entries()) {
|
|
61
|
-
result[k] = v;
|
|
62
|
-
}
|
|
63
|
-
return result;
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
/**
|
|
67
|
-
* Like mapping(), but returns an object rather than a Map instance.
|
|
68
|
-
*/
|
|
69
|
-
export function dict<T>(decoder: Decoder<T>): Decoder<{ [string]: T }> {
|
|
70
|
-
return map(mapping(decoder), mapToObject);
|
|
71
|
-
}
|
package/number.js
DELETED
|
@@ -1,38 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
|
|
3
|
-
Object.defineProperty(exports, "__esModule", {
|
|
4
|
-
value: true
|
|
5
|
-
});
|
|
6
|
-
exports.positiveNumber = exports.positiveInteger = exports.number = exports.integer = void 0;
|
|
7
|
-
|
|
8
|
-
var _debrief = require("debrief");
|
|
9
|
-
|
|
10
|
-
var _Result = require("lemons/Result");
|
|
11
|
-
|
|
12
|
-
var _utils = require("./utils");
|
|
13
|
-
|
|
14
|
-
var anyNumber = function anyNumber(blob) {
|
|
15
|
-
return typeof blob === 'number' && !Number.isNaN(blob) ? (0, _Result.Ok)(blob) : (0, _Result.Err)((0, _debrief.annotate)(blob, 'Must be number'));
|
|
16
|
-
};
|
|
17
|
-
|
|
18
|
-
var isInteger = function isInteger(n) {
|
|
19
|
-
return Number.isInteger(n);
|
|
20
|
-
};
|
|
21
|
-
|
|
22
|
-
var isFinite = function isFinite(n) {
|
|
23
|
-
return Number.isFinite(n);
|
|
24
|
-
};
|
|
25
|
-
|
|
26
|
-
var number = (0, _utils.compose)(anyNumber, (0, _utils.predicate)(isFinite, 'Number must be finite'));
|
|
27
|
-
exports.number = number;
|
|
28
|
-
var positiveNumber = (0, _utils.compose)(number, (0, _utils.predicate)(function (n) {
|
|
29
|
-
return n >= 0;
|
|
30
|
-
}, 'Number must be positive')); // Integers
|
|
31
|
-
|
|
32
|
-
exports.positiveNumber = positiveNumber;
|
|
33
|
-
var integer = (0, _utils.compose)(number, (0, _utils.predicate)(isInteger, 'Number must be an integer'));
|
|
34
|
-
exports.integer = integer;
|
|
35
|
-
var positiveInteger = (0, _utils.compose)(integer, (0, _utils.predicate)(function (n) {
|
|
36
|
-
return n >= 0;
|
|
37
|
-
}, 'Number must be positive'));
|
|
38
|
-
exports.positiveInteger = positiveInteger;
|
package/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 _Result = require("lemons/Result");
|
|
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, _Result.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, _Result.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, _Result.Err)(err);
|
|
179
|
-
}
|
|
180
|
-
|
|
181
|
-
return (0, _Result.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, _Result.Err)((0, _debrief.annotate)(blob, "Superfluous keys: ".concat(Array.from(superfluous).join(', '))));
|
|
194
|
-
}
|
|
195
|
-
|
|
196
|
-
return (0, _Result.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/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 _Result = require("lemons/Result");
|
|
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, _Result.Ok)(blob) : // Combine error message into a single line
|
|
43
|
-
(0, _Result.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
|
-
}
|