decoders 2.0.0-beta9 → 2.0.0
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 +56 -29
- package/Decoder.d.ts +94 -0
- package/Decoder.js +222 -0
- package/Decoder.js.flow +286 -0
- package/Decoder.mjs +215 -0
- package/README.md +122 -1507
- package/_utils.d.ts +0 -1
- package/_utils.js +11 -17
- package/_utils.js.flow +13 -17
- package/_utils.mjs +10 -14
- package/format.d.ts +4 -2
- package/format.js +1 -1
- package/format.js.flow +3 -1
- package/format.mjs +1 -1
- package/index.d.ts +29 -31
- package/index.js +62 -84
- package/index.js.flow +30 -48
- package/index.mjs +11 -36
- package/{core → lib}/_helpers.d.ts +0 -0
- package/lib/arrays.d.ts +59 -0
- package/lib/arrays.js +139 -0
- package/lib/arrays.js.flow +138 -0
- package/lib/arrays.mjs +124 -0
- package/lib/basics.d.ts +93 -0
- package/lib/basics.js +144 -0
- package/lib/basics.js.flow +124 -0
- package/lib/basics.mjs +120 -0
- package/lib/booleans.d.ts +16 -0
- package/lib/booleans.js +35 -0
- package/lib/booleans.js.flow +22 -0
- package/lib/booleans.mjs +25 -0
- package/lib/dates.d.ts +15 -0
- package/lib/dates.js +44 -0
- package/lib/dates.js.flow +40 -0
- package/lib/dates.mjs +34 -0
- package/lib/json.d.ts +35 -0
- package/lib/json.js +55 -0
- package/lib/json.js.flow +50 -0
- package/lib/json.mjs +40 -0
- package/lib/numbers.d.ts +31 -0
- package/lib/numbers.js +51 -0
- package/lib/numbers.js.flow +48 -0
- package/lib/numbers.mjs +41 -0
- package/lib/objects.d.ts +75 -0
- package/{core/object.js → lib/objects.js} +78 -85
- package/{core/object.js.flow → lib/objects.js.flow} +89 -102
- package/{core/object.mjs → lib/objects.mjs} +77 -82
- package/lib/strings.d.ts +56 -0
- package/lib/strings.js +101 -0
- package/lib/strings.js.flow +90 -0
- package/lib/strings.mjs +82 -0
- package/lib/unions.d.ts +55 -0
- package/lib/unions.js +160 -0
- package/{core/either.js.flow → lib/unions.js.flow} +67 -17
- package/lib/unions.mjs +146 -0
- package/lib/utilities.d.ts +34 -0
- package/lib/utilities.js +75 -0
- package/lib/utilities.js.flow +65 -0
- package/lib/utilities.mjs +60 -0
- package/package.json +64 -17
- package/result.d.ts +0 -23
- package/result.js +0 -68
- package/result.js.flow +0 -72
- package/result.mjs +0 -54
- package/_guard.d.ts +0 -7
- package/_guard.js +0 -22
- package/_guard.js.flow +0 -20
- package/_guard.mjs +0 -15
- package/_types.d.ts +0 -13
- package/_types.js +0 -1
- package/_types.js.flow +0 -20
- package/_types.mjs +0 -0
- package/core/array.d.ts +0 -8
- package/core/array.js +0 -115
- package/core/array.js.flow +0 -107
- package/core/array.mjs +0 -100
- package/core/boolean.d.ts +0 -5
- package/core/boolean.js +0 -40
- package/core/boolean.js.flow +0 -27
- package/core/boolean.mjs +0 -28
- package/core/composition.d.ts +0 -18
- package/core/composition.js +0 -82
- package/core/composition.js.flow +0 -74
- package/core/composition.mjs +0 -70
- package/core/constants.d.ts +0 -11
- package/core/constants.js +0 -65
- package/core/constants.js.flow +0 -44
- package/core/constants.mjs +0 -46
- package/core/date.d.ts +0 -4
- package/core/date.js +0 -42
- package/core/date.js.flow +0 -38
- package/core/date.mjs +0 -28
- package/core/describe.d.ts +0 -3
- package/core/describe.js +0 -22
- package/core/describe.js.flow +0 -17
- package/core/describe.mjs +0 -16
- package/core/dispatch.d.ts +0 -8
- package/core/dispatch.js +0 -60
- package/core/dispatch.js.flow +0 -59
- package/core/dispatch.mjs +0 -52
- package/core/either.d.ts +0 -66
- package/core/either.js +0 -101
- package/core/either.mjs +0 -90
- package/core/fail.d.ts +0 -3
- package/core/fail.js +0 -17
- package/core/fail.js.flow +0 -12
- package/core/fail.mjs +0 -11
- package/core/instanceOf.d.ts +0 -3
- package/core/instanceOf.js +0 -15
- package/core/instanceOf.js.flow +0 -20
- package/core/instanceOf.mjs +0 -8
- package/core/json.d.ts +0 -11
- package/core/json.js +0 -31
- package/core/json.js.flow +0 -28
- package/core/json.mjs +0 -15
- package/core/lazy.d.ts +0 -3
- package/core/lazy.js +0 -16
- package/core/lazy.js.flow +0 -15
- package/core/lazy.mjs +0 -11
- package/core/number.d.ts +0 -6
- package/core/number.js +0 -36
- package/core/number.js.flow +0 -40
- package/core/number.mjs +0 -25
- package/core/object.d.ts +0 -38
- package/core/optional.d.ts +0 -5
- package/core/optional.js +0 -50
- package/core/optional.js.flow +0 -41
- package/core/optional.mjs +0 -38
- package/core/string.d.ts +0 -13
- package/core/string.js +0 -80
- package/core/string.js.flow +0 -72
- package/core/string.mjs +0 -58
- package/core/tuple.d.ts +0 -30
- package/core/tuple.js +0 -54
- package/core/tuple.js.flow +0 -51
- package/core/tuple.mjs +0 -45
package/core/composition.js
DELETED
|
@@ -1,82 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
|
|
3
|
-
exports.__esModule = true;
|
|
4
|
-
exports.compose = compose;
|
|
5
|
-
exports.predicate = predicate;
|
|
6
|
-
exports.prep = prep;
|
|
7
|
-
exports.transform = transform;
|
|
8
|
-
|
|
9
|
-
var _result = require("../result");
|
|
10
|
-
|
|
11
|
-
var _annotate = require("../annotate");
|
|
12
|
-
|
|
13
|
-
/**
|
|
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.
|
|
18
|
-
*/
|
|
19
|
-
function transform(decoder, transformFn) {
|
|
20
|
-
return compose(decoder, function (x) {
|
|
21
|
-
try {
|
|
22
|
-
return (0, _result.ok)(transformFn(x));
|
|
23
|
-
} catch (e) {
|
|
24
|
-
return (0, _result.err)((0, _annotate.annotate)(x, e instanceof Error ? e.message : String(e)));
|
|
25
|
-
}
|
|
26
|
-
});
|
|
27
|
-
}
|
|
28
|
-
/**
|
|
29
|
-
* Compose two decoders by passing the result of the first into the second.
|
|
30
|
-
* The second decoder may assume as its input type the output type of the first
|
|
31
|
-
* decoder (so it's not necessary to accept the typical "mixed"). This is
|
|
32
|
-
* useful for "narrowing down" the checks. For example, if you want to write
|
|
33
|
-
* a decoder for positive numbers, you can compose it from an existing decoder
|
|
34
|
-
* for any number, and a decoder that, assuming a number, checks if it's
|
|
35
|
-
* positive. Very often combined with the predicate() helper as the second
|
|
36
|
-
* argument.
|
|
37
|
-
*/
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
function compose(decoder, next) {
|
|
41
|
-
return function (blob) {
|
|
42
|
-
return (0, _result.andThen)(decoder(blob), next);
|
|
43
|
-
};
|
|
44
|
-
}
|
|
45
|
-
/**
|
|
46
|
-
* Factory function returning a Decoder<T>, given a predicate function that
|
|
47
|
-
* accepts/rejects the input of type T.
|
|
48
|
-
*/
|
|
49
|
-
|
|
50
|
-
|
|
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
|
-
);
|
|
81
|
-
};
|
|
82
|
-
}
|
package/core/composition.js.flow
DELETED
|
@@ -1,74 +0,0 @@
|
|
|
1
|
-
// @flow strict
|
|
2
|
-
|
|
3
|
-
import { andThen, err, ok, orElse } from '../result';
|
|
4
|
-
import { annotate } from '../annotate';
|
|
5
|
-
import type { Decoder } from '../_types';
|
|
6
|
-
|
|
7
|
-
/**
|
|
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.
|
|
12
|
-
*/
|
|
13
|
-
export function transform<T, V>(decoder: Decoder<T>, transformFn: (T) => V): Decoder<V> {
|
|
14
|
-
return compose(decoder, (x) => {
|
|
15
|
-
try {
|
|
16
|
-
return ok(transformFn(x));
|
|
17
|
-
} catch (e) {
|
|
18
|
-
return err(annotate(x, e instanceof Error ? e.message : String(e)));
|
|
19
|
-
}
|
|
20
|
-
});
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
/**
|
|
24
|
-
* Compose two decoders by passing the result of the first into the second.
|
|
25
|
-
* The second decoder may assume as its input type the output type of the first
|
|
26
|
-
* decoder (so it's not necessary to accept the typical "mixed"). This is
|
|
27
|
-
* useful for "narrowing down" the checks. For example, if you want to write
|
|
28
|
-
* a decoder for positive numbers, you can compose it from an existing decoder
|
|
29
|
-
* for any number, and a decoder that, assuming a number, checks if it's
|
|
30
|
-
* positive. Very often combined with the predicate() helper as the second
|
|
31
|
-
* argument.
|
|
32
|
-
*/
|
|
33
|
-
export function compose<T, V>(decoder: Decoder<T>, next: Decoder<V, T>): Decoder<V> {
|
|
34
|
-
return (blob: mixed) => andThen(decoder(blob), next);
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
/**
|
|
38
|
-
* Factory function returning a Decoder<T>, given a predicate function that
|
|
39
|
-
* accepts/rejects the input of type T.
|
|
40
|
-
*/
|
|
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
|
-
);
|
|
73
|
-
};
|
|
74
|
-
}
|
package/core/composition.mjs
DELETED
|
@@ -1,70 +0,0 @@
|
|
|
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
|
-
}
|
package/core/constants.d.ts
DELETED
|
@@ -1,11 +0,0 @@
|
|
|
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
DELETED
|
@@ -1,65 +0,0 @@
|
|
|
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 _annotate = require("../annotate");
|
|
9
|
-
|
|
10
|
-
var _result = require("../result");
|
|
11
|
-
|
|
12
|
-
/**
|
|
13
|
-
* Decoder that only returns Ok for `null` inputs. Err otherwise.
|
|
14
|
-
*/
|
|
15
|
-
var null_ = function null_(blob) {
|
|
16
|
-
return blob === null ? (0, _result.ok)(blob) : (0, _result.err)((0, _annotate.annotate)(blob, 'Must be null'));
|
|
17
|
-
};
|
|
18
|
-
/**
|
|
19
|
-
* Decoder that only returns Ok for `undefined` inputs. Err otherwise.
|
|
20
|
-
*/
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
exports.null_ = null_;
|
|
24
|
-
|
|
25
|
-
var undefined_ = function undefined_(blob) {
|
|
26
|
-
return blob === undefined ? (0, _result.ok)(blob) : (0, _result.err)((0, _annotate.annotate)(blob, 'Must be undefined'));
|
|
27
|
-
};
|
|
28
|
-
/**
|
|
29
|
-
* Decoder that only returns Ok for the given value constant. Err otherwise.
|
|
30
|
-
*/
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
exports.undefined_ = undefined_;
|
|
34
|
-
|
|
35
|
-
function constant(value) {
|
|
36
|
-
return function (blob) {
|
|
37
|
-
return blob === value ? (0, _result.ok)(value) : (0, _result.err)((0, _annotate.annotate)(blob, "Must be constant " + String(value)));
|
|
38
|
-
};
|
|
39
|
-
}
|
|
40
|
-
/**
|
|
41
|
-
* Decoder that always returns Ok for the given hardcoded value, no matter what the input.
|
|
42
|
-
*/
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
function hardcoded(value) {
|
|
46
|
-
return function () {
|
|
47
|
-
return (0, _result.ok)(value);
|
|
48
|
-
};
|
|
49
|
-
}
|
|
50
|
-
/**
|
|
51
|
-
* Decoder that always returns Ok for the given hardcoded value, no matter what the input.
|
|
52
|
-
*/
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
var unknown = function unknown(blob) {
|
|
56
|
-
return (0, _result.ok)(blob);
|
|
57
|
-
};
|
|
58
|
-
/**
|
|
59
|
-
* Alias of unknown.
|
|
60
|
-
*/
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
exports.unknown = unknown;
|
|
64
|
-
var mixed = unknown;
|
|
65
|
-
exports.mixed = mixed;
|
package/core/constants.js.flow
DELETED
|
@@ -1,44 +0,0 @@
|
|
|
1
|
-
// @flow strict
|
|
2
|
-
|
|
3
|
-
import { annotate } from '../annotate';
|
|
4
|
-
import { err, ok } from '../result';
|
|
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 ? ok(blob) : 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 ? ok(blob) : err(annotate(blob, 'Must be undefined'));
|
|
18
|
-
|
|
19
|
-
/**
|
|
20
|
-
* Decoder that only returns Ok for the given value constant. Err otherwise.
|
|
21
|
-
*/
|
|
22
|
-
export function constant<T: Scalar>(value: T): Decoder<T> {
|
|
23
|
-
return (blob: mixed) =>
|
|
24
|
-
blob === value
|
|
25
|
-
? ok(value)
|
|
26
|
-
: err(annotate(blob, `Must be constant ${String(value)}`));
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
/**
|
|
30
|
-
* Decoder that always returns Ok for the given hardcoded value, no matter what the input.
|
|
31
|
-
*/
|
|
32
|
-
export function hardcoded<T>(value: T): Decoder<T> {
|
|
33
|
-
return () => ok(value);
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
/**
|
|
37
|
-
* Decoder that always returns Ok for the given hardcoded value, no matter what the input.
|
|
38
|
-
*/
|
|
39
|
-
export const unknown: Decoder<mixed> = (blob: mixed) => ok(blob);
|
|
40
|
-
|
|
41
|
-
/**
|
|
42
|
-
* Alias of unknown.
|
|
43
|
-
*/
|
|
44
|
-
export const mixed: Decoder<mixed> = unknown;
|
package/core/constants.mjs
DELETED
|
@@ -1,46 +0,0 @@
|
|
|
1
|
-
import { annotate } from '../annotate.mjs';
|
|
2
|
-
import { err, ok } from '../result.mjs';
|
|
3
|
-
|
|
4
|
-
/**
|
|
5
|
-
* Decoder that only returns Ok for `null` inputs. Err otherwise.
|
|
6
|
-
*/
|
|
7
|
-
export var null_ = function null_(blob) {
|
|
8
|
-
return blob === null ? ok(blob) : err(annotate(blob, 'Must be null'));
|
|
9
|
-
};
|
|
10
|
-
/**
|
|
11
|
-
* Decoder that only returns Ok for `undefined` inputs. Err otherwise.
|
|
12
|
-
*/
|
|
13
|
-
|
|
14
|
-
export var undefined_ = function undefined_(blob) {
|
|
15
|
-
return blob === undefined ? ok(blob) : err(annotate(blob, 'Must be undefined'));
|
|
16
|
-
};
|
|
17
|
-
/**
|
|
18
|
-
* Decoder that only returns Ok for the given value constant. Err otherwise.
|
|
19
|
-
*/
|
|
20
|
-
|
|
21
|
-
export function constant(value) {
|
|
22
|
-
return function (blob) {
|
|
23
|
-
return blob === value ? ok(value) : err(annotate(blob, "Must be constant " + String(value)));
|
|
24
|
-
};
|
|
25
|
-
}
|
|
26
|
-
/**
|
|
27
|
-
* Decoder that always returns Ok for the given hardcoded value, no matter what the input.
|
|
28
|
-
*/
|
|
29
|
-
|
|
30
|
-
export function hardcoded(value) {
|
|
31
|
-
return function () {
|
|
32
|
-
return ok(value);
|
|
33
|
-
};
|
|
34
|
-
}
|
|
35
|
-
/**
|
|
36
|
-
* Decoder that always returns Ok for the given hardcoded value, no matter what the input.
|
|
37
|
-
*/
|
|
38
|
-
|
|
39
|
-
export var unknown = function unknown(blob) {
|
|
40
|
-
return ok(blob);
|
|
41
|
-
};
|
|
42
|
-
/**
|
|
43
|
-
* Alias of unknown.
|
|
44
|
-
*/
|
|
45
|
-
|
|
46
|
-
export var mixed = unknown;
|
package/core/date.d.ts
DELETED
package/core/date.js
DELETED
|
@@ -1,42 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
|
|
3
|
-
exports.__esModule = true;
|
|
4
|
-
exports.iso8601 = exports.date = void 0;
|
|
5
|
-
|
|
6
|
-
var _annotate = require("../annotate");
|
|
7
|
-
|
|
8
|
-
var _result = require("../result");
|
|
9
|
-
|
|
10
|
-
var _utils = require("../_utils");
|
|
11
|
-
|
|
12
|
-
var _string = require("./string");
|
|
13
|
-
|
|
14
|
-
var _composition = require("./composition");
|
|
15
|
-
|
|
16
|
-
// Only matches the shape. This "over-matches" some values that still aren't
|
|
17
|
-
// valid dates (like 9999-99-99), but those will be caught by JS Date's
|
|
18
|
-
// internal validations
|
|
19
|
-
var iso8601_re = /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(?:[.]\d+)?(?:Z|[+-]\d{2}:?\d{2})$/;
|
|
20
|
-
|
|
21
|
-
var date = function date(value) {
|
|
22
|
-
return (0, _utils.isDate)(value) ? (0, _result.ok)(value) : (0, _result.err)((0, _annotate.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
|
-
|
|
29
|
-
|
|
30
|
-
exports.date = date;
|
|
31
|
-
var iso8601 = (0, _composition.transform)( // Input itself needs to match the ISO8601 regex...
|
|
32
|
-
(0, _string.regex)(iso8601_re, 'Must be ISO8601 format'), // Make sure it is a _valid_ date
|
|
33
|
-
function (value) {
|
|
34
|
-
var date = new Date(value);
|
|
35
|
-
|
|
36
|
-
if (isNaN(date.getTime())) {
|
|
37
|
-
throw new Error('Must be valid date/time value');
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
return date;
|
|
41
|
-
});
|
|
42
|
-
exports.iso8601 = iso8601;
|
package/core/date.js.flow
DELETED
|
@@ -1,38 +0,0 @@
|
|
|
1
|
-
// @flow strict
|
|
2
|
-
|
|
3
|
-
import { annotate } from '../annotate';
|
|
4
|
-
import { err, ok } from '../result';
|
|
5
|
-
import { isDate } from '../_utils';
|
|
6
|
-
import { regex } from './string';
|
|
7
|
-
import { transform } from './composition';
|
|
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) ? ok(((value: cast): Date)) : err(annotate(value, 'Must be a Date'));
|
|
21
|
-
|
|
22
|
-
/**
|
|
23
|
-
* Decoder that only returns Ok for strings that are valid ISO8601 date
|
|
24
|
-
* strings. Err otherwise.
|
|
25
|
-
*/
|
|
26
|
-
export const iso8601: Decoder<Date> = transform(
|
|
27
|
-
// Input itself needs to match the ISO8601 regex...
|
|
28
|
-
regex(iso8601_re, 'Must be ISO8601 format'),
|
|
29
|
-
|
|
30
|
-
// Make sure it is a _valid_ date
|
|
31
|
-
(value: string) => {
|
|
32
|
-
const date = new Date(value);
|
|
33
|
-
if (isNaN(date.getTime())) {
|
|
34
|
-
throw new Error('Must be valid date/time value');
|
|
35
|
-
}
|
|
36
|
-
return date;
|
|
37
|
-
},
|
|
38
|
-
);
|
package/core/date.mjs
DELETED
|
@@ -1,28 +0,0 @@
|
|
|
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
|
-
// Only matches the shape. This "over-matches" some values that still aren't
|
|
7
|
-
// valid dates (like 9999-99-99), but those will be caught by JS Date's
|
|
8
|
-
// internal validations
|
|
9
|
-
var iso8601_re = /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(?:[.]\d+)?(?:Z|[+-]\d{2}:?\d{2})$/;
|
|
10
|
-
export var date = function date(value) {
|
|
11
|
-
return isDate(value) ? ok(value) : err(annotate(value, 'Must be a Date'));
|
|
12
|
-
};
|
|
13
|
-
/**
|
|
14
|
-
* Decoder that only returns Ok for strings that are valid ISO8601 date
|
|
15
|
-
* strings. Err otherwise.
|
|
16
|
-
*/
|
|
17
|
-
|
|
18
|
-
export var iso8601 = transform( // Input itself needs to match the ISO8601 regex...
|
|
19
|
-
regex(iso8601_re, 'Must be ISO8601 format'), // Make sure it is a _valid_ date
|
|
20
|
-
function (value) {
|
|
21
|
-
var date = new Date(value);
|
|
22
|
-
|
|
23
|
-
if (isNaN(date.getTime())) {
|
|
24
|
-
throw new Error('Must be valid date/time value');
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
return date;
|
|
28
|
-
});
|
package/core/describe.d.ts
DELETED
package/core/describe.js
DELETED
|
@@ -1,22 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
|
|
3
|
-
exports.__esModule = true;
|
|
4
|
-
exports.describe = describe;
|
|
5
|
-
|
|
6
|
-
var _annotate = require("../annotate");
|
|
7
|
-
|
|
8
|
-
var _result = require("../result");
|
|
9
|
-
|
|
10
|
-
/**
|
|
11
|
-
* Wrap another decoder, and override the error message in case it fails. This
|
|
12
|
-
* is useful to "simplify" otherwise potentially complex error messages, or to
|
|
13
|
-
* use language in those error messages that can be relayed to end users (for
|
|
14
|
-
* example to show in form errors).
|
|
15
|
-
*/
|
|
16
|
-
function describe(decoder, message) {
|
|
17
|
-
return function (blob) {
|
|
18
|
-
return (0, _result.mapError)(decoder(blob), function (err) {
|
|
19
|
-
return (0, _annotate.annotate)(err, message);
|
|
20
|
-
});
|
|
21
|
-
};
|
|
22
|
-
}
|
package/core/describe.js.flow
DELETED
|
@@ -1,17 +0,0 @@
|
|
|
1
|
-
// @flow strict
|
|
2
|
-
|
|
3
|
-
import { annotate } from '../annotate';
|
|
4
|
-
import { mapError } from '../result';
|
|
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 mapError(decoder(blob), (err) => annotate(err, message));
|
|
16
|
-
};
|
|
17
|
-
}
|
package/core/describe.mjs
DELETED
|
@@ -1,16 +0,0 @@
|
|
|
1
|
-
import { annotate } from '../annotate.mjs';
|
|
2
|
-
import { mapError } from '../result.mjs';
|
|
3
|
-
|
|
4
|
-
/**
|
|
5
|
-
* Wrap another decoder, and override the error message in case it fails. This
|
|
6
|
-
* is useful to "simplify" otherwise potentially complex error messages, or to
|
|
7
|
-
* use language in those error messages that can be relayed to end users (for
|
|
8
|
-
* example to show in form errors).
|
|
9
|
-
*/
|
|
10
|
-
export function describe(decoder, message) {
|
|
11
|
-
return function (blob) {
|
|
12
|
-
return mapError(decoder(blob), function (err) {
|
|
13
|
-
return annotate(err, message);
|
|
14
|
-
});
|
|
15
|
-
};
|
|
16
|
-
}
|
package/core/dispatch.d.ts
DELETED
|
@@ -1,8 +0,0 @@
|
|
|
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
DELETED
|
@@ -1,60 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
|
|
3
|
-
exports.__esModule = true;
|
|
4
|
-
exports.taggedUnion = taggedUnion;
|
|
5
|
-
|
|
6
|
-
var _result = require("../result");
|
|
7
|
-
|
|
8
|
-
var _object2 = require("./object");
|
|
9
|
-
|
|
10
|
-
var _either = require("./either");
|
|
11
|
-
|
|
12
|
-
var _composition = require("./composition");
|
|
13
|
-
|
|
14
|
-
/**
|
|
15
|
-
* Dispatches to one of several given decoders, based on the value found at
|
|
16
|
-
* runtime in the given field. For example, suppose you have these decoders:
|
|
17
|
-
*
|
|
18
|
-
* const rectangle = object({
|
|
19
|
-
* type: constant('rect'),
|
|
20
|
-
* x: number,
|
|
21
|
-
* y: number,
|
|
22
|
-
* width: number,
|
|
23
|
-
* height: number,
|
|
24
|
-
* });
|
|
25
|
-
*
|
|
26
|
-
* const circle = object({
|
|
27
|
-
* type: constant('circle'),
|
|
28
|
-
* cx: number,
|
|
29
|
-
* cy: number,
|
|
30
|
-
* r: number,
|
|
31
|
-
* });
|
|
32
|
-
*
|
|
33
|
-
* Then these two decoders are equivalent:
|
|
34
|
-
*
|
|
35
|
-
* const shape = either(rectangle, circle)
|
|
36
|
-
* const shape = taggedUnion('type', { rectangle, circle })
|
|
37
|
-
*
|
|
38
|
-
* Will be of type Decoder<Rectangle | Circle>.
|
|
39
|
-
*
|
|
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.
|
|
43
|
-
*
|
|
44
|
-
* The `either` version will simply try to invoke each decoder, until it finds
|
|
45
|
-
* one that matches.
|
|
46
|
-
*
|
|
47
|
-
* Also, the error messages will be less ambiguous using `taggedUnion()`.
|
|
48
|
-
*/
|
|
49
|
-
function taggedUnion(field, mapping) {
|
|
50
|
-
var _object;
|
|
51
|
-
|
|
52
|
-
var base = (0, _object2.object)((_object = {}, _object[field] = (0, _composition.prep)(String, (0, _either.oneOf)(Object.keys(mapping))), _object));
|
|
53
|
-
return function (blob) {
|
|
54
|
-
return (0, _result.andThen)(base(blob), function (baseObj) {
|
|
55
|
-
var decoderName = baseObj[field];
|
|
56
|
-
var decoder = mapping[decoderName];
|
|
57
|
-
return decoder(blob);
|
|
58
|
-
});
|
|
59
|
-
};
|
|
60
|
-
}
|