decoders 1.25.5 → 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/CHANGELOG.md +23 -3
- 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/{array.js.flow → _esm/core/array.js.flow} +22 -25
- package/_esm/core/boolean.js +28 -0
- package/{boolean.js.flow → _esm/core/boolean.js.flow} +8 -7
- package/_esm/core/composition.js +42 -0
- package/{utils.js.flow → _esm/core/composition.js.flow} +7 -22
- package/_esm/core/constants.js +46 -0
- package/{constants.js.flow → _esm/core/constants.js.flow} +13 -12
- package/_esm/core/date.js +28 -0
- package/{date.js.flow → _esm/core/date.js.flow} +9 -7
- package/{describe.js → _esm/core/describe.js} +5 -11
- package/{describe.js.flow → _esm/core/describe.js.flow} +4 -4
- package/{dispatch.js → _esm/core/dispatch.js} +8 -15
- package/{dispatch.js.flow → _esm/core/dispatch.js.flow} +6 -5
- package/_esm/core/either.js +90 -0
- package/{either.js.flow → _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/{instanceOf.js.flow → _esm/core/instanceOf.js.flow} +7 -8
- package/_esm/core/json.js +15 -0
- package/{json.js.flow → _esm/core/json.js.flow} +4 -4
- package/_esm/core/lazy.js +11 -0
- package/{lazy.js.flow → _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/{number.js.flow → _esm/core/number.js.flow} +10 -11
- package/_esm/core/object.js +175 -0
- package/{object.js.flow → _esm/core/object.js.flow} +44 -52
- package/_esm/core/optional.js +38 -0
- package/{optional.js.flow → _esm/core/optional.js.flow} +6 -7
- package/{string.js → _esm/core/string.js} +18 -35
- package/{string.js.flow → _esm/core/string.js.flow} +13 -12
- package/_esm/core/tuple.js +155 -0
- package/{tuple.js.flow → _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/_esm/index.js.flow +56 -0
- 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/{json.js → core/json.js} +3 -5
- package/core/json.js.flow +28 -0
- package/{lazy.js → 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 +118 -395
- package/index.js.flow +24 -24
- package/package.json +2 -6
- package/result.js +172 -0
- package/result.js.flow +166 -0
- package/array.d.ts +0 -5
- package/array.js +0 -133
- package/boolean.d.ts +0 -5
- package/boolean.js +0 -42
- package/constants.d.ts +0 -11
- package/constants.js +0 -67
- package/date.d.ts +0 -4
- package/date.js +0 -42
- package/describe.d.ts +0 -3
- package/dispatch.d.ts +0 -8
- package/either.d.ts +0 -61
- package/either.js +0 -85
- 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/helpers.d.ts +0 -79
- package/index.d.ts +0 -38
- package/instanceOf.d.ts +0 -3
- package/instanceOf.js +0 -17
- package/json.d.ts +0 -11
- package/lazy.d.ts +0 -3
- package/mapping.d.ts +0 -4
- package/mapping.js +0 -113
- package/mapping.js.flow +0 -71
- package/number.d.ts +0 -6
- package/number.js +0 -38
- package/object.d.ts +0 -33
- package/object.js +0 -254
- package/optional.d.ts +0 -5
- package/optional.js +0 -52
- package/string.d.ts +0 -7
- package/tuple.d.ts +0 -30
- package/tuple.js +0 -199
- 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
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
exports.__esModule = true;
|
|
4
|
+
exports.compose = compose;
|
|
5
|
+
exports.map = map;
|
|
6
|
+
exports.predicate = predicate;
|
|
7
|
+
|
|
8
|
+
var Result = _interopRequireWildcard(require("../result"));
|
|
9
|
+
|
|
10
|
+
var _annotate = require("../annotate");
|
|
11
|
+
|
|
12
|
+
function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
|
|
13
|
+
|
|
14
|
+
function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { "default": obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj["default"] = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* Given a decoder T and a mapping function from T's to V's, returns a decoder
|
|
18
|
+
* for V's. This is useful to change the original input data.
|
|
19
|
+
*/
|
|
20
|
+
function map(decoder, mapper) {
|
|
21
|
+
return compose(decoder, function (x) {
|
|
22
|
+
try {
|
|
23
|
+
return Result.ok(mapper(x));
|
|
24
|
+
} catch (e) {
|
|
25
|
+
return Result.err((0, _annotate.annotate)(x, e instanceof Error ? e.message : String(e)));
|
|
26
|
+
}
|
|
27
|
+
});
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* Compose two decoders by passing the result of the first into the second.
|
|
31
|
+
* The second decoder may assume as its input type the output type of the first
|
|
32
|
+
* decoder (so it's not necessary to accept the typical "mixed"). This is
|
|
33
|
+
* useful for "narrowing down" the checks. For example, if you want to write
|
|
34
|
+
* a decoder for positive numbers, you can compose it from an existing decoder
|
|
35
|
+
* for any number, and a decoder that, assuming a number, checks if it's
|
|
36
|
+
* positive. Very often combined with the predicate() helper as the second
|
|
37
|
+
* argument.
|
|
38
|
+
*/
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
function compose(decoder, next) {
|
|
42
|
+
return function (blob) {
|
|
43
|
+
return Result.andThen(decoder(blob), next);
|
|
44
|
+
};
|
|
45
|
+
}
|
|
46
|
+
/**
|
|
47
|
+
* Factory function returning a Decoder<T>, given a predicate function that
|
|
48
|
+
* accepts/rejects the input of type T.
|
|
49
|
+
*/
|
|
50
|
+
|
|
51
|
+
|
|
52
|
+
function predicate(predicate, msg) {
|
|
53
|
+
return function (value) {
|
|
54
|
+
return predicate(value) ? Result.ok(value) : Result.err((0, _annotate.annotate)(value, msg));
|
|
55
|
+
};
|
|
56
|
+
}
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
// @flow strict
|
|
2
|
+
|
|
3
|
+
import * as Result from '../result';
|
|
4
|
+
import { annotate } from '../annotate';
|
|
5
|
+
import type { Decoder } from '../_types';
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Given a decoder T and a mapping function from T's to V's, returns a decoder
|
|
9
|
+
* for V's. This is useful to change the original input data.
|
|
10
|
+
*/
|
|
11
|
+
export function map<T, V>(decoder: Decoder<T>, mapper: (T) => V): Decoder<V> {
|
|
12
|
+
return compose(decoder, (x) => {
|
|
13
|
+
try {
|
|
14
|
+
return Result.ok(mapper(x));
|
|
15
|
+
} catch (e) {
|
|
16
|
+
return Result.err(annotate(x, e instanceof Error ? e.message : String(e)));
|
|
17
|
+
}
|
|
18
|
+
});
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* Compose two decoders by passing the result of the first into the second.
|
|
23
|
+
* The second decoder may assume as its input type the output type of the first
|
|
24
|
+
* decoder (so it's not necessary to accept the typical "mixed"). This is
|
|
25
|
+
* useful for "narrowing down" the checks. For example, if you want to write
|
|
26
|
+
* a decoder for positive numbers, you can compose it from an existing decoder
|
|
27
|
+
* for any number, and a decoder that, assuming a number, checks if it's
|
|
28
|
+
* positive. Very often combined with the predicate() helper as the second
|
|
29
|
+
* argument.
|
|
30
|
+
*/
|
|
31
|
+
export function compose<T, V>(decoder: Decoder<T>, next: Decoder<V, T>): Decoder<V> {
|
|
32
|
+
return (blob: mixed) => Result.andThen(decoder(blob), next);
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* Factory function returning a Decoder<T>, given a predicate function that
|
|
37
|
+
* accepts/rejects the input of type T.
|
|
38
|
+
*/
|
|
39
|
+
export function predicate<T>(predicate: (T) => boolean, msg: string): Decoder<T, T> {
|
|
40
|
+
return (value: T) => {
|
|
41
|
+
return predicate(value) ? Result.ok(value) : Result.err(annotate(value, msg));
|
|
42
|
+
};
|
|
43
|
+
}
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
exports.__esModule = true;
|
|
4
|
+
exports.constant = constant;
|
|
5
|
+
exports.hardcoded = hardcoded;
|
|
6
|
+
exports.unknown = exports.undefined_ = exports.null_ = exports.mixed = void 0;
|
|
7
|
+
|
|
8
|
+
var Result = _interopRequireWildcard(require("../result"));
|
|
9
|
+
|
|
10
|
+
var _annotate = require("../annotate");
|
|
11
|
+
|
|
12
|
+
function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
|
|
13
|
+
|
|
14
|
+
function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { "default": obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj["default"] = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* Decoder that only returns Ok for `null` inputs. Err otherwise.
|
|
18
|
+
*/
|
|
19
|
+
var null_ = function null_(blob) {
|
|
20
|
+
return blob === null ? Result.ok(blob) : Result.err((0, _annotate.annotate)(blob, 'Must be null'));
|
|
21
|
+
};
|
|
22
|
+
/**
|
|
23
|
+
* Decoder that only returns Ok for `undefined` inputs. Err otherwise.
|
|
24
|
+
*/
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
exports.null_ = null_;
|
|
28
|
+
|
|
29
|
+
var undefined_ = function undefined_(blob) {
|
|
30
|
+
return blob === undefined ? Result.ok(blob) : Result.err((0, _annotate.annotate)(blob, 'Must be undefined'));
|
|
31
|
+
};
|
|
32
|
+
/**
|
|
33
|
+
* Decoder that only returns Ok for the given value constant. Err otherwise.
|
|
34
|
+
*/
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
exports.undefined_ = undefined_;
|
|
38
|
+
|
|
39
|
+
function constant(value) {
|
|
40
|
+
return function (blob) {
|
|
41
|
+
return blob === value ? Result.ok(value) : Result.err((0, _annotate.annotate)(blob, "Must be constant " + String(value)));
|
|
42
|
+
};
|
|
43
|
+
}
|
|
44
|
+
/**
|
|
45
|
+
* Decoder that always returns Ok for the given hardcoded value, no matter what the input.
|
|
46
|
+
*/
|
|
47
|
+
|
|
48
|
+
|
|
49
|
+
function hardcoded(value) {
|
|
50
|
+
return function () {
|
|
51
|
+
return Result.ok(value);
|
|
52
|
+
};
|
|
53
|
+
}
|
|
54
|
+
/**
|
|
55
|
+
* Decoder that always returns Ok for the given hardcoded value, no matter what the input.
|
|
56
|
+
*/
|
|
57
|
+
|
|
58
|
+
|
|
59
|
+
var unknown = function unknown(blob) {
|
|
60
|
+
return Result.ok(blob);
|
|
61
|
+
};
|
|
62
|
+
/**
|
|
63
|
+
* Alias of unknown.
|
|
64
|
+
*/
|
|
65
|
+
|
|
66
|
+
|
|
67
|
+
exports.unknown = unknown;
|
|
68
|
+
var mixed = unknown;
|
|
69
|
+
exports.mixed = mixed;
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
// @flow strict
|
|
2
|
+
|
|
3
|
+
import * as Result from '../result';
|
|
4
|
+
import { annotate } from '../annotate';
|
|
5
|
+
import type { Decoder, Scalar } from '../_types';
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Decoder that only returns Ok for `null` inputs. Err otherwise.
|
|
9
|
+
*/
|
|
10
|
+
export const null_: Decoder<null> = (blob: mixed) =>
|
|
11
|
+
blob === null ? Result.ok(blob) : Result.err(annotate(blob, 'Must be null'));
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* Decoder that only returns Ok for `undefined` inputs. Err otherwise.
|
|
15
|
+
*/
|
|
16
|
+
export const undefined_: Decoder<void> = (blob: mixed) =>
|
|
17
|
+
blob === undefined
|
|
18
|
+
? Result.ok(blob)
|
|
19
|
+
: Result.err(annotate(blob, 'Must be undefined'));
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* Decoder that only returns Ok for the given value constant. Err otherwise.
|
|
23
|
+
*/
|
|
24
|
+
export function constant<T: Scalar>(value: T): Decoder<T> {
|
|
25
|
+
return (blob: mixed) =>
|
|
26
|
+
blob === value
|
|
27
|
+
? Result.ok(value)
|
|
28
|
+
: Result.err(annotate(blob, `Must be constant ${String(value)}`));
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* Decoder that always returns Ok for the given hardcoded value, no matter what the input.
|
|
33
|
+
*/
|
|
34
|
+
export function hardcoded<T>(value: T): Decoder<T> {
|
|
35
|
+
return () => Result.ok(value);
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* Decoder that always returns Ok for the given hardcoded value, no matter what the input.
|
|
40
|
+
*/
|
|
41
|
+
export const unknown: Decoder<mixed> = (blob: mixed) => Result.ok(blob);
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* Alias of unknown.
|
|
45
|
+
*/
|
|
46
|
+
export const mixed: Decoder<mixed> = unknown;
|
package/core/date.js
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
exports.__esModule = true;
|
|
4
|
+
exports.iso8601 = exports.date = void 0;
|
|
5
|
+
|
|
6
|
+
var Result = _interopRequireWildcard(require("../result"));
|
|
7
|
+
|
|
8
|
+
var _annotate = require("../annotate");
|
|
9
|
+
|
|
10
|
+
var _utils = require("../_utils");
|
|
11
|
+
|
|
12
|
+
var _composition = require("./composition");
|
|
13
|
+
|
|
14
|
+
var _string = require("./string");
|
|
15
|
+
|
|
16
|
+
function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
|
|
17
|
+
|
|
18
|
+
function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { "default": obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj["default"] = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
|
|
19
|
+
|
|
20
|
+
// Only matches the shape. This "over-matches" some values that still aren't
|
|
21
|
+
// valid dates (like 9999-99-99), but those will be caught by JS Date's
|
|
22
|
+
// internal validations
|
|
23
|
+
var iso8601_re = /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(?:[.]\d+)?(?:Z|[+-]\d{2}:?\d{2})$/;
|
|
24
|
+
|
|
25
|
+
var date = function date(value) {
|
|
26
|
+
return (0, _utils.isDate)(value) ? Result.ok(value) : Result.err((0, _annotate.annotate)(value, 'Must be a Date'));
|
|
27
|
+
};
|
|
28
|
+
/**
|
|
29
|
+
* Decoder that only returns Ok for strings that are valid ISO8601 date
|
|
30
|
+
* strings. Err otherwise.
|
|
31
|
+
*/
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
exports.date = date;
|
|
35
|
+
var iso8601 = (0, _composition.map)( // Input itself needs to match the ISO8601 regex...
|
|
36
|
+
(0, _string.regex)(iso8601_re, 'Must be ISO8601 format'), // Make sure it is a _valid_ date
|
|
37
|
+
function (value) {
|
|
38
|
+
var date = new Date(value);
|
|
39
|
+
|
|
40
|
+
if (isNaN(date.getTime())) {
|
|
41
|
+
throw new Error('Must be valid date/time value');
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
return date;
|
|
45
|
+
});
|
|
46
|
+
exports.iso8601 = iso8601;
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
// @flow strict
|
|
2
|
+
|
|
3
|
+
import * as Result from '../result';
|
|
4
|
+
import { annotate } from '../annotate';
|
|
5
|
+
import { isDate } from '../_utils';
|
|
6
|
+
import { map } from './composition';
|
|
7
|
+
import { regex } from './string';
|
|
8
|
+
import type { Decoder } from '../_types';
|
|
9
|
+
|
|
10
|
+
// $FlowFixMe[unclear-type] (not really an issue) - deliberate casting
|
|
11
|
+
type cast = any;
|
|
12
|
+
|
|
13
|
+
// Only matches the shape. This "over-matches" some values that still aren't
|
|
14
|
+
// valid dates (like 9999-99-99), but those will be caught by JS Date's
|
|
15
|
+
// internal validations
|
|
16
|
+
const iso8601_re =
|
|
17
|
+
/^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(?:[.]\d+)?(?:Z|[+-]\d{2}:?\d{2})$/;
|
|
18
|
+
|
|
19
|
+
export const date: Decoder<Date> = (value: mixed) =>
|
|
20
|
+
isDate(value)
|
|
21
|
+
? Result.ok(((value: cast): Date))
|
|
22
|
+
: Result.err(annotate(value, 'Must be a Date'));
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* Decoder that only returns Ok for strings that are valid ISO8601 date
|
|
26
|
+
* strings. Err otherwise.
|
|
27
|
+
*/
|
|
28
|
+
export const iso8601: Decoder<Date> = map(
|
|
29
|
+
// Input itself needs to match the ISO8601 regex...
|
|
30
|
+
regex(iso8601_re, 'Must be ISO8601 format'),
|
|
31
|
+
|
|
32
|
+
// Make sure it is a _valid_ date
|
|
33
|
+
(value: string) => {
|
|
34
|
+
const date = new Date(value);
|
|
35
|
+
if (isNaN(date.getTime())) {
|
|
36
|
+
throw new Error('Must be valid date/time value');
|
|
37
|
+
}
|
|
38
|
+
return date;
|
|
39
|
+
},
|
|
40
|
+
);
|
package/core/describe.js
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
exports.__esModule = true;
|
|
4
|
+
exports.describe = describe;
|
|
5
|
+
|
|
6
|
+
var Result = _interopRequireWildcard(require("../result"));
|
|
7
|
+
|
|
8
|
+
var _annotate = require("../annotate");
|
|
9
|
+
|
|
10
|
+
function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
|
|
11
|
+
|
|
12
|
+
function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { "default": obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj["default"] = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* Wrap another decoder, and override the error message in case it fails. This
|
|
16
|
+
* is useful to "simplify" otherwise potentially complex error messages, or to
|
|
17
|
+
* use language in those error messages that can be relayed to end users (for
|
|
18
|
+
* example to show in form errors).
|
|
19
|
+
*/
|
|
20
|
+
function describe(decoder, message) {
|
|
21
|
+
return function (blob) {
|
|
22
|
+
return Result.mapError(decoder(blob), function (err) {
|
|
23
|
+
return (0, _annotate.annotate)(err, message);
|
|
24
|
+
});
|
|
25
|
+
};
|
|
26
|
+
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
// @flow strict
|
|
2
|
+
|
|
3
|
+
import * as Result from '../result';
|
|
4
|
+
import { annotate } from '../annotate';
|
|
5
|
+
import type { Decoder } from '../_types';
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Wrap another decoder, and override the error message in case it fails. This
|
|
9
|
+
* is useful to "simplify" otherwise potentially complex error messages, or to
|
|
10
|
+
* use language in those error messages that can be relayed to end users (for
|
|
11
|
+
* example to show in form errors).
|
|
12
|
+
*/
|
|
13
|
+
export function describe<T>(decoder: Decoder<T>, message: string): Decoder<T> {
|
|
14
|
+
return (blob: mixed) => {
|
|
15
|
+
return Result.mapError(decoder(blob), (err) => annotate(err, message));
|
|
16
|
+
};
|
|
17
|
+
}
|
package/core/dispatch.js
ADDED
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
exports.__esModule = true;
|
|
4
|
+
exports.dispatch = dispatch;
|
|
5
|
+
|
|
6
|
+
var Result = _interopRequireWildcard(require("../result"));
|
|
7
|
+
|
|
8
|
+
var _object2 = require("./object");
|
|
9
|
+
|
|
10
|
+
var _either = require("./either");
|
|
11
|
+
|
|
12
|
+
function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
|
|
13
|
+
|
|
14
|
+
function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { "default": obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj["default"] = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* Dispatches to one of several given decoders, based on the value found at
|
|
18
|
+
* runtime in the given field. For example, suppose you have these decoders:
|
|
19
|
+
*
|
|
20
|
+
* const rectangle = object({
|
|
21
|
+
* type: constant('rect'),
|
|
22
|
+
* x: number,
|
|
23
|
+
* y: number,
|
|
24
|
+
* width: number,
|
|
25
|
+
* height: number,
|
|
26
|
+
* });
|
|
27
|
+
*
|
|
28
|
+
* const circle = object({
|
|
29
|
+
* type: constant('circle'),
|
|
30
|
+
* cx: number,
|
|
31
|
+
* cy: number,
|
|
32
|
+
* r: number,
|
|
33
|
+
* });
|
|
34
|
+
*
|
|
35
|
+
* Then these two decoders are equivalent:
|
|
36
|
+
*
|
|
37
|
+
* const shape = either(rectangle, circle)
|
|
38
|
+
* const shape = dispatch('type', { rectangle, circle })
|
|
39
|
+
*
|
|
40
|
+
* Will be of type Decoder<Rectangle | Circle>.
|
|
41
|
+
*
|
|
42
|
+
* But the dispatch version will typically be more runtime-efficient. The
|
|
43
|
+
* reason is that it will first do minimal work to "look ahead" into the `type`
|
|
44
|
+
* field here, and based on that value, pick the decoder to invoke.
|
|
45
|
+
*
|
|
46
|
+
* The `either` version will simply try to invoke each decoder, until it finds
|
|
47
|
+
* one that matches.
|
|
48
|
+
*
|
|
49
|
+
* Also, the error messages will be less ambiguous using `dispatch()`.
|
|
50
|
+
*/
|
|
51
|
+
function dispatch(field, mapping) {
|
|
52
|
+
var _object;
|
|
53
|
+
|
|
54
|
+
var base = (0, _object2.object)((_object = {}, _object[field] = (0, _either.oneOf)(Object.keys(mapping)), _object));
|
|
55
|
+
return function (blob) {
|
|
56
|
+
return Result.andThen(base(blob), function (baseObj) {
|
|
57
|
+
var decoderName = baseObj[field];
|
|
58
|
+
var decoder = mapping[decoderName];
|
|
59
|
+
return decoder(blob);
|
|
60
|
+
});
|
|
61
|
+
};
|
|
62
|
+
}
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
// @flow strict
|
|
2
|
+
|
|
3
|
+
import * as Result from '../result';
|
|
4
|
+
import { object } from './object';
|
|
5
|
+
import { oneOf } from './either';
|
|
6
|
+
import type { Decoder, DecoderType } from '../_types';
|
|
7
|
+
|
|
8
|
+
// $FlowFixMe[unclear-type] (not really an issue) - deliberate use of `any` - not sure how we should get rid of this
|
|
9
|
+
type anything = any;
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* Dispatches to one of several given decoders, based on the value found at
|
|
13
|
+
* runtime in the given field. For example, suppose you have these decoders:
|
|
14
|
+
*
|
|
15
|
+
* const rectangle = object({
|
|
16
|
+
* type: constant('rect'),
|
|
17
|
+
* x: number,
|
|
18
|
+
* y: number,
|
|
19
|
+
* width: number,
|
|
20
|
+
* height: number,
|
|
21
|
+
* });
|
|
22
|
+
*
|
|
23
|
+
* const circle = object({
|
|
24
|
+
* type: constant('circle'),
|
|
25
|
+
* cx: number,
|
|
26
|
+
* cy: number,
|
|
27
|
+
* r: number,
|
|
28
|
+
* });
|
|
29
|
+
*
|
|
30
|
+
* Then these two decoders are equivalent:
|
|
31
|
+
*
|
|
32
|
+
* const shape = either(rectangle, circle)
|
|
33
|
+
* const shape = dispatch('type', { rectangle, circle })
|
|
34
|
+
*
|
|
35
|
+
* Will be of type Decoder<Rectangle | Circle>.
|
|
36
|
+
*
|
|
37
|
+
* But the dispatch version will typically be more runtime-efficient. The
|
|
38
|
+
* reason is that it will first do minimal work to "look ahead" into the `type`
|
|
39
|
+
* field here, and based on that value, pick the decoder to invoke.
|
|
40
|
+
*
|
|
41
|
+
* The `either` version will simply try to invoke each decoder, until it finds
|
|
42
|
+
* one that matches.
|
|
43
|
+
*
|
|
44
|
+
* Also, the error messages will be less ambiguous using `dispatch()`.
|
|
45
|
+
*/
|
|
46
|
+
export function dispatch<O: { +[field: string]: Decoder<anything>, ... }>(
|
|
47
|
+
field: string,
|
|
48
|
+
mapping: O,
|
|
49
|
+
): Decoder<$Values<$ObjMap<O, DecoderType>>> {
|
|
50
|
+
const base = object({ [field]: oneOf(Object.keys(mapping)) });
|
|
51
|
+
return (blob: mixed) => {
|
|
52
|
+
return Result.andThen(base(blob), (baseObj) => {
|
|
53
|
+
const decoderName = baseObj[field];
|
|
54
|
+
const decoder = mapping[decoderName];
|
|
55
|
+
return decoder(blob);
|
|
56
|
+
});
|
|
57
|
+
};
|
|
58
|
+
}
|
package/core/either.js
ADDED
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
exports.__esModule = true;
|
|
4
|
+
exports.either = either;
|
|
5
|
+
exports.either3 = either3;
|
|
6
|
+
exports.either4 = either4;
|
|
7
|
+
exports.either5 = either5;
|
|
8
|
+
exports.either6 = either6;
|
|
9
|
+
exports.either7 = either7;
|
|
10
|
+
exports.either8 = either8;
|
|
11
|
+
exports.either9 = either9;
|
|
12
|
+
exports.oneOf = oneOf;
|
|
13
|
+
|
|
14
|
+
var Result = _interopRequireWildcard(require("../result"));
|
|
15
|
+
|
|
16
|
+
var _annotate = require("../annotate");
|
|
17
|
+
|
|
18
|
+
var _utils = require("../_utils");
|
|
19
|
+
|
|
20
|
+
function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
|
|
21
|
+
|
|
22
|
+
function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { "default": obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj["default"] = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* Indents and adds a dash in front of this (potentially multiline) string.
|
|
26
|
+
*/
|
|
27
|
+
function itemize(s) {
|
|
28
|
+
return '-' + (0, _utils.indent)(s).substring(1);
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* Nests another error as an item under a new-to-be-created "Either error". If
|
|
32
|
+
* the given subitem already is an "Either error" of itself, don't indent, but
|
|
33
|
+
* just "inject" its items at the same error level, for nicely flattened either
|
|
34
|
+
* expressions.
|
|
35
|
+
*
|
|
36
|
+
* Avoids:
|
|
37
|
+
*
|
|
38
|
+
* Either:
|
|
39
|
+
* - Either:
|
|
40
|
+
* - Must be P
|
|
41
|
+
* - Either:
|
|
42
|
+
* - Must be Q
|
|
43
|
+
* - Must be R
|
|
44
|
+
* - Must be S
|
|
45
|
+
*
|
|
46
|
+
* And "flattens" these to:
|
|
47
|
+
*
|
|
48
|
+
* Either:
|
|
49
|
+
* - Must be P
|
|
50
|
+
* - Must be Q
|
|
51
|
+
* - Must be R
|
|
52
|
+
* - Must be S
|
|
53
|
+
*
|
|
54
|
+
*/
|
|
55
|
+
|
|
56
|
+
|
|
57
|
+
function nest(errText) {
|
|
58
|
+
var EITHER_PREFIX = 'Either:\n';
|
|
59
|
+
return errText.startsWith(EITHER_PREFIX) ? errText.substr(EITHER_PREFIX.length) : itemize(errText);
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
function either(d1, d2) {
|
|
63
|
+
return function (blob) {
|
|
64
|
+
return Result.orElse(d1(blob), function (err1) {
|
|
65
|
+
return Result.orElse(d2(blob), function (err2) {
|
|
66
|
+
var serr1 = (0, _utils.summarize)(err1).join('\n');
|
|
67
|
+
var serr2 = (0, _utils.summarize)(err2).join('\n');
|
|
68
|
+
var text = ['Either:', nest(serr1), nest(serr2)].join('\n');
|
|
69
|
+
return Result.err((0, _annotate.annotate)(blob, text));
|
|
70
|
+
});
|
|
71
|
+
});
|
|
72
|
+
};
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
function either3(d1, d2, d3) {
|
|
76
|
+
return either(d1, either(d2, d3));
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
function either4(d1, d2, d3, d4) {
|
|
80
|
+
return either(d1, either3(d2, d3, d4));
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
function either5(d1, d2, d3, d4, d5) {
|
|
84
|
+
return either(d1, either4(d2, d3, d4, d5));
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
function either6(d1, d2, d3, d4, d5, d6) {
|
|
88
|
+
return either(d1, either5(d2, d3, d4, d5, d6));
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
function either7(d1, d2, d3, d4, d5, d6, d7) {
|
|
92
|
+
return either(d1, either6(d2, d3, d4, d5, d6, d7));
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
function either8(d1, d2, d3, d4, d5, d6, d7, d8) {
|
|
96
|
+
return either(d1, either7(d2, d3, d4, d5, d6, d7, d8));
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
function either9(d1, d2, d3, d4, d5, d6, d7, d8, d9) {
|
|
100
|
+
return either(d1, either8(d2, d3, d4, d5, d6, d7, d8, d9));
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
function oneOf(constants) {
|
|
104
|
+
return function (blob) {
|
|
105
|
+
var winner = constants.find(function (c) {
|
|
106
|
+
return c === blob;
|
|
107
|
+
});
|
|
108
|
+
|
|
109
|
+
if (winner !== undefined) {
|
|
110
|
+
return Result.ok(winner);
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
return Result.err((0, _annotate.annotate)(blob, "Must be one of " + constants.map(function (value) {
|
|
114
|
+
return JSON.stringify(value);
|
|
115
|
+
}).join(', ')));
|
|
116
|
+
};
|
|
117
|
+
}
|