decoders 1.26.0-beta2 → 2.0.0-beta3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +21 -5
- package/_esm/_guard.js +15 -0
- package/_esm/_guard.js.flow +20 -0
- package/_esm/_types.js +0 -0
- package/_esm/_types.js.flow +20 -0
- package/_esm/_utils.js +93 -0
- package/_esm/_utils.js.flow +97 -0
- package/_esm/annotate.js +144 -0
- package/_esm/annotate.js.flow +218 -0
- package/_esm/core/array.js +91 -0
- package/{cjs → _esm/core}/array.js.flow +22 -25
- package/_esm/core/boolean.js +28 -0
- package/{cjs → _esm/core}/boolean.js.flow +8 -7
- package/_esm/core/composition.js +42 -0
- package/{cjs/utils.js.flow → _esm/core/composition.js.flow} +7 -22
- package/_esm/core/constants.js +46 -0
- package/{cjs → _esm/core}/constants.js.flow +13 -12
- package/_esm/core/date.js +28 -0
- package/{cjs → _esm/core}/date.js.flow +9 -7
- package/{cjs → _esm/core}/describe.js +5 -11
- package/{cjs → _esm/core}/describe.js.flow +4 -4
- package/{cjs → _esm/core}/dispatch.js +8 -15
- package/{cjs → _esm/core}/dispatch.js.flow +6 -5
- package/_esm/core/either.js +90 -0
- package/{cjs → _esm/core}/either.js.flow +55 -35
- package/_esm/core/fail.js +11 -0
- package/_esm/core/fail.js.flow +12 -0
- package/_esm/core/instanceOf.js +8 -0
- package/{cjs → _esm/core}/instanceOf.js.flow +7 -8
- package/_esm/core/json.js +15 -0
- package/{cjs → _esm/core}/json.js.flow +4 -4
- package/_esm/core/lazy.js +11 -0
- package/{cjs → _esm/core}/lazy.js.flow +1 -1
- package/_esm/core/mapping.js +54 -0
- package/_esm/core/mapping.js.flow +54 -0
- package/_esm/core/number.js +25 -0
- package/{cjs → _esm/core}/number.js.flow +10 -11
- package/_esm/core/object.js +175 -0
- package/{cjs → _esm/core}/object.js.flow +44 -52
- package/_esm/core/optional.js +38 -0
- package/{cjs → _esm/core}/optional.js.flow +6 -7
- package/{cjs → _esm/core}/string.js +18 -35
- package/{cjs → _esm/core}/string.js.flow +13 -12
- package/_esm/core/tuple.js +155 -0
- package/{cjs → _esm/core}/tuple.js.flow +62 -63
- package/_esm/format/index.js +2 -0
- package/_esm/format/index.js.flow +4 -0
- package/_esm/format/inline.js +137 -0
- package/_esm/format/inline.js.flow +122 -0
- package/_esm/format/short.js +4 -0
- package/_esm/format/short.js.flow +8 -0
- package/_esm/index.js +37 -0
- package/{cjs → _esm}/index.js.flow +31 -24
- package/_esm/result.js +139 -0
- package/_esm/result.js.flow +166 -0
- package/_guard.js +26 -0
- package/_guard.js.flow +20 -0
- package/_types.js +1 -0
- package/_types.js.flow +20 -0
- package/_utils.js +108 -0
- package/_utils.js.flow +97 -0
- package/annotate.js +161 -0
- package/annotate.js.flow +218 -0
- package/core/array.js +108 -0
- package/core/array.js.flow +103 -0
- package/core/boolean.js +44 -0
- package/core/boolean.js.flow +29 -0
- package/core/composition.js +56 -0
- package/core/composition.js.flow +43 -0
- package/core/constants.js +69 -0
- package/core/constants.js.flow +46 -0
- package/core/date.js +46 -0
- package/core/date.js.flow +40 -0
- package/core/describe.js +26 -0
- package/core/describe.js.flow +17 -0
- package/core/dispatch.js +62 -0
- package/core/dispatch.js.flow +58 -0
- package/core/either.js +117 -0
- package/core/either.js.flow +151 -0
- package/core/fail.js +21 -0
- package/core/fail.js.flow +12 -0
- package/core/instanceOf.js +19 -0
- package/core/instanceOf.js.flow +20 -0
- package/{cjs → core}/json.js +3 -5
- package/core/json.js.flow +28 -0
- package/{cjs → core}/lazy.js +1 -3
- package/core/lazy.js.flow +15 -0
- package/core/mapping.js +67 -0
- package/core/mapping.js.flow +54 -0
- package/core/number.js +40 -0
- package/core/number.js.flow +34 -0
- package/core/object.js +194 -0
- package/core/object.js.flow +203 -0
- package/core/optional.js +54 -0
- package/core/optional.js.flow +41 -0
- package/core/string.js +98 -0
- package/core/string.js.flow +82 -0
- package/core/tuple.js +173 -0
- package/core/tuple.js.flow +220 -0
- package/format/index.js +12 -0
- package/format/index.js.flow +4 -0
- package/format/inline.js +146 -0
- package/format/inline.js.flow +122 -0
- package/format/short.js +10 -0
- package/format/short.js.flow +8 -0
- package/index.js +120 -0
- package/index.js.flow +63 -0
- package/package.json +3 -11
- package/result.js +172 -0
- package/result.js.flow +166 -0
- package/cjs/array.js +0 -133
- package/cjs/boolean.js +0 -42
- package/cjs/constants.js +0 -67
- package/cjs/date.js +0 -42
- package/cjs/either.js +0 -85
- package/cjs/fail.js +0 -19
- package/cjs/fail.js.flow +0 -13
- package/cjs/guard.js +0 -30
- package/cjs/guard.js.flow +0 -36
- package/cjs/index.js +0 -397
- package/cjs/instanceOf.js +0 -17
- package/cjs/mapping.js +0 -113
- package/cjs/mapping.js.flow +0 -71
- package/cjs/number.js +0 -38
- package/cjs/object.js +0 -254
- package/cjs/optional.js +0 -52
- package/cjs/tuple.js +0 -199
- package/cjs/types.js +0 -3
- package/cjs/types.js.flow +0 -26
- package/cjs/utils.js +0 -70
- package/es/index.js +0 -1039
- package/ts/array.d.ts +0 -5
- package/ts/boolean.d.ts +0 -5
- package/ts/constants.d.ts +0 -11
- package/ts/date.d.ts +0 -4
- package/ts/describe.d.ts +0 -3
- package/ts/dispatch.d.ts +0 -8
- package/ts/either.d.ts +0 -61
- package/ts/fail.d.ts +0 -3
- package/ts/guard.d.ts +0 -7
- package/ts/helpers.d.ts +0 -79
- package/ts/index.d.ts +0 -38
- package/ts/instanceOf.d.ts +0 -3
- package/ts/json.d.ts +0 -11
- package/ts/lazy.d.ts +0 -3
- package/ts/mapping.d.ts +0 -4
- package/ts/number.d.ts +0 -6
- package/ts/object.d.ts +0 -33
- package/ts/optional.d.ts +0 -5
- package/ts/string.d.ts +0 -7
- package/ts/tuple.d.ts +0 -30
- package/ts/types.d.ts +0 -18
- package/ts/utils.d.ts +0 -13
package/cjs/boolean.js
DELETED
|
@@ -1,42 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
|
|
3
|
-
Object.defineProperty(exports, "__esModule", {
|
|
4
|
-
value: true
|
|
5
|
-
});
|
|
6
|
-
exports.truthy = exports.numericBoolean = exports["boolean"] = void 0;
|
|
7
|
-
|
|
8
|
-
var _debrief = require("debrief");
|
|
9
|
-
|
|
10
|
-
var _lemons = require("lemons");
|
|
11
|
-
|
|
12
|
-
var _number = require("./number");
|
|
13
|
-
|
|
14
|
-
var _utils = require("./utils");
|
|
15
|
-
|
|
16
|
-
/**
|
|
17
|
-
* Decoder that only returns Ok for boolean inputs. Err otherwise.
|
|
18
|
-
*/
|
|
19
|
-
var _boolean = function _boolean(blob) {
|
|
20
|
-
return typeof blob === 'boolean' ? (0, _lemons.Ok)(blob) : (0, _lemons.Err)((0, _debrief.annotate)(blob, 'Must be boolean'));
|
|
21
|
-
};
|
|
22
|
-
/**
|
|
23
|
-
* Decoder that returns true for all truthy values, and false otherwise. Never fails.
|
|
24
|
-
*/
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
exports["boolean"] = _boolean;
|
|
28
|
-
|
|
29
|
-
var truthy = function truthy(blob) {
|
|
30
|
-
return (0, _lemons.Ok)(!!blob);
|
|
31
|
-
};
|
|
32
|
-
/**
|
|
33
|
-
* Decoder that only returns Ok for numeric input values representing booleans.
|
|
34
|
-
* Returns their boolean representation. Err otherwise.
|
|
35
|
-
*/
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
exports.truthy = truthy;
|
|
39
|
-
var numericBoolean = (0, _utils.map)(_number.number, function (n) {
|
|
40
|
-
return !!n;
|
|
41
|
-
});
|
|
42
|
-
exports.numericBoolean = numericBoolean;
|
package/cjs/constants.js
DELETED
|
@@ -1,67 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
|
|
3
|
-
Object.defineProperty(exports, "__esModule", {
|
|
4
|
-
value: true
|
|
5
|
-
});
|
|
6
|
-
exports.constant = constant;
|
|
7
|
-
exports.hardcoded = hardcoded;
|
|
8
|
-
exports.unknown = exports.undefined_ = exports.null_ = exports.mixed = void 0;
|
|
9
|
-
|
|
10
|
-
var _debrief = require("debrief");
|
|
11
|
-
|
|
12
|
-
var _lemons = require("lemons");
|
|
13
|
-
|
|
14
|
-
/**
|
|
15
|
-
* Decoder that only returns Ok for `null` inputs. Err otherwise.
|
|
16
|
-
*/
|
|
17
|
-
var null_ = function null_(blob) {
|
|
18
|
-
return blob === null ? (0, _lemons.Ok)(blob) : (0, _lemons.Err)((0, _debrief.annotate)(blob, 'Must be null'));
|
|
19
|
-
};
|
|
20
|
-
/**
|
|
21
|
-
* Decoder that only returns Ok for `undefined` inputs. Err otherwise.
|
|
22
|
-
*/
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
exports.null_ = null_;
|
|
26
|
-
|
|
27
|
-
var undefined_ = function undefined_(blob) {
|
|
28
|
-
return blob === undefined ? (0, _lemons.Ok)(blob) : (0, _lemons.Err)((0, _debrief.annotate)(blob, 'Must be undefined'));
|
|
29
|
-
};
|
|
30
|
-
/**
|
|
31
|
-
* Decoder that only returns Ok for the given value constant. Err otherwise.
|
|
32
|
-
*/
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
exports.undefined_ = undefined_;
|
|
36
|
-
|
|
37
|
-
function constant(value) {
|
|
38
|
-
return function (blob) {
|
|
39
|
-
return blob === value ? (0, _lemons.Ok)(value) : (0, _lemons.Err)((0, _debrief.annotate)(blob, "Must be constant ".concat(String(value))));
|
|
40
|
-
};
|
|
41
|
-
}
|
|
42
|
-
/**
|
|
43
|
-
* Decoder that always returns Ok for the given hardcoded value, no matter what the input.
|
|
44
|
-
*/
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
function hardcoded(value) {
|
|
48
|
-
return function (_) {
|
|
49
|
-
return (0, _lemons.Ok)(value);
|
|
50
|
-
};
|
|
51
|
-
}
|
|
52
|
-
/**
|
|
53
|
-
* Decoder that always returns Ok for the given hardcoded value, no matter what the input.
|
|
54
|
-
*/
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
var mixed = function mixed(blob) {
|
|
58
|
-
return (0, _lemons.Ok)(blob);
|
|
59
|
-
};
|
|
60
|
-
/**
|
|
61
|
-
* Alias of mixed.
|
|
62
|
-
*/
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
exports.mixed = mixed;
|
|
66
|
-
var unknown = mixed;
|
|
67
|
-
exports.unknown = unknown;
|
package/cjs/date.js
DELETED
|
@@ -1,42 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
|
|
3
|
-
Object.defineProperty(exports, "__esModule", {
|
|
4
|
-
value: true
|
|
5
|
-
});
|
|
6
|
-
exports.iso8601 = exports.date = void 0;
|
|
7
|
-
|
|
8
|
-
var _debrief = require("debrief");
|
|
9
|
-
|
|
10
|
-
var _lemons = require("lemons");
|
|
11
|
-
|
|
12
|
-
var _string = require("./string");
|
|
13
|
-
|
|
14
|
-
var _utils = require("./utils");
|
|
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, _lemons.Ok)(value) : (0, _lemons.Err)((0, _debrief.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, _utils.map)( // 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/cjs/either.js
DELETED
|
@@ -1,85 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
|
|
3
|
-
Object.defineProperty(exports, "__esModule", {
|
|
4
|
-
value: true
|
|
5
|
-
});
|
|
6
|
-
exports.either = either;
|
|
7
|
-
exports.either3 = either3;
|
|
8
|
-
exports.either4 = either4;
|
|
9
|
-
exports.either5 = either5;
|
|
10
|
-
exports.either6 = either6;
|
|
11
|
-
exports.either7 = either7;
|
|
12
|
-
exports.either8 = either8;
|
|
13
|
-
exports.either9 = either9;
|
|
14
|
-
exports.oneOf = oneOf;
|
|
15
|
-
|
|
16
|
-
var _debrief = require("debrief");
|
|
17
|
-
|
|
18
|
-
var _lemons = require("lemons");
|
|
19
|
-
|
|
20
|
-
/**
|
|
21
|
-
* Indents and adds a dash in front of this (potentially multiline) string.
|
|
22
|
-
*/
|
|
23
|
-
// istanbul ignore next
|
|
24
|
-
function itemize() {
|
|
25
|
-
var s = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : '';
|
|
26
|
-
return '-' + (0, _debrief.indent)(s).substring(1);
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
function either(d1, d2) {
|
|
30
|
-
return function (blob) {
|
|
31
|
-
return d1(blob).dispatch(function (value1) {
|
|
32
|
-
return (0, _lemons.Ok)(value1);
|
|
33
|
-
}, function (err1) {
|
|
34
|
-
return d2(blob).dispatch(function (value2) {
|
|
35
|
-
return (0, _lemons.Ok)(value2);
|
|
36
|
-
}, function (err2) {
|
|
37
|
-
return (0, _lemons.Err)((0, _debrief.annotate)(blob, ['Either:', itemize((0, _debrief.summarize)(err1).join('\n')), itemize((0, _debrief.summarize)(err2).join('\n'))].join('\n')));
|
|
38
|
-
});
|
|
39
|
-
});
|
|
40
|
-
};
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
function either3(d1, d2, d3) {
|
|
44
|
-
return either(d1, either(d2, d3));
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
function either4(d1, d2, d3, d4) {
|
|
48
|
-
return either(d1, either3(d2, d3, d4));
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
function either5(d1, d2, d3, d4, d5) {
|
|
52
|
-
return either(d1, either4(d2, d3, d4, d5));
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
function either6(d1, d2, d3, d4, d5, d6) {
|
|
56
|
-
return either(d1, either5(d2, d3, d4, d5, d6));
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
function either7(d1, d2, d3, d4, d5, d6, d7) {
|
|
60
|
-
return either(d1, either6(d2, d3, d4, d5, d6, d7));
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
function either8(d1, d2, d3, d4, d5, d6, d7, d8) {
|
|
64
|
-
return either(d1, either7(d2, d3, d4, d5, d6, d7, d8));
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
function either9(d1, d2, d3, d4, d5, d6, d7, d8, d9) {
|
|
68
|
-
return either(d1, either8(d2, d3, d4, d5, d6, d7, d8, d9));
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
function oneOf(constants) {
|
|
72
|
-
return function (blob) {
|
|
73
|
-
var winner = constants.find(function (c) {
|
|
74
|
-
return c === blob;
|
|
75
|
-
});
|
|
76
|
-
|
|
77
|
-
if (winner !== undefined) {
|
|
78
|
-
return (0, _lemons.Ok)(winner);
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
return (0, _lemons.Err)((0, _debrief.annotate)(blob, "Must be one of ".concat(constants.map(function (value) {
|
|
82
|
-
return JSON.stringify(value);
|
|
83
|
-
}).join(', '))));
|
|
84
|
-
};
|
|
85
|
-
}
|
package/cjs/fail.js
DELETED
|
@@ -1,19 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
|
|
3
|
-
Object.defineProperty(exports, "__esModule", {
|
|
4
|
-
value: true
|
|
5
|
-
});
|
|
6
|
-
exports.fail = fail;
|
|
7
|
-
|
|
8
|
-
var _debrief = require("debrief");
|
|
9
|
-
|
|
10
|
-
var _lemons = require("lemons");
|
|
11
|
-
|
|
12
|
-
/**
|
|
13
|
-
* Decoder that always fails with the given error message, no matter what the input.
|
|
14
|
-
*/
|
|
15
|
-
function fail(msg) {
|
|
16
|
-
return function (blob) {
|
|
17
|
-
return (0, _lemons.Err)((0, _debrief.annotate)(blob, msg));
|
|
18
|
-
};
|
|
19
|
-
}
|
package/cjs/fail.js.flow
DELETED
|
@@ -1,13 +0,0 @@
|
|
|
1
|
-
// @flow strict
|
|
2
|
-
|
|
3
|
-
import { annotate } from 'debrief';
|
|
4
|
-
import { Err } from 'lemons';
|
|
5
|
-
|
|
6
|
-
import type { Decoder } from './types';
|
|
7
|
-
|
|
8
|
-
/**
|
|
9
|
-
* Decoder that always fails with the given error message, no matter what the input.
|
|
10
|
-
*/
|
|
11
|
-
export function fail(msg: string): Decoder<empty> {
|
|
12
|
-
return (blob: mixed) => Err(annotate(blob, msg));
|
|
13
|
-
}
|
package/cjs/guard.js
DELETED
|
@@ -1,30 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
|
|
3
|
-
Object.defineProperty(exports, "__esModule", {
|
|
4
|
-
value: true
|
|
5
|
-
});
|
|
6
|
-
exports.guard = guard;
|
|
7
|
-
|
|
8
|
-
var _debrief = require("debrief");
|
|
9
|
-
|
|
10
|
-
/**
|
|
11
|
-
* Alternative for serialize() that does not echo back the input value.
|
|
12
|
-
*/
|
|
13
|
-
function serializeSimple(annotation) {
|
|
14
|
-
return (0, _debrief.summarize)(annotation).join('\n');
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
function guard(decoder, options) {
|
|
18
|
-
var o = options || {};
|
|
19
|
-
var style = o.style || 'inline';
|
|
20
|
-
var serializer = style === 'inline' ? _debrief.serialize // Normal serializer, which echoes back inputted value and inlines errors
|
|
21
|
-
: serializeSimple; // Only returns error messages, without echoing back input
|
|
22
|
-
|
|
23
|
-
return function (blob) {
|
|
24
|
-
return decoder(blob).mapError(function (annotation) {
|
|
25
|
-
var err = new Error('\n' + serializer(annotation));
|
|
26
|
-
err.name = 'Decoding error';
|
|
27
|
-
return err;
|
|
28
|
-
}).unwrap();
|
|
29
|
-
};
|
|
30
|
-
}
|
package/cjs/guard.js.flow
DELETED
|
@@ -1,36 +0,0 @@
|
|
|
1
|
-
// @flow strict
|
|
2
|
-
|
|
3
|
-
import { serialize as serializeInline, summarize } from 'debrief';
|
|
4
|
-
import type { Annotation } from 'debrief';
|
|
5
|
-
|
|
6
|
-
import type { Decoder, Guard } from './types';
|
|
7
|
-
|
|
8
|
-
type Options = {|
|
|
9
|
-
style?: 'inline' | 'simple', // `inline` by default
|
|
10
|
-
|};
|
|
11
|
-
|
|
12
|
-
/**
|
|
13
|
-
* Alternative for serialize() that does not echo back the input value.
|
|
14
|
-
*/
|
|
15
|
-
function serializeSimple(annotation: Annotation) {
|
|
16
|
-
return summarize(annotation).join('\n');
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
export function guard<T>(decoder: Decoder<T>, options?: Options): Guard<T> {
|
|
20
|
-
const o = options || {};
|
|
21
|
-
const style = o.style || 'inline';
|
|
22
|
-
|
|
23
|
-
const serializer =
|
|
24
|
-
style === 'inline'
|
|
25
|
-
? serializeInline // Normal serializer, which echoes back inputted value and inlines errors
|
|
26
|
-
: serializeSimple; // Only returns error messages, without echoing back input
|
|
27
|
-
|
|
28
|
-
return (blob: mixed) =>
|
|
29
|
-
decoder(blob)
|
|
30
|
-
.mapError((annotation) => {
|
|
31
|
-
const err = new Error('\n' + serializer(annotation));
|
|
32
|
-
err.name = 'Decoding error';
|
|
33
|
-
return err;
|
|
34
|
-
})
|
|
35
|
-
.unwrap();
|
|
36
|
-
}
|