decoders 2.0.0-beta5 → 2.0.0-beta9
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 +19 -0
- package/NotSupportedTSVersion.d.ts +1 -0
- package/README.md +934 -387
- package/_guard.d.ts +7 -0
- package/_guard.js +2 -6
- package/_guard.js.flow +3 -3
- package/{_esm/_guard.js → _guard.mjs} +3 -3
- package/_types.d.ts +13 -0
- package/{_esm/_types.js → _types.mjs} +0 -0
- package/_utils.d.ts +10 -0
- package/_utils.js +1 -1
- package/_utils.js.flow +3 -3
- package/{_esm/_utils.js → _utils.mjs} +1 -1
- package/annotate.d.ts +62 -0
- package/{_esm/annotate.js → annotate.mjs} +0 -0
- package/core/_helpers.d.ts +79 -0
- package/core/array.d.ts +8 -0
- package/core/array.js +19 -12
- package/core/array.js.flow +15 -11
- package/{_esm/core/array.js → core/array.mjs} +19 -10
- package/core/boolean.d.ts +5 -0
- package/core/boolean.js +5 -9
- package/core/boolean.js.flow +5 -7
- package/{_esm/core/boolean.js → core/boolean.mjs} +7 -7
- package/core/composition.d.ts +18 -0
- package/core/composition.js +41 -15
- package/core/composition.js.flow +41 -10
- package/core/composition.mjs +70 -0
- package/core/constants.d.ts +11 -0
- package/core/constants.js +6 -10
- package/core/constants.js.flow +7 -9
- package/{_esm/core/constants.js → core/constants.mjs} +7 -7
- package/core/date.d.ts +4 -0
- package/core/date.js +5 -9
- package/core/date.js.flow +4 -6
- package/{_esm/core/date.js → core/date.mjs} +7 -7
- package/core/describe.d.ts +3 -0
- package/core/describe.js +2 -6
- package/core/describe.js.flow +2 -2
- package/{_esm/core/describe.js → core/describe.mjs} +3 -3
- package/core/dispatch.d.ts +8 -0
- package/core/dispatch.js +11 -13
- package/core/dispatch.js.flow +13 -12
- package/{_esm/core/dispatch.js → core/dispatch.mjs} +12 -11
- package/core/either.d.ts +66 -0
- package/core/either.js +34 -50
- package/core/either.js.flow +40 -86
- package/core/either.mjs +90 -0
- package/core/fail.d.ts +3 -0
- package/core/fail.js +2 -6
- package/core/fail.js.flow +2 -2
- package/{_esm/core/fail.js → core/fail.mjs} +3 -3
- package/core/instanceOf.d.ts +3 -0
- package/core/instanceOf.js +2 -6
- package/core/instanceOf.js.flow +3 -3
- package/core/instanceOf.mjs +8 -0
- package/core/json.d.ts +11 -0
- package/core/json.js +3 -3
- package/core/json.js.flow +3 -3
- package/core/json.mjs +15 -0
- package/core/lazy.d.ts +3 -0
- package/{_esm/core/lazy.js → core/lazy.mjs} +0 -0
- package/core/number.d.ts +6 -0
- package/core/number.js +9 -13
- package/core/number.js.flow +18 -12
- package/core/number.mjs +25 -0
- package/core/object.d.ts +38 -0
- package/core/object.js +66 -13
- package/core/object.js.flow +84 -28
- package/{_esm/core/object.js → core/object.mjs} +64 -11
- package/core/optional.d.ts +5 -0
- package/core/optional.js +4 -8
- package/core/optional.js.flow +3 -3
- package/{_esm/core/optional.js → core/optional.mjs} +6 -6
- package/core/string.d.ts +13 -0
- package/core/string.js +31 -49
- package/core/string.js.flow +29 -39
- package/core/string.mjs +58 -0
- package/core/tuple.d.ts +30 -0
- package/core/tuple.js +30 -149
- package/core/tuple.js.flow +33 -197
- package/core/tuple.mjs +45 -0
- package/format.d.ts +4 -0
- package/{format/inline.js → format.js} +6 -1
- package/{_esm/format/inline.js.flow → format.js.flow} +6 -2
- package/{_esm/format/inline.js → format.mjs} +4 -1
- package/index.d.ts +42 -0
- package/index.js +33 -42
- package/index.js.flow +17 -18
- package/{_esm/index.js → index.mjs} +18 -19
- package/package.json +15 -3
- package/result.d.ts +39 -0
- package/result.js +9 -90
- package/result.js.flow +11 -87
- package/result.mjs +81 -0
- package/_esm/_guard.js.flow +0 -20
- package/_esm/_types.js.flow +0 -20
- package/_esm/_utils.js.flow +0 -97
- package/_esm/annotate.js.flow +0 -218
- package/_esm/core/array.js.flow +0 -103
- package/_esm/core/boolean.js.flow +0 -29
- package/_esm/core/composition.js +0 -42
- package/_esm/core/composition.js.flow +0 -43
- package/_esm/core/constants.js.flow +0 -46
- package/_esm/core/date.js.flow +0 -40
- package/_esm/core/describe.js.flow +0 -17
- package/_esm/core/dispatch.js.flow +0 -58
- package/_esm/core/either.js +0 -90
- package/_esm/core/either.js.flow +0 -151
- package/_esm/core/fail.js.flow +0 -12
- package/_esm/core/instanceOf.js +0 -8
- package/_esm/core/instanceOf.js.flow +0 -20
- package/_esm/core/json.js +0 -15
- package/_esm/core/json.js.flow +0 -28
- package/_esm/core/lazy.js.flow +0 -15
- package/_esm/core/mapping.js +0 -54
- package/_esm/core/mapping.js.flow +0 -54
- package/_esm/core/number.js +0 -25
- package/_esm/core/number.js.flow +0 -34
- package/_esm/core/object.js.flow +0 -203
- package/_esm/core/optional.js.flow +0 -41
- package/_esm/core/string.js +0 -76
- package/_esm/core/string.js.flow +0 -82
- package/_esm/core/tuple.js +0 -155
- package/_esm/core/tuple.js.flow +0 -215
- package/_esm/format/index.js +0 -2
- package/_esm/format/index.js.flow +0 -4
- package/_esm/format/short.js +0 -4
- package/_esm/format/short.js.flow +0 -8
- package/_esm/index.js.flow +0 -63
- package/_esm/result.js +0 -148
- package/_esm/result.js.flow +0 -174
- package/core/mapping.js +0 -67
- package/core/mapping.js.flow +0 -54
- package/format/index.js +0 -12
- package/format/index.js.flow +0 -4
- package/format/inline.js.flow +0 -122
- package/format/short.js +0 -10
- package/format/short.js.flow +0 -8
package/core/composition.js
CHANGED
|
@@ -2,27 +2,26 @@
|
|
|
2
2
|
|
|
3
3
|
exports.__esModule = true;
|
|
4
4
|
exports.compose = compose;
|
|
5
|
-
exports.map = map;
|
|
6
5
|
exports.predicate = predicate;
|
|
6
|
+
exports.prep = prep;
|
|
7
|
+
exports.transform = transform;
|
|
7
8
|
|
|
8
|
-
var
|
|
9
|
+
var _result = require("../result");
|
|
9
10
|
|
|
10
11
|
var _annotate = require("../annotate");
|
|
11
12
|
|
|
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
13
|
/**
|
|
17
|
-
*
|
|
18
|
-
*
|
|
14
|
+
* Accepts any value the given decoder accepts, and on success, will call the
|
|
15
|
+
* mapper value **on the decoded result**. If the mapper function throws an
|
|
16
|
+
* error, the whole decoder will fail using the error message as the failure
|
|
17
|
+
* reason.
|
|
19
18
|
*/
|
|
20
|
-
function
|
|
19
|
+
function transform(decoder, transformFn) {
|
|
21
20
|
return compose(decoder, function (x) {
|
|
22
21
|
try {
|
|
23
|
-
return
|
|
22
|
+
return (0, _result.ok)(transformFn(x));
|
|
24
23
|
} catch (e) {
|
|
25
|
-
return
|
|
24
|
+
return (0, _result.err)((0, _annotate.annotate)(x, e instanceof Error ? e.message : String(e)));
|
|
26
25
|
}
|
|
27
26
|
});
|
|
28
27
|
}
|
|
@@ -40,7 +39,7 @@ function map(decoder, mapper) {
|
|
|
40
39
|
|
|
41
40
|
function compose(decoder, next) {
|
|
42
41
|
return function (blob) {
|
|
43
|
-
return
|
|
42
|
+
return (0, _result.andThen)(decoder(blob), next);
|
|
44
43
|
};
|
|
45
44
|
}
|
|
46
45
|
/**
|
|
@@ -49,8 +48,35 @@ function compose(decoder, next) {
|
|
|
49
48
|
*/
|
|
50
49
|
|
|
51
50
|
|
|
52
|
-
function predicate(
|
|
53
|
-
return function (
|
|
54
|
-
return
|
|
51
|
+
function predicate(decoder, predicateFn, msg) {
|
|
52
|
+
return function (blob) {
|
|
53
|
+
return (0, _result.andThen)(decoder(blob), function (value) {
|
|
54
|
+
return predicateFn(value) ? (0, _result.ok)(value) : (0, _result.err)((0, _annotate.annotate)(value, msg));
|
|
55
|
+
});
|
|
56
|
+
};
|
|
57
|
+
}
|
|
58
|
+
/**
|
|
59
|
+
* Pre-process the data input before passing it into the decoder. This gives
|
|
60
|
+
* you the ability to arbitrarily customize the input on the fly before passing
|
|
61
|
+
* it to the decoder. Of course, the input value at that point is still of
|
|
62
|
+
* `unknown` type, so you will have to deal with that accordingly.
|
|
63
|
+
*/
|
|
64
|
+
|
|
65
|
+
|
|
66
|
+
function prep(mapperFn, decoder) {
|
|
67
|
+
return function (blob) {
|
|
68
|
+
var blob2;
|
|
69
|
+
|
|
70
|
+
try {
|
|
71
|
+
blob2 = mapperFn(blob);
|
|
72
|
+
} catch (e) {
|
|
73
|
+
return (0, _result.err)((0, _annotate.annotate)(blob, e.message));
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
return (0, _result.orElse)(decoder(blob2), function (ann) {
|
|
77
|
+
return (0, _result.err)((0, _annotate.annotate)(blob, ann.text));
|
|
78
|
+
} // ^^^^ Annotates the _original_ input value
|
|
79
|
+
// (instead of echoing back blob2 in the output)
|
|
80
|
+
);
|
|
55
81
|
};
|
|
56
82
|
}
|
package/core/composition.js.flow
CHANGED
|
@@ -1,19 +1,21 @@
|
|
|
1
1
|
// @flow strict
|
|
2
2
|
|
|
3
|
-
import
|
|
3
|
+
import { andThen, err, ok, orElse } from '../result';
|
|
4
4
|
import { annotate } from '../annotate';
|
|
5
5
|
import type { Decoder } from '../_types';
|
|
6
6
|
|
|
7
7
|
/**
|
|
8
|
-
*
|
|
9
|
-
*
|
|
8
|
+
* Accepts any value the given decoder accepts, and on success, will call the
|
|
9
|
+
* mapper value **on the decoded result**. If the mapper function throws an
|
|
10
|
+
* error, the whole decoder will fail using the error message as the failure
|
|
11
|
+
* reason.
|
|
10
12
|
*/
|
|
11
|
-
export function
|
|
13
|
+
export function transform<T, V>(decoder: Decoder<T>, transformFn: (T) => V): Decoder<V> {
|
|
12
14
|
return compose(decoder, (x) => {
|
|
13
15
|
try {
|
|
14
|
-
return
|
|
16
|
+
return ok(transformFn(x));
|
|
15
17
|
} catch (e) {
|
|
16
|
-
return
|
|
18
|
+
return err(annotate(x, e instanceof Error ? e.message : String(e)));
|
|
17
19
|
}
|
|
18
20
|
});
|
|
19
21
|
}
|
|
@@ -29,15 +31,44 @@ export function map<T, V>(decoder: Decoder<T>, mapper: (T) => V): Decoder<V> {
|
|
|
29
31
|
* argument.
|
|
30
32
|
*/
|
|
31
33
|
export function compose<T, V>(decoder: Decoder<T>, next: Decoder<V, T>): Decoder<V> {
|
|
32
|
-
return (blob: mixed) =>
|
|
34
|
+
return (blob: mixed) => andThen(decoder(blob), next);
|
|
33
35
|
}
|
|
34
36
|
|
|
35
37
|
/**
|
|
36
38
|
* Factory function returning a Decoder<T>, given a predicate function that
|
|
37
39
|
* accepts/rejects the input of type T.
|
|
38
40
|
*/
|
|
39
|
-
export function predicate<T>(
|
|
40
|
-
|
|
41
|
-
|
|
41
|
+
export function predicate<T>(
|
|
42
|
+
decoder: Decoder<T>,
|
|
43
|
+
predicateFn: (T) => boolean,
|
|
44
|
+
msg: string,
|
|
45
|
+
): Decoder<T> {
|
|
46
|
+
return (blob: mixed) =>
|
|
47
|
+
andThen(decoder(blob), (value) =>
|
|
48
|
+
predicateFn(value) ? ok(value) : err(annotate(value, msg)),
|
|
49
|
+
);
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* Pre-process the data input before passing it into the decoder. This gives
|
|
54
|
+
* you the ability to arbitrarily customize the input on the fly before passing
|
|
55
|
+
* it to the decoder. Of course, the input value at that point is still of
|
|
56
|
+
* `unknown` type, so you will have to deal with that accordingly.
|
|
57
|
+
*/
|
|
58
|
+
export function prep<I, T>(mapperFn: (mixed) => I, decoder: Decoder<T, I>): Decoder<T> {
|
|
59
|
+
return (blob: mixed) => {
|
|
60
|
+
let blob2;
|
|
61
|
+
try {
|
|
62
|
+
blob2 = mapperFn(blob);
|
|
63
|
+
} catch (e) {
|
|
64
|
+
return err(annotate(blob, e.message));
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
return orElse(
|
|
68
|
+
decoder(blob2),
|
|
69
|
+
(ann) => err(annotate(blob, ann.text)),
|
|
70
|
+
// ^^^^ Annotates the _original_ input value
|
|
71
|
+
// (instead of echoing back blob2 in the output)
|
|
72
|
+
);
|
|
42
73
|
};
|
|
43
74
|
}
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
import { andThen, err, ok, orElse } from '../result.mjs';
|
|
2
|
+
import { annotate } from '../annotate.mjs';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Accepts any value the given decoder accepts, and on success, will call the
|
|
6
|
+
* mapper value **on the decoded result**. If the mapper function throws an
|
|
7
|
+
* error, the whole decoder will fail using the error message as the failure
|
|
8
|
+
* reason.
|
|
9
|
+
*/
|
|
10
|
+
export function transform(decoder, transformFn) {
|
|
11
|
+
return compose(decoder, function (x) {
|
|
12
|
+
try {
|
|
13
|
+
return ok(transformFn(x));
|
|
14
|
+
} catch (e) {
|
|
15
|
+
return err(annotate(x, e instanceof Error ? e.message : String(e)));
|
|
16
|
+
}
|
|
17
|
+
});
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* Compose two decoders by passing the result of the first into the second.
|
|
21
|
+
* The second decoder may assume as its input type the output type of the first
|
|
22
|
+
* decoder (so it's not necessary to accept the typical "mixed"). This is
|
|
23
|
+
* useful for "narrowing down" the checks. For example, if you want to write
|
|
24
|
+
* a decoder for positive numbers, you can compose it from an existing decoder
|
|
25
|
+
* for any number, and a decoder that, assuming a number, checks if it's
|
|
26
|
+
* positive. Very often combined with the predicate() helper as the second
|
|
27
|
+
* argument.
|
|
28
|
+
*/
|
|
29
|
+
|
|
30
|
+
export function compose(decoder, next) {
|
|
31
|
+
return function (blob) {
|
|
32
|
+
return 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
|
+
|
|
40
|
+
export function predicate(decoder, predicateFn, msg) {
|
|
41
|
+
return function (blob) {
|
|
42
|
+
return andThen(decoder(blob), function (value) {
|
|
43
|
+
return predicateFn(value) ? ok(value) : err(annotate(value, msg));
|
|
44
|
+
});
|
|
45
|
+
};
|
|
46
|
+
}
|
|
47
|
+
/**
|
|
48
|
+
* Pre-process the data input before passing it into the decoder. This gives
|
|
49
|
+
* you the ability to arbitrarily customize the input on the fly before passing
|
|
50
|
+
* it to the decoder. Of course, the input value at that point is still of
|
|
51
|
+
* `unknown` type, so you will have to deal with that accordingly.
|
|
52
|
+
*/
|
|
53
|
+
|
|
54
|
+
export function prep(mapperFn, decoder) {
|
|
55
|
+
return function (blob) {
|
|
56
|
+
var blob2;
|
|
57
|
+
|
|
58
|
+
try {
|
|
59
|
+
blob2 = mapperFn(blob);
|
|
60
|
+
} catch (e) {
|
|
61
|
+
return err(annotate(blob, e.message));
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
return orElse(decoder(blob2), function (ann) {
|
|
65
|
+
return err(annotate(blob, ann.text));
|
|
66
|
+
} // ^^^^ Annotates the _original_ input value
|
|
67
|
+
// (instead of echoing back blob2 in the output)
|
|
68
|
+
);
|
|
69
|
+
};
|
|
70
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { Decoder, Scalar } from '../_types';
|
|
2
|
+
|
|
3
|
+
// Constants
|
|
4
|
+
|
|
5
|
+
export const null_: Decoder<null>;
|
|
6
|
+
export const undefined_: Decoder<undefined>;
|
|
7
|
+
export function constant<T extends Scalar>(value: T): Decoder<T>;
|
|
8
|
+
export function hardcoded<T extends Scalar>(value: T): Decoder<T>;
|
|
9
|
+
export function hardcoded<T>(value: T): Decoder<T>;
|
|
10
|
+
export const mixed: Decoder<unknown>;
|
|
11
|
+
export const unknown: Decoder<unknown>;
|
package/core/constants.js
CHANGED
|
@@ -5,19 +5,15 @@ exports.constant = constant;
|
|
|
5
5
|
exports.hardcoded = hardcoded;
|
|
6
6
|
exports.unknown = exports.undefined_ = exports.null_ = exports.mixed = void 0;
|
|
7
7
|
|
|
8
|
-
var Result = _interopRequireWildcard(require("../result"));
|
|
9
|
-
|
|
10
8
|
var _annotate = require("../annotate");
|
|
11
9
|
|
|
12
|
-
|
|
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; }
|
|
10
|
+
var _result = require("../result");
|
|
15
11
|
|
|
16
12
|
/**
|
|
17
13
|
* Decoder that only returns Ok for `null` inputs. Err otherwise.
|
|
18
14
|
*/
|
|
19
15
|
var null_ = function null_(blob) {
|
|
20
|
-
return blob === null ?
|
|
16
|
+
return blob === null ? (0, _result.ok)(blob) : (0, _result.err)((0, _annotate.annotate)(blob, 'Must be null'));
|
|
21
17
|
};
|
|
22
18
|
/**
|
|
23
19
|
* Decoder that only returns Ok for `undefined` inputs. Err otherwise.
|
|
@@ -27,7 +23,7 @@ var null_ = function null_(blob) {
|
|
|
27
23
|
exports.null_ = null_;
|
|
28
24
|
|
|
29
25
|
var undefined_ = function undefined_(blob) {
|
|
30
|
-
return blob === undefined ?
|
|
26
|
+
return blob === undefined ? (0, _result.ok)(blob) : (0, _result.err)((0, _annotate.annotate)(blob, 'Must be undefined'));
|
|
31
27
|
};
|
|
32
28
|
/**
|
|
33
29
|
* Decoder that only returns Ok for the given value constant. Err otherwise.
|
|
@@ -38,7 +34,7 @@ exports.undefined_ = undefined_;
|
|
|
38
34
|
|
|
39
35
|
function constant(value) {
|
|
40
36
|
return function (blob) {
|
|
41
|
-
return blob === value ?
|
|
37
|
+
return blob === value ? (0, _result.ok)(value) : (0, _result.err)((0, _annotate.annotate)(blob, "Must be constant " + String(value)));
|
|
42
38
|
};
|
|
43
39
|
}
|
|
44
40
|
/**
|
|
@@ -48,7 +44,7 @@ function constant(value) {
|
|
|
48
44
|
|
|
49
45
|
function hardcoded(value) {
|
|
50
46
|
return function () {
|
|
51
|
-
return
|
|
47
|
+
return (0, _result.ok)(value);
|
|
52
48
|
};
|
|
53
49
|
}
|
|
54
50
|
/**
|
|
@@ -57,7 +53,7 @@ function hardcoded(value) {
|
|
|
57
53
|
|
|
58
54
|
|
|
59
55
|
var unknown = function unknown(blob) {
|
|
60
|
-
return
|
|
56
|
+
return (0, _result.ok)(blob);
|
|
61
57
|
};
|
|
62
58
|
/**
|
|
63
59
|
* Alias of unknown.
|
package/core/constants.js.flow
CHANGED
|
@@ -1,22 +1,20 @@
|
|
|
1
1
|
// @flow strict
|
|
2
2
|
|
|
3
|
-
import * as Result from '../result';
|
|
4
3
|
import { annotate } from '../annotate';
|
|
4
|
+
import { err, ok } from '../result';
|
|
5
5
|
import type { Decoder, Scalar } from '../_types';
|
|
6
6
|
|
|
7
7
|
/**
|
|
8
8
|
* Decoder that only returns Ok for `null` inputs. Err otherwise.
|
|
9
9
|
*/
|
|
10
10
|
export const null_: Decoder<null> = (blob: mixed) =>
|
|
11
|
-
blob === null ?
|
|
11
|
+
blob === null ? ok(blob) : err(annotate(blob, 'Must be null'));
|
|
12
12
|
|
|
13
13
|
/**
|
|
14
14
|
* Decoder that only returns Ok for `undefined` inputs. Err otherwise.
|
|
15
15
|
*/
|
|
16
16
|
export const undefined_: Decoder<void> = (blob: mixed) =>
|
|
17
|
-
blob === undefined
|
|
18
|
-
? Result.ok(blob)
|
|
19
|
-
: Result.err(annotate(blob, 'Must be undefined'));
|
|
17
|
+
blob === undefined ? ok(blob) : err(annotate(blob, 'Must be undefined'));
|
|
20
18
|
|
|
21
19
|
/**
|
|
22
20
|
* Decoder that only returns Ok for the given value constant. Err otherwise.
|
|
@@ -24,21 +22,21 @@ export const undefined_: Decoder<void> = (blob: mixed) =>
|
|
|
24
22
|
export function constant<T: Scalar>(value: T): Decoder<T> {
|
|
25
23
|
return (blob: mixed) =>
|
|
26
24
|
blob === value
|
|
27
|
-
?
|
|
28
|
-
:
|
|
25
|
+
? ok(value)
|
|
26
|
+
: err(annotate(blob, `Must be constant ${String(value)}`));
|
|
29
27
|
}
|
|
30
28
|
|
|
31
29
|
/**
|
|
32
30
|
* Decoder that always returns Ok for the given hardcoded value, no matter what the input.
|
|
33
31
|
*/
|
|
34
32
|
export function hardcoded<T>(value: T): Decoder<T> {
|
|
35
|
-
return () =>
|
|
33
|
+
return () => ok(value);
|
|
36
34
|
}
|
|
37
35
|
|
|
38
36
|
/**
|
|
39
37
|
* Decoder that always returns Ok for the given hardcoded value, no matter what the input.
|
|
40
38
|
*/
|
|
41
|
-
export const unknown: Decoder<mixed> = (blob: mixed) =>
|
|
39
|
+
export const unknown: Decoder<mixed> = (blob: mixed) => ok(blob);
|
|
42
40
|
|
|
43
41
|
/**
|
|
44
42
|
* Alias of unknown.
|
|
@@ -1,18 +1,18 @@
|
|
|
1
|
-
import
|
|
2
|
-
import {
|
|
1
|
+
import { annotate } from '../annotate.mjs';
|
|
2
|
+
import { err, ok } from '../result.mjs';
|
|
3
3
|
|
|
4
4
|
/**
|
|
5
5
|
* Decoder that only returns Ok for `null` inputs. Err otherwise.
|
|
6
6
|
*/
|
|
7
7
|
export var null_ = function null_(blob) {
|
|
8
|
-
return blob === null ?
|
|
8
|
+
return blob === null ? ok(blob) : err(annotate(blob, 'Must be null'));
|
|
9
9
|
};
|
|
10
10
|
/**
|
|
11
11
|
* Decoder that only returns Ok for `undefined` inputs. Err otherwise.
|
|
12
12
|
*/
|
|
13
13
|
|
|
14
14
|
export var undefined_ = function undefined_(blob) {
|
|
15
|
-
return blob === undefined ?
|
|
15
|
+
return blob === undefined ? ok(blob) : err(annotate(blob, 'Must be undefined'));
|
|
16
16
|
};
|
|
17
17
|
/**
|
|
18
18
|
* Decoder that only returns Ok for the given value constant. Err otherwise.
|
|
@@ -20,7 +20,7 @@ export var undefined_ = function undefined_(blob) {
|
|
|
20
20
|
|
|
21
21
|
export function constant(value) {
|
|
22
22
|
return function (blob) {
|
|
23
|
-
return blob === value ?
|
|
23
|
+
return blob === value ? ok(value) : err(annotate(blob, "Must be constant " + String(value)));
|
|
24
24
|
};
|
|
25
25
|
}
|
|
26
26
|
/**
|
|
@@ -29,7 +29,7 @@ export function constant(value) {
|
|
|
29
29
|
|
|
30
30
|
export function hardcoded(value) {
|
|
31
31
|
return function () {
|
|
32
|
-
return
|
|
32
|
+
return ok(value);
|
|
33
33
|
};
|
|
34
34
|
}
|
|
35
35
|
/**
|
|
@@ -37,7 +37,7 @@ export function hardcoded(value) {
|
|
|
37
37
|
*/
|
|
38
38
|
|
|
39
39
|
export var unknown = function unknown(blob) {
|
|
40
|
-
return
|
|
40
|
+
return ok(blob);
|
|
41
41
|
};
|
|
42
42
|
/**
|
|
43
43
|
* Alias of unknown.
|
package/core/date.d.ts
ADDED
package/core/date.js
CHANGED
|
@@ -3,19 +3,15 @@
|
|
|
3
3
|
exports.__esModule = true;
|
|
4
4
|
exports.iso8601 = exports.date = void 0;
|
|
5
5
|
|
|
6
|
-
var Result = _interopRequireWildcard(require("../result"));
|
|
7
|
-
|
|
8
6
|
var _annotate = require("../annotate");
|
|
9
7
|
|
|
10
|
-
var
|
|
8
|
+
var _result = require("../result");
|
|
11
9
|
|
|
12
|
-
var
|
|
10
|
+
var _utils = require("../_utils");
|
|
13
11
|
|
|
14
12
|
var _string = require("./string");
|
|
15
13
|
|
|
16
|
-
|
|
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; }
|
|
14
|
+
var _composition = require("./composition");
|
|
19
15
|
|
|
20
16
|
// Only matches the shape. This "over-matches" some values that still aren't
|
|
21
17
|
// valid dates (like 9999-99-99), but those will be caught by JS Date's
|
|
@@ -23,7 +19,7 @@ function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj &&
|
|
|
23
19
|
var iso8601_re = /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(?:[.]\d+)?(?:Z|[+-]\d{2}:?\d{2})$/;
|
|
24
20
|
|
|
25
21
|
var date = function date(value) {
|
|
26
|
-
return (0, _utils.isDate)(value) ?
|
|
22
|
+
return (0, _utils.isDate)(value) ? (0, _result.ok)(value) : (0, _result.err)((0, _annotate.annotate)(value, 'Must be a Date'));
|
|
27
23
|
};
|
|
28
24
|
/**
|
|
29
25
|
* Decoder that only returns Ok for strings that are valid ISO8601 date
|
|
@@ -32,7 +28,7 @@ var date = function date(value) {
|
|
|
32
28
|
|
|
33
29
|
|
|
34
30
|
exports.date = date;
|
|
35
|
-
var iso8601 = (0, _composition.
|
|
31
|
+
var iso8601 = (0, _composition.transform)( // Input itself needs to match the ISO8601 regex...
|
|
36
32
|
(0, _string.regex)(iso8601_re, 'Must be ISO8601 format'), // Make sure it is a _valid_ date
|
|
37
33
|
function (value) {
|
|
38
34
|
var date = new Date(value);
|
package/core/date.js.flow
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
// @flow strict
|
|
2
2
|
|
|
3
|
-
import * as Result from '../result';
|
|
4
3
|
import { annotate } from '../annotate';
|
|
4
|
+
import { err, ok } from '../result';
|
|
5
5
|
import { isDate } from '../_utils';
|
|
6
|
-
import { map } from './composition';
|
|
7
6
|
import { regex } from './string';
|
|
7
|
+
import { transform } from './composition';
|
|
8
8
|
import type { Decoder } from '../_types';
|
|
9
9
|
|
|
10
10
|
// $FlowFixMe[unclear-type] (not really an issue) - deliberate casting
|
|
@@ -17,15 +17,13 @@ const iso8601_re =
|
|
|
17
17
|
/^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(?:[.]\d+)?(?:Z|[+-]\d{2}:?\d{2})$/;
|
|
18
18
|
|
|
19
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'));
|
|
20
|
+
isDate(value) ? ok(((value: cast): Date)) : err(annotate(value, 'Must be a Date'));
|
|
23
21
|
|
|
24
22
|
/**
|
|
25
23
|
* Decoder that only returns Ok for strings that are valid ISO8601 date
|
|
26
24
|
* strings. Err otherwise.
|
|
27
25
|
*/
|
|
28
|
-
export const iso8601: Decoder<Date> =
|
|
26
|
+
export const iso8601: Decoder<Date> = transform(
|
|
29
27
|
// Input itself needs to match the ISO8601 regex...
|
|
30
28
|
regex(iso8601_re, 'Must be ISO8601 format'),
|
|
31
29
|
|
|
@@ -1,21 +1,21 @@
|
|
|
1
|
-
import
|
|
2
|
-
import {
|
|
3
|
-
import { isDate } from '../_utils';
|
|
4
|
-
import {
|
|
5
|
-
import {
|
|
1
|
+
import { annotate } from '../annotate.mjs';
|
|
2
|
+
import { err, ok } from '../result.mjs';
|
|
3
|
+
import { isDate } from '../_utils.mjs';
|
|
4
|
+
import { regex } from './string.mjs';
|
|
5
|
+
import { transform } from './composition.mjs';
|
|
6
6
|
// Only matches the shape. This "over-matches" some values that still aren't
|
|
7
7
|
// valid dates (like 9999-99-99), but those will be caught by JS Date's
|
|
8
8
|
// internal validations
|
|
9
9
|
var iso8601_re = /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(?:[.]\d+)?(?:Z|[+-]\d{2}:?\d{2})$/;
|
|
10
10
|
export var date = function date(value) {
|
|
11
|
-
return isDate(value) ?
|
|
11
|
+
return isDate(value) ? ok(value) : err(annotate(value, 'Must be a Date'));
|
|
12
12
|
};
|
|
13
13
|
/**
|
|
14
14
|
* Decoder that only returns Ok for strings that are valid ISO8601 date
|
|
15
15
|
* strings. Err otherwise.
|
|
16
16
|
*/
|
|
17
17
|
|
|
18
|
-
export var iso8601 =
|
|
18
|
+
export var iso8601 = transform( // Input itself needs to match the ISO8601 regex...
|
|
19
19
|
regex(iso8601_re, 'Must be ISO8601 format'), // Make sure it is a _valid_ date
|
|
20
20
|
function (value) {
|
|
21
21
|
var date = new Date(value);
|
package/core/describe.js
CHANGED
|
@@ -3,13 +3,9 @@
|
|
|
3
3
|
exports.__esModule = true;
|
|
4
4
|
exports.describe = describe;
|
|
5
5
|
|
|
6
|
-
var Result = _interopRequireWildcard(require("../result"));
|
|
7
|
-
|
|
8
6
|
var _annotate = require("../annotate");
|
|
9
7
|
|
|
10
|
-
|
|
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; }
|
|
8
|
+
var _result = require("../result");
|
|
13
9
|
|
|
14
10
|
/**
|
|
15
11
|
* Wrap another decoder, and override the error message in case it fails. This
|
|
@@ -19,7 +15,7 @@ function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj &&
|
|
|
19
15
|
*/
|
|
20
16
|
function describe(decoder, message) {
|
|
21
17
|
return function (blob) {
|
|
22
|
-
return
|
|
18
|
+
return (0, _result.mapError)(decoder(blob), function (err) {
|
|
23
19
|
return (0, _annotate.annotate)(err, message);
|
|
24
20
|
});
|
|
25
21
|
};
|
package/core/describe.js.flow
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
// @flow strict
|
|
2
2
|
|
|
3
|
-
import * as Result from '../result';
|
|
4
3
|
import { annotate } from '../annotate';
|
|
4
|
+
import { mapError } from '../result';
|
|
5
5
|
import type { Decoder } from '../_types';
|
|
6
6
|
|
|
7
7
|
/**
|
|
@@ -12,6 +12,6 @@ import type { Decoder } from '../_types';
|
|
|
12
12
|
*/
|
|
13
13
|
export function describe<T>(decoder: Decoder<T>, message: string): Decoder<T> {
|
|
14
14
|
return (blob: mixed) => {
|
|
15
|
-
return
|
|
15
|
+
return mapError(decoder(blob), (err) => annotate(err, message));
|
|
16
16
|
};
|
|
17
17
|
}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import
|
|
2
|
-
import {
|
|
1
|
+
import { annotate } from '../annotate.mjs';
|
|
2
|
+
import { mapError } from '../result.mjs';
|
|
3
3
|
|
|
4
4
|
/**
|
|
5
5
|
* Wrap another decoder, and override the error message in case it fails. This
|
|
@@ -9,7 +9,7 @@ import { annotate } from '../annotate';
|
|
|
9
9
|
*/
|
|
10
10
|
export function describe(decoder, message) {
|
|
11
11
|
return function (blob) {
|
|
12
|
-
return
|
|
12
|
+
return mapError(decoder(blob), function (err) {
|
|
13
13
|
return annotate(err, message);
|
|
14
14
|
});
|
|
15
15
|
};
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { Decoder, DecoderType } from '../_types';
|
|
2
|
+
|
|
3
|
+
export type Values<T extends object> = T[keyof T];
|
|
4
|
+
|
|
5
|
+
export function taggedUnion<O extends { [key: string]: Decoder<any> }>(
|
|
6
|
+
field: string,
|
|
7
|
+
mapping: O,
|
|
8
|
+
): Decoder<Values<{ [key in keyof O]: DecoderType<O[key]> }>>;
|
package/core/dispatch.js
CHANGED
|
@@ -1,17 +1,15 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
|
|
3
3
|
exports.__esModule = true;
|
|
4
|
-
exports.
|
|
4
|
+
exports.taggedUnion = taggedUnion;
|
|
5
5
|
|
|
6
|
-
var
|
|
6
|
+
var _result = require("../result");
|
|
7
7
|
|
|
8
8
|
var _object2 = require("./object");
|
|
9
9
|
|
|
10
10
|
var _either = require("./either");
|
|
11
11
|
|
|
12
|
-
|
|
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; }
|
|
12
|
+
var _composition = require("./composition");
|
|
15
13
|
|
|
16
14
|
/**
|
|
17
15
|
* Dispatches to one of several given decoders, based on the value found at
|
|
@@ -35,25 +33,25 @@ function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj &&
|
|
|
35
33
|
* Then these two decoders are equivalent:
|
|
36
34
|
*
|
|
37
35
|
* const shape = either(rectangle, circle)
|
|
38
|
-
* const shape =
|
|
36
|
+
* const shape = taggedUnion('type', { rectangle, circle })
|
|
39
37
|
*
|
|
40
38
|
* Will be of type Decoder<Rectangle | Circle>.
|
|
41
39
|
*
|
|
42
|
-
* But
|
|
43
|
-
*
|
|
44
|
-
*
|
|
40
|
+
* But `taggedUnion` will typically be more runtime-efficient. The reason is
|
|
41
|
+
* that it will first do minimal work to "look ahead" into the `type` field
|
|
42
|
+
* here, and based on that value, pick the decoder to invoke.
|
|
45
43
|
*
|
|
46
44
|
* The `either` version will simply try to invoke each decoder, until it finds
|
|
47
45
|
* one that matches.
|
|
48
46
|
*
|
|
49
|
-
* Also, the error messages will be less ambiguous using `
|
|
47
|
+
* Also, the error messages will be less ambiguous using `taggedUnion()`.
|
|
50
48
|
*/
|
|
51
|
-
function
|
|
49
|
+
function taggedUnion(field, mapping) {
|
|
52
50
|
var _object;
|
|
53
51
|
|
|
54
|
-
var base = (0, _object2.object)((_object = {}, _object[field] = (0, _either.oneOf)(Object.keys(mapping)), _object));
|
|
52
|
+
var base = (0, _object2.object)((_object = {}, _object[field] = (0, _composition.prep)(String, (0, _either.oneOf)(Object.keys(mapping))), _object));
|
|
55
53
|
return function (blob) {
|
|
56
|
-
return
|
|
54
|
+
return (0, _result.andThen)(base(blob), function (baseObj) {
|
|
57
55
|
var decoderName = baseObj[field];
|
|
58
56
|
var decoder = mapping[decoderName];
|
|
59
57
|
return decoder(blob);
|