decoders 1.25.4 → 2.0.0-beta1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +30 -3
- package/cjs/_guard.js +26 -0
- package/cjs/_guard.js.flow +20 -0
- package/cjs/_types.js +1 -0
- package/cjs/_types.js.flow +20 -0
- package/cjs/_utils.js +108 -0
- package/cjs/_utils.js.flow +97 -0
- package/cjs/annotate.js +161 -0
- package/cjs/annotate.js.flow +218 -0
- package/cjs/format/index.js +12 -0
- package/cjs/format/index.js.flow +4 -0
- package/cjs/format/inline.js +146 -0
- package/cjs/format/inline.js.flow +122 -0
- package/cjs/format/short.js +10 -0
- package/cjs/format/short.js.flow +8 -0
- package/cjs/index.js +120 -0
- package/{index.js.flow → cjs/index.js.flow} +31 -24
- package/cjs/result.js +172 -0
- package/cjs/result.js.flow +166 -0
- package/cjs/stdlib/array.js +108 -0
- package/{array.js.flow → cjs/stdlib/array.js.flow} +22 -25
- package/cjs/stdlib/boolean.js +44 -0
- package/{boolean.js.flow → cjs/stdlib/boolean.js.flow} +8 -7
- package/cjs/stdlib/composition.js +56 -0
- package/{utils.js.flow → cjs/stdlib/composition.js.flow} +7 -22
- package/cjs/stdlib/constants.js +69 -0
- package/{constants.js.flow → cjs/stdlib/constants.js.flow} +13 -12
- package/cjs/stdlib/date.js +46 -0
- package/{date.js.flow → cjs/stdlib/date.js.flow} +9 -7
- package/cjs/stdlib/describe.js +26 -0
- package/{describe.js.flow → cjs/stdlib/describe.js.flow} +4 -4
- package/cjs/stdlib/dispatch.js +62 -0
- package/{dispatch.js.flow → cjs/stdlib/dispatch.js.flow} +6 -5
- package/cjs/stdlib/either.js +117 -0
- package/{either.js.flow → cjs/stdlib/either.js.flow} +55 -35
- package/cjs/stdlib/fail.js +21 -0
- package/cjs/stdlib/fail.js.flow +12 -0
- package/cjs/stdlib/instanceOf.js +19 -0
- package/{instanceOf.js.flow → cjs/stdlib/instanceOf.js.flow} +7 -8
- package/{json.js → cjs/stdlib/json.js} +3 -5
- package/{json.js.flow → cjs/stdlib/json.js.flow} +4 -4
- package/{lazy.js → cjs/stdlib/lazy.js} +1 -3
- package/{lazy.js.flow → cjs/stdlib/lazy.js.flow} +1 -1
- package/cjs/stdlib/mapping.js +67 -0
- package/cjs/stdlib/mapping.js.flow +54 -0
- package/cjs/stdlib/number.js +40 -0
- package/{number.js.flow → cjs/stdlib/number.js.flow} +10 -11
- package/cjs/stdlib/object.js +194 -0
- package/{object.js.flow → cjs/stdlib/object.js.flow} +44 -52
- package/cjs/stdlib/optional.js +54 -0
- package/{optional.js.flow → cjs/stdlib/optional.js.flow} +6 -7
- package/cjs/stdlib/string.js +98 -0
- package/{string.js.flow → cjs/stdlib/string.js.flow} +13 -12
- package/cjs/stdlib/tuple.js +173 -0
- package/{tuple.js.flow → cjs/stdlib/tuple.js.flow} +62 -63
- package/es/_guard.js +15 -0
- package/es/_types.js +0 -0
- package/es/_utils.js +93 -0
- package/es/annotate.js +144 -0
- package/es/format/index.js +2 -0
- package/es/format/inline.js +137 -0
- package/es/format/short.js +4 -0
- package/es/index.js +37 -0
- package/es/result.js +139 -0
- package/es/stdlib/array.js +91 -0
- package/es/stdlib/boolean.js +28 -0
- package/es/stdlib/composition.js +42 -0
- package/es/stdlib/constants.js +46 -0
- package/es/stdlib/date.js +28 -0
- package/{describe.js → es/stdlib/describe.js} +5 -11
- package/{dispatch.js → es/stdlib/dispatch.js} +8 -15
- package/es/stdlib/either.js +90 -0
- package/es/stdlib/fail.js +11 -0
- package/es/stdlib/instanceOf.js +8 -0
- package/es/stdlib/json.js +15 -0
- package/es/stdlib/lazy.js +11 -0
- package/es/stdlib/mapping.js +54 -0
- package/es/stdlib/number.js +25 -0
- package/es/stdlib/object.js +175 -0
- package/es/stdlib/optional.js +38 -0
- package/{string.js → es/stdlib/string.js} +18 -35
- package/es/stdlib/tuple.js +155 -0
- package/package.json +12 -7
- package/ts/_guard.d.ts +7 -0
- package/ts/_helpers.d.ts +79 -0
- package/{types.d.ts → ts/_types.d.ts} +3 -5
- package/{utils.d.ts → ts/_utils.d.ts} +3 -3
- package/ts/annotate.d.ts +58 -0
- package/{array.d.ts → ts/array.d.ts} +1 -1
- package/{boolean.d.ts → ts/boolean.d.ts} +1 -1
- package/{constants.d.ts → ts/constants.d.ts} +1 -1
- package/{date.d.ts → ts/date.d.ts} +1 -1
- package/{describe.d.ts → ts/describe.d.ts} +1 -1
- package/{dispatch.d.ts → ts/dispatch.d.ts} +3 -3
- package/{either.d.ts → ts/either.d.ts} +8 -8
- package/{fail.d.ts → ts/fail.d.ts} +1 -1
- package/ts/index.d.ts +42 -0
- package/ts/inline.d.ts +3 -0
- package/{instanceOf.d.ts → ts/instanceOf.d.ts} +1 -1
- package/{json.d.ts → ts/json.d.ts} +1 -1
- package/{lazy.d.ts → ts/lazy.d.ts} +1 -1
- package/{mapping.d.ts → ts/mapping.d.ts} +1 -1
- package/{number.d.ts → ts/number.d.ts} +1 -1
- package/{object.d.ts → ts/object.d.ts} +8 -8
- package/{optional.d.ts → ts/optional.d.ts} +1 -1
- package/ts/result.d.ts +39 -0
- package/ts/short.d.ts +3 -0
- package/{string.d.ts → ts/string.d.ts} +1 -1
- package/{tuple.d.ts → ts/tuple.d.ts} +5 -5
- package/array.js +0 -133
- package/boolean.js +0 -42
- package/constants.js +0 -67
- package/date.js +0 -42
- package/either.js +0 -85
- package/fail.js +0 -19
- package/fail.js.flow +0 -13
- package/guard.d.ts +0 -7
- package/guard.js +0 -30
- package/guard.js.flow +0 -36
- package/helpers.d.ts +0 -62
- package/index.d.ts +0 -38
- package/index.js +0 -397
- package/instanceOf.js +0 -17
- package/mapping.js +0 -113
- package/mapping.js.flow +0 -71
- package/number.js +0 -38
- package/object.js +0 -254
- package/optional.js +0 -52
- package/tuple.js +0 -199
- package/types.js +0 -5
- package/types.js.flow +0 -26
- package/utils.js +0 -70
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
import { Decoder,
|
|
1
|
+
import { Decoder, DecoderType } from '../_types';
|
|
2
2
|
|
|
3
3
|
export type $Values<T extends object> = T[keyof T];
|
|
4
4
|
|
|
5
5
|
export function dispatch<O extends { [key: string]: Decoder<any> }>(
|
|
6
6
|
field: string,
|
|
7
|
-
mapping: O
|
|
8
|
-
): Decoder<$Values<{ [key in keyof O]:
|
|
7
|
+
mapping: O,
|
|
8
|
+
): Decoder<$Values<{ [key in keyof O]: DecoderType<O[key]> }>>;
|
|
@@ -1,24 +1,24 @@
|
|
|
1
|
-
import { Decoder, Scalar } from '
|
|
1
|
+
import { Decoder, Scalar } from '../_types';
|
|
2
2
|
|
|
3
3
|
export function either<T1, T2>(d1: Decoder<T1>, d2: Decoder<T2>): Decoder<T1 | T2>;
|
|
4
4
|
export function either2<T1, T2>(d1: Decoder<T1>, d2: Decoder<T2>): Decoder<T1 | T2>;
|
|
5
5
|
export function either3<T1, T2, T3>(
|
|
6
6
|
d1: Decoder<T1>,
|
|
7
7
|
d2: Decoder<T2>,
|
|
8
|
-
d3: Decoder<T3
|
|
8
|
+
d3: Decoder<T3>,
|
|
9
9
|
): Decoder<T1 | T2 | T3>;
|
|
10
10
|
export function either4<T1, T2, T3, T4>(
|
|
11
11
|
d1: Decoder<T1>,
|
|
12
12
|
d2: Decoder<T2>,
|
|
13
13
|
d3: Decoder<T3>,
|
|
14
|
-
d4: Decoder<T4
|
|
14
|
+
d4: Decoder<T4>,
|
|
15
15
|
): Decoder<T1 | T2 | T3 | T4>;
|
|
16
16
|
export function either5<T1, T2, T3, T4, T5>(
|
|
17
17
|
d1: Decoder<T1>,
|
|
18
18
|
d2: Decoder<T2>,
|
|
19
19
|
d3: Decoder<T3>,
|
|
20
20
|
d4: Decoder<T4>,
|
|
21
|
-
d5: Decoder<T5
|
|
21
|
+
d5: Decoder<T5>,
|
|
22
22
|
): Decoder<T1 | T2 | T3 | T4 | T5>;
|
|
23
23
|
export function either6<T1, T2, T3, T4, T5, T6>(
|
|
24
24
|
d1: Decoder<T1>,
|
|
@@ -26,7 +26,7 @@ export function either6<T1, T2, T3, T4, T5, T6>(
|
|
|
26
26
|
d3: Decoder<T3>,
|
|
27
27
|
d4: Decoder<T4>,
|
|
28
28
|
d5: Decoder<T5>,
|
|
29
|
-
d6: Decoder<T6
|
|
29
|
+
d6: Decoder<T6>,
|
|
30
30
|
): Decoder<T1 | T2 | T3 | T4 | T5 | T6>;
|
|
31
31
|
export function either7<T1, T2, T3, T4, T5, T6, T7>(
|
|
32
32
|
d1: Decoder<T1>,
|
|
@@ -35,7 +35,7 @@ export function either7<T1, T2, T3, T4, T5, T6, T7>(
|
|
|
35
35
|
d4: Decoder<T4>,
|
|
36
36
|
d5: Decoder<T5>,
|
|
37
37
|
d6: Decoder<T6>,
|
|
38
|
-
d7: Decoder<T7
|
|
38
|
+
d7: Decoder<T7>,
|
|
39
39
|
): Decoder<T1 | T2 | T3 | T4 | T5 | T6 | T7>;
|
|
40
40
|
export function either8<T1, T2, T3, T4, T5, T6, T7, T8>(
|
|
41
41
|
d1: Decoder<T1>,
|
|
@@ -45,7 +45,7 @@ export function either8<T1, T2, T3, T4, T5, T6, T7, T8>(
|
|
|
45
45
|
d5: Decoder<T5>,
|
|
46
46
|
d6: Decoder<T6>,
|
|
47
47
|
d7: Decoder<T7>,
|
|
48
|
-
d8: Decoder<T8
|
|
48
|
+
d8: Decoder<T8>,
|
|
49
49
|
): Decoder<T1 | T2 | T3 | T4 | T5 | T6 | T7 | T8>;
|
|
50
50
|
export function either9<T1, T2, T3, T4, T5, T6, T7, T8, T9>(
|
|
51
51
|
d1: Decoder<T1>,
|
|
@@ -56,6 +56,6 @@ export function either9<T1, T2, T3, T4, T5, T6, T7, T8, T9>(
|
|
|
56
56
|
d6: Decoder<T6>,
|
|
57
57
|
d7: Decoder<T7>,
|
|
58
58
|
d8: Decoder<T8>,
|
|
59
|
-
d9: Decoder<T9
|
|
59
|
+
d9: Decoder<T9>,
|
|
60
60
|
): Decoder<T1 | T2 | T3 | T4 | T5 | T6 | T7 | T8 | T9>;
|
|
61
61
|
export function oneOf<T extends Scalar>(constants: readonly T[]): Decoder<T>;
|
package/ts/index.d.ts
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
export { Decoder, Guard } from './_types';
|
|
2
|
+
export { DecoderType, GuardType } from './_types';
|
|
3
|
+
|
|
4
|
+
export { guard } from './_guard';
|
|
5
|
+
export { compose, map, predicate } from './_utils';
|
|
6
|
+
|
|
7
|
+
export { JSONArray, JSONObject, JSONValue } from './stdlib/json';
|
|
8
|
+
|
|
9
|
+
export { array, nonEmptyArray, poja } from './stdlib/array';
|
|
10
|
+
export { boolean, numericBoolean, truthy } from './stdlib/boolean';
|
|
11
|
+
export {
|
|
12
|
+
constant,
|
|
13
|
+
hardcoded,
|
|
14
|
+
mixed,
|
|
15
|
+
null_,
|
|
16
|
+
undefined_,
|
|
17
|
+
unknown,
|
|
18
|
+
} from './stdlib/constants';
|
|
19
|
+
export { date, iso8601 } from './stdlib/date';
|
|
20
|
+
export { describe } from './stdlib/describe';
|
|
21
|
+
export { dispatch } from './stdlib/dispatch';
|
|
22
|
+
export {
|
|
23
|
+
either,
|
|
24
|
+
either3,
|
|
25
|
+
either4,
|
|
26
|
+
either5,
|
|
27
|
+
either6,
|
|
28
|
+
either7,
|
|
29
|
+
either8,
|
|
30
|
+
either9,
|
|
31
|
+
oneOf,
|
|
32
|
+
} from './stdlib/either';
|
|
33
|
+
export { fail } from './stdlib/fail';
|
|
34
|
+
export { instanceOf } from './stdlib/instanceOf';
|
|
35
|
+
export { json, jsonArray, jsonObject } from './stdlib/json';
|
|
36
|
+
export { lazy } from './stdlib/lazy';
|
|
37
|
+
export { mapping, dict } from './stdlib/mapping';
|
|
38
|
+
export { integer, number, positiveInteger, positiveNumber } from './stdlib/number';
|
|
39
|
+
export { exact, inexact, object, pojo } from './stdlib/object';
|
|
40
|
+
export { maybe, nullable, optional } from './stdlib/optional';
|
|
41
|
+
export { email, nonEmptyString, regex, string, url } from './stdlib/string';
|
|
42
|
+
export { tuple1, tuple2, tuple3, tuple4, tuple5, tuple6 } from './stdlib/tuple';
|
package/ts/inline.d.ts
ADDED
|
@@ -1,14 +1,14 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { AllowImplicit } from './
|
|
1
|
+
import { Decoder, DecoderType } from '../_types';
|
|
2
|
+
import { AllowImplicit } from './_helpers';
|
|
3
3
|
|
|
4
|
-
export type ObjectDecoderType<T> = AllowImplicit<
|
|
5
|
-
|
|
6
|
-
>;
|
|
4
|
+
export type ObjectDecoderType<T> = AllowImplicit<{
|
|
5
|
+
[key in keyof T]: DecoderType<T[key]>;
|
|
6
|
+
}>;
|
|
7
7
|
|
|
8
8
|
export const pojo: Decoder<{ [key: string]: unknown }>;
|
|
9
9
|
|
|
10
10
|
export function object<O extends { [key: string]: Decoder<any> }>(
|
|
11
|
-
mapping: O
|
|
11
|
+
mapping: O,
|
|
12
12
|
): Decoder<{ [K in keyof ObjectDecoderType<O>]: ObjectDecoderType<O>[K] }>;
|
|
13
13
|
// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
|
14
14
|
// This is basically just equivalent to:
|
|
@@ -19,13 +19,13 @@ export function object<O extends { [key: string]: Decoder<any> }>(
|
|
|
19
19
|
// work with while developing.
|
|
20
20
|
|
|
21
21
|
export function exact<O extends { [key: string]: Decoder<any> }>(
|
|
22
|
-
mapping: O
|
|
22
|
+
mapping: O,
|
|
23
23
|
): Decoder<{ [K in keyof ObjectDecoderType<O>]: ObjectDecoderType<O>[K] }>;
|
|
24
24
|
// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
|
25
25
|
// Ditto (see above)
|
|
26
26
|
|
|
27
27
|
export function inexact<O extends { [key: string]: Decoder<any> }>(
|
|
28
|
-
mapping: O
|
|
28
|
+
mapping: O,
|
|
29
29
|
): Decoder<
|
|
30
30
|
{ [K in keyof ObjectDecoderType<O>]: ObjectDecoderType<O>[K] } & {
|
|
31
31
|
[extra: string]: unknown;
|
package/ts/result.d.ts
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
export interface Ok<T> {
|
|
2
|
+
readonly type: 'ok';
|
|
3
|
+
readonly value: T;
|
|
4
|
+
}
|
|
5
|
+
|
|
6
|
+
export interface Err<E> {
|
|
7
|
+
readonly type: 'err';
|
|
8
|
+
readonly error: E;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export type Result<T, E> = Ok<T> | Err<E>;
|
|
12
|
+
|
|
13
|
+
export function ok<T>(value: T): Ok<T>;
|
|
14
|
+
export function err<E>(error: E): Err<E>;
|
|
15
|
+
export function toString(result: Result<unknown, unknown>): string;
|
|
16
|
+
export function isOk(result: Result<unknown, unknown>): boolean;
|
|
17
|
+
export function isErr(result: Result<unknown, unknown>): boolean;
|
|
18
|
+
export function withDefault<T>(result: Result<T, unknown>, defaultValue: T): T;
|
|
19
|
+
export function value<T>(result: Result<T, unknown>): void | T;
|
|
20
|
+
export function errValue<E>(result: Result<unknown, E>): void | E;
|
|
21
|
+
export function unwrap<T>(result: Result<T, unknown>): T;
|
|
22
|
+
export function expect<T>(result: Result<T, unknown>, message: string | Error): T;
|
|
23
|
+
export function dispatch<T, E, O>(
|
|
24
|
+
result: Result<T, E>,
|
|
25
|
+
okCallback: (value: T) => O,
|
|
26
|
+
errCallback: (error: E) => O,
|
|
27
|
+
): O;
|
|
28
|
+
export function andThen<T, E, V>(
|
|
29
|
+
result: Result<T, E>,
|
|
30
|
+
callback: (value: T) => Result<V, E>,
|
|
31
|
+
): Result<V, E>;
|
|
32
|
+
export function map<T, E, T2>(
|
|
33
|
+
result: Result<T, E>,
|
|
34
|
+
mapper: (value: T) => T2,
|
|
35
|
+
): Result<T2, E>;
|
|
36
|
+
export function mapError<T, E, E2>(
|
|
37
|
+
result: Result<T, E>,
|
|
38
|
+
mapper: (error: E) => E2,
|
|
39
|
+
): Result<T, E2>;
|
package/ts/short.d.ts
ADDED
|
@@ -1,24 +1,24 @@
|
|
|
1
|
-
import { Decoder } from '
|
|
1
|
+
import { Decoder } from '../_types';
|
|
2
2
|
|
|
3
3
|
export function tuple1<T1>(d1: Decoder<T1>): Decoder<[T1]>;
|
|
4
4
|
export function tuple2<T1, T2>(d1: Decoder<T1>, d2: Decoder<T2>): Decoder<[T1, T2]>;
|
|
5
5
|
export function tuple3<T1, T2, T3>(
|
|
6
6
|
d1: Decoder<T1>,
|
|
7
7
|
d2: Decoder<T2>,
|
|
8
|
-
d3: Decoder<T3
|
|
8
|
+
d3: Decoder<T3>,
|
|
9
9
|
): Decoder<[T1, T2, T3]>;
|
|
10
10
|
export function tuple4<T1, T2, T3, T4>(
|
|
11
11
|
d1: Decoder<T1>,
|
|
12
12
|
d2: Decoder<T2>,
|
|
13
13
|
d3: Decoder<T3>,
|
|
14
|
-
d4: Decoder<T4
|
|
14
|
+
d4: Decoder<T4>,
|
|
15
15
|
): Decoder<[T1, T2, T3, T4]>;
|
|
16
16
|
export function tuple5<T1, T2, T3, T4, T5>(
|
|
17
17
|
d1: Decoder<T1>,
|
|
18
18
|
d2: Decoder<T2>,
|
|
19
19
|
d3: Decoder<T3>,
|
|
20
20
|
d4: Decoder<T4>,
|
|
21
|
-
d5: Decoder<T5
|
|
21
|
+
d5: Decoder<T5>,
|
|
22
22
|
): Decoder<[T1, T2, T3, T4, T5]>;
|
|
23
23
|
export function tuple6<T1, T2, T3, T4, T5, T6>(
|
|
24
24
|
d1: Decoder<T1>,
|
|
@@ -26,5 +26,5 @@ export function tuple6<T1, T2, T3, T4, T5, T6>(
|
|
|
26
26
|
d3: Decoder<T3>,
|
|
27
27
|
d4: Decoder<T4>,
|
|
28
28
|
d5: Decoder<T5>,
|
|
29
|
-
d6: Decoder<T6
|
|
29
|
+
d6: Decoder<T6>,
|
|
30
30
|
): Decoder<[T1, T2, T3, T4, T5, T6]>;
|
package/array.js
DELETED
|
@@ -1,133 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
|
|
3
|
-
Object.defineProperty(exports, "__esModule", {
|
|
4
|
-
value: true
|
|
5
|
-
});
|
|
6
|
-
exports.array = array;
|
|
7
|
-
exports.nonEmptyArray = nonEmptyArray;
|
|
8
|
-
exports.poja = void 0;
|
|
9
|
-
|
|
10
|
-
var _debrief = require("debrief");
|
|
11
|
-
|
|
12
|
-
var _Result = require("lemons/Result");
|
|
13
|
-
|
|
14
|
-
var _utils = require("./utils");
|
|
15
|
-
|
|
16
|
-
function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread(); }
|
|
17
|
-
|
|
18
|
-
function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
|
|
19
|
-
|
|
20
|
-
function _iterableToArray(iter) { if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter); }
|
|
21
|
-
|
|
22
|
-
function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) return _arrayLikeToArray(arr); }
|
|
23
|
-
|
|
24
|
-
function _createForOfIteratorHelper(o, allowArrayLike) { var it = typeof Symbol !== "undefined" && o[Symbol.iterator] || o["@@iterator"]; if (!it) { if (Array.isArray(o) || (it = _unsupportedIterableToArray(o)) || allowArrayLike && o && typeof o.length === "number") { if (it) o = it; var i = 0; var F = function F() {}; return { s: F, n: function n() { if (i >= o.length) return { done: true }; return { done: false, value: o[i++] }; }, e: function e(_e) { throw _e; }, f: F }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } var normalCompletion = true, didErr = false, err; return { s: function s() { it = it.call(o); }, n: function n() { var step = it.next(); normalCompletion = step.done; return step; }, e: function e(_e2) { didErr = true; err = _e2; }, f: function f() { try { if (!normalCompletion && it["return"] != null) it["return"](); } finally { if (didErr) throw err; } } }; }
|
|
25
|
-
|
|
26
|
-
function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
|
|
27
|
-
|
|
28
|
-
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; }
|
|
29
|
-
|
|
30
|
-
/**
|
|
31
|
-
* Like a "Plain Old JavaScript Object", but for arrays: "Plain Old JavaScript
|
|
32
|
-
* Array" ^_^
|
|
33
|
-
*/
|
|
34
|
-
var poja = function poja(blob) {
|
|
35
|
-
if (!Array.isArray(blob)) {
|
|
36
|
-
return (0, _Result.Err)((0, _debrief.annotate)(blob, 'Must be an array'));
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
return (0, _Result.Ok)( // NOTE: Since Flow 0.98, Array.isArray() returns $ReadOnlyArray<mixed>
|
|
40
|
-
// instead of Array<mixed>. For rationale, see
|
|
41
|
-
// https://github.com/facebook/flow/issues/7684. In this case, we
|
|
42
|
-
// don't want to output read-only types because it's up to the user of
|
|
43
|
-
// decoders to determine what they want to do with the decoded output.
|
|
44
|
-
// If they want to write items into the array, that's fine!
|
|
45
|
-
// The fastest way to turn a read-only array into a normal array in
|
|
46
|
-
// Javascript is to use .slice() on it, see this benchmark:
|
|
47
|
-
// http://jsben.ch/lO6C5
|
|
48
|
-
blob.slice());
|
|
49
|
-
};
|
|
50
|
-
/**
|
|
51
|
-
* Given an iterable of Result instances, exhaust them all and return:
|
|
52
|
-
* - An [index, err] tuple, indicating the (index of the) first Err instance
|
|
53
|
-
* encountered; or
|
|
54
|
-
* - a new Ok with an array of all unwrapped Ok'ed values
|
|
55
|
-
*/
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
exports.poja = poja;
|
|
59
|
-
|
|
60
|
-
function all(iterable, blobs) {
|
|
61
|
-
var results = [];
|
|
62
|
-
var index = 0;
|
|
63
|
-
|
|
64
|
-
var _iterator = _createForOfIteratorHelper(iterable),
|
|
65
|
-
_step;
|
|
66
|
-
|
|
67
|
-
try {
|
|
68
|
-
for (_iterator.s(); !(_step = _iterator.n()).done;) {
|
|
69
|
-
var result = _step.value;
|
|
70
|
-
|
|
71
|
-
try {
|
|
72
|
-
var value = result.unwrap();
|
|
73
|
-
results.push(value);
|
|
74
|
-
} catch (ann) {
|
|
75
|
-
// Rewrite the annotation to include the index information, and inject it into the original blob
|
|
76
|
-
var clone = _toConsumableArray(blobs);
|
|
77
|
-
|
|
78
|
-
clone.splice(index, 1, (0, _debrief.annotate)(ann, ann.annotation !== undefined ? "".concat(ann.annotation, " (at index ").concat(index, ")") : "index ".concat(index))); // const errValue = [];
|
|
79
|
-
// if (index > 0) {
|
|
80
|
-
// errValue.push('...'); // TODO: make special mark, not string!
|
|
81
|
-
// }
|
|
82
|
-
// errValue.push(
|
|
83
|
-
// );
|
|
84
|
-
// if (index < iterable.length - 1) {
|
|
85
|
-
// errValue.push('...'); // TODO: make special mark, not string!
|
|
86
|
-
// }
|
|
87
|
-
|
|
88
|
-
return (0, _Result.Err)((0, _debrief.annotate)(clone));
|
|
89
|
-
}
|
|
90
|
-
|
|
91
|
-
index++;
|
|
92
|
-
}
|
|
93
|
-
} catch (err) {
|
|
94
|
-
_iterator.e(err);
|
|
95
|
-
} finally {
|
|
96
|
-
_iterator.f();
|
|
97
|
-
}
|
|
98
|
-
|
|
99
|
-
return (0, _Result.Ok)(results);
|
|
100
|
-
}
|
|
101
|
-
/**
|
|
102
|
-
* Given a T, builds a decoder that assumes an array input and returns an
|
|
103
|
-
* Array<T>.
|
|
104
|
-
*/
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
function members(decoder) {
|
|
108
|
-
return function (blobs) {
|
|
109
|
-
var results = blobs.map(decoder);
|
|
110
|
-
var result = all(results, blobs);
|
|
111
|
-
return result;
|
|
112
|
-
};
|
|
113
|
-
}
|
|
114
|
-
/**
|
|
115
|
-
* Builds a Decoder that returns Ok for values of `Array<T>`, given a Decoder
|
|
116
|
-
* for `T`. Err otherwise.
|
|
117
|
-
*/
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
function array(decoder) {
|
|
121
|
-
return (0, _utils.compose)(poja, members(decoder));
|
|
122
|
-
}
|
|
123
|
-
/**
|
|
124
|
-
* Builds a Decoder that returns Ok for values of `Array<T>`, but will reject
|
|
125
|
-
* empty arrays.
|
|
126
|
-
*/
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
function nonEmptyArray(decoder) {
|
|
130
|
-
return (0, _utils.compose)(array(decoder), (0, _utils.predicate)(function (arr) {
|
|
131
|
-
return arr.length > 0;
|
|
132
|
-
}, 'Must be non-empty array'));
|
|
133
|
-
}
|
package/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 _Result = require("lemons/Result");
|
|
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, _Result.Ok)(blob) : (0, _Result.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, _Result.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/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 _Result = require("lemons/Result");
|
|
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, _Result.Ok)(blob) : (0, _Result.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, _Result.Ok)(blob) : (0, _Result.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, _Result.Ok)(value) : (0, _Result.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, _Result.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, _Result.Ok)(blob);
|
|
59
|
-
};
|
|
60
|
-
/**
|
|
61
|
-
* Alias of mixed.
|
|
62
|
-
*/
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
exports.mixed = mixed;
|
|
66
|
-
var unknown = mixed;
|
|
67
|
-
exports.unknown = unknown;
|
package/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 _Result = require("lemons/Result");
|
|
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, _Result.Ok)(value) : (0, _Result.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;
|