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/result.mjs
CHANGED
|
@@ -24,58 +24,4 @@ export function err(error) {
|
|
|
24
24
|
value: undefined,
|
|
25
25
|
error: error
|
|
26
26
|
};
|
|
27
|
-
}
|
|
28
|
-
/**
|
|
29
|
-
* Unwrap the value from this Result instance if this is an "Ok" result.
|
|
30
|
-
* Otherwise, will throw the "Err" error via a runtime exception.
|
|
31
|
-
*/
|
|
32
|
-
|
|
33
|
-
export function unwrap(result) {
|
|
34
|
-
if (result.ok) {
|
|
35
|
-
return result.value;
|
|
36
|
-
} else {
|
|
37
|
-
throw result.error;
|
|
38
|
-
}
|
|
39
|
-
}
|
|
40
|
-
export function expect(result, message) {
|
|
41
|
-
if (result.ok) {
|
|
42
|
-
return result.value;
|
|
43
|
-
} else {
|
|
44
|
-
throw message instanceof Error ? message : new Error(message);
|
|
45
|
-
}
|
|
46
|
-
}
|
|
47
|
-
export function dispatch(result, okCallback, errCallback) {
|
|
48
|
-
return result.ok ? okCallback(result.value) : errCallback(result.error);
|
|
49
|
-
}
|
|
50
|
-
/**
|
|
51
|
-
* Like .and(), aka &&, but the second argument gets evaluated lazily only if
|
|
52
|
-
* the first result is an Ok result. If so, it has access to the Ok value from
|
|
53
|
-
* the first argument.
|
|
54
|
-
*/
|
|
55
|
-
|
|
56
|
-
export function andThen(result1, lazyResult2) {
|
|
57
|
-
return result1.ok ? lazyResult2(result1.value) : result1;
|
|
58
|
-
}
|
|
59
|
-
/**
|
|
60
|
-
* Like .or(), aka ||, but the second argument gets evaluated lazily only if
|
|
61
|
-
* the first result is an Err result. If so, it has access to the Err value
|
|
62
|
-
* from the first argument.
|
|
63
|
-
*/
|
|
64
|
-
|
|
65
|
-
export function orElse(result1, lazyResult2) {
|
|
66
|
-
return result1.ok ? result1 : lazyResult2(result1.error);
|
|
67
|
-
}
|
|
68
|
-
/**
|
|
69
|
-
* Transform an Ok result. Will not touch Err results.
|
|
70
|
-
*/
|
|
71
|
-
|
|
72
|
-
export function mapOk(result, mapper) {
|
|
73
|
-
return result.ok ? ok(mapper(result.value)) : result;
|
|
74
|
-
}
|
|
75
|
-
/**
|
|
76
|
-
* Transform an Err value. Will not touch Ok results.
|
|
77
|
-
*/
|
|
78
|
-
|
|
79
|
-
export function mapError(result, mapper) {
|
|
80
|
-
return result.ok ? result : err(mapper(result.error));
|
|
81
27
|
}
|
package/_guard.d.ts
DELETED
package/_guard.js
DELETED
|
@@ -1,22 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
|
|
3
|
-
exports.__esModule = true;
|
|
4
|
-
exports.guard = guard;
|
|
5
|
-
|
|
6
|
-
var _format = require("./format");
|
|
7
|
-
|
|
8
|
-
var _result = require("./result");
|
|
9
|
-
|
|
10
|
-
function guard(decoder, formatter) {
|
|
11
|
-
if (formatter === void 0) {
|
|
12
|
-
formatter = _format.formatInline;
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
return function (blob) {
|
|
16
|
-
return (0, _result.unwrap)((0, _result.mapError)(decoder(blob), function (annotation) {
|
|
17
|
-
var err = new Error('\n' + formatter(annotation));
|
|
18
|
-
err.name = 'Decoding error';
|
|
19
|
-
return err;
|
|
20
|
-
}));
|
|
21
|
-
};
|
|
22
|
-
}
|
package/_guard.js.flow
DELETED
|
@@ -1,20 +0,0 @@
|
|
|
1
|
-
// @flow strict
|
|
2
|
-
|
|
3
|
-
import { formatInline } from './format';
|
|
4
|
-
import { mapError, unwrap } from './result';
|
|
5
|
-
import type { Annotation } from './annotate';
|
|
6
|
-
import type { Decoder, Guard } from './_types';
|
|
7
|
-
|
|
8
|
-
export function guard<T>(
|
|
9
|
-
decoder: Decoder<T>,
|
|
10
|
-
formatter: (Annotation) => string = formatInline,
|
|
11
|
-
): Guard<T> {
|
|
12
|
-
return (blob: mixed) =>
|
|
13
|
-
unwrap(
|
|
14
|
-
mapError(decoder(blob), (annotation) => {
|
|
15
|
-
const err = new Error('\n' + formatter(annotation));
|
|
16
|
-
err.name = 'Decoding error';
|
|
17
|
-
return err;
|
|
18
|
-
}),
|
|
19
|
-
);
|
|
20
|
-
}
|
package/_guard.mjs
DELETED
|
@@ -1,15 +0,0 @@
|
|
|
1
|
-
import { formatInline } from './format.mjs';
|
|
2
|
-
import { mapError, unwrap } from './result.mjs';
|
|
3
|
-
export function guard(decoder, formatter) {
|
|
4
|
-
if (formatter === void 0) {
|
|
5
|
-
formatter = formatInline;
|
|
6
|
-
}
|
|
7
|
-
|
|
8
|
-
return function (blob) {
|
|
9
|
-
return unwrap(mapError(decoder(blob), function (annotation) {
|
|
10
|
-
var err = new Error('\n' + formatter(annotation));
|
|
11
|
-
err.name = 'Decoding error';
|
|
12
|
-
return err;
|
|
13
|
-
}));
|
|
14
|
-
};
|
|
15
|
-
}
|
package/_types.d.ts
DELETED
|
@@ -1,13 +0,0 @@
|
|
|
1
|
-
import { Annotation } from './annotate';
|
|
2
|
-
import { Result } from './result';
|
|
3
|
-
|
|
4
|
-
export type Scalar = string | number | boolean | symbol | undefined | null;
|
|
5
|
-
|
|
6
|
-
export type Predicate<T> = (value: T) => boolean;
|
|
7
|
-
export type DecodeResult<T> = Result<T, Annotation>;
|
|
8
|
-
|
|
9
|
-
export type Decoder<T, F = unknown> = (blob: F) => DecodeResult<T>;
|
|
10
|
-
export type Guard<T> = (blob: unknown) => T;
|
|
11
|
-
|
|
12
|
-
export type DecoderType<T> = T extends Decoder<infer V> ? V : never;
|
|
13
|
-
export type GuardType<T> = T extends Guard<infer V> ? V : never;
|
package/_types.js
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
"use strict";
|
package/_types.js.flow
DELETED
|
@@ -1,20 +0,0 @@
|
|
|
1
|
-
// @flow strict
|
|
2
|
-
|
|
3
|
-
import type { Annotation } from './annotate';
|
|
4
|
-
import type { Result } from './result';
|
|
5
|
-
|
|
6
|
-
export type Scalar = string | number | boolean | symbol | void | null;
|
|
7
|
-
|
|
8
|
-
export type Predicate<T> = (T) => boolean;
|
|
9
|
-
export type DecodeResult<T> = Result<T, Annotation>;
|
|
10
|
-
|
|
11
|
-
export type Decoder<T, F = mixed> = (F) => DecodeResult<T>;
|
|
12
|
-
export type Guard<T> = (mixed) => T;
|
|
13
|
-
|
|
14
|
-
/**
|
|
15
|
-
* A "type function" which informs Flow about how a type will be modified at runtime.
|
|
16
|
-
* Read this as "given a Guard of type T, I can produce a value of type T". This
|
|
17
|
-
* definition helps construct $ObjMap types.
|
|
18
|
-
*/
|
|
19
|
-
export type DecoderType = <T>(Decoder<T>) => T;
|
|
20
|
-
export type GuardType = <T>(Guard<T>) => T;
|
package/_types.mjs
DELETED
|
File without changes
|
package/core/array.d.ts
DELETED
|
@@ -1,8 +0,0 @@
|
|
|
1
|
-
/// <reference lib="es6" />
|
|
2
|
-
|
|
3
|
-
import { Decoder } from '../_types';
|
|
4
|
-
|
|
5
|
-
export const poja: Decoder<unknown[]>;
|
|
6
|
-
export function array<T>(decoder: Decoder<T>): Decoder<T[]>;
|
|
7
|
-
export function nonEmptyArray<T>(decoder: Decoder<T>): Decoder<[T, ...T[]]>;
|
|
8
|
-
export function set<T>(decoder: Decoder<T>): Decoder<Set<T>>;
|
package/core/array.js
DELETED
|
@@ -1,115 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
|
|
3
|
-
exports.__esModule = true;
|
|
4
|
-
exports.array = array;
|
|
5
|
-
exports.nonEmptyArray = nonEmptyArray;
|
|
6
|
-
exports.poja = void 0;
|
|
7
|
-
exports.set = set;
|
|
8
|
-
|
|
9
|
-
var _annotate = require("../annotate");
|
|
10
|
-
|
|
11
|
-
var _composition = require("./composition");
|
|
12
|
-
|
|
13
|
-
var _result = require("../result");
|
|
14
|
-
|
|
15
|
-
/**
|
|
16
|
-
* Like a "Plain Old JavaScript Object", but for arrays: "Plain Old JavaScript
|
|
17
|
-
* Array" ^_^
|
|
18
|
-
*/
|
|
19
|
-
var poja = function poja(blob) {
|
|
20
|
-
if (!Array.isArray(blob)) {
|
|
21
|
-
return (0, _result.err)((0, _annotate.annotate)(blob, 'Must be an array'));
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
return (0, _result.ok)( // NOTE: Since Flow 0.98, Array.isArray() returns $ReadOnlyArray<mixed>
|
|
25
|
-
// instead of Array<mixed>. For rationale, see
|
|
26
|
-
// https://github.com/facebook/flow/issues/7684. In this case, we
|
|
27
|
-
// don't want to output read-only types because it's up to the user of
|
|
28
|
-
// decoders to determine what they want to do with the decoded output.
|
|
29
|
-
// If they want to write items into the array, that's fine!
|
|
30
|
-
// The fastest way to turn a read-only array into a normal array in
|
|
31
|
-
// Javascript is to use .slice() on it, see this benchmark:
|
|
32
|
-
// http://jsben.ch/lO6C5
|
|
33
|
-
blob.slice());
|
|
34
|
-
};
|
|
35
|
-
/**
|
|
36
|
-
* Given an array of Result instances, loop over them all and return:
|
|
37
|
-
* - An [index, err] tuple, indicating the (index of the) first Err instance
|
|
38
|
-
* encountered; or
|
|
39
|
-
* - a new Ok with an array of all unwrapped Ok'ed values
|
|
40
|
-
*/
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
exports.poja = poja;
|
|
44
|
-
|
|
45
|
-
function all(items, blobs) {
|
|
46
|
-
var results = [];
|
|
47
|
-
|
|
48
|
-
for (var index = 0; index < items.length; ++index) {
|
|
49
|
-
var result = items[index];
|
|
50
|
-
|
|
51
|
-
if (result.ok) {
|
|
52
|
-
results.push(result.value);
|
|
53
|
-
} else {
|
|
54
|
-
var ann = result.error; // Rewrite the annotation to include the index information, and inject it into the original blob
|
|
55
|
-
|
|
56
|
-
var clone = [].concat(blobs);
|
|
57
|
-
clone.splice(index, 1, (0, _annotate.annotate)(ann, ann.text ? ann.text + " (at index " + index + ")" : "index " + index)); // const errValue = [];
|
|
58
|
-
// if (index > 0) {
|
|
59
|
-
// errValue.push('...'); // TODO: make special mark, not string!
|
|
60
|
-
// }
|
|
61
|
-
// errValue.push(
|
|
62
|
-
// );
|
|
63
|
-
// if (index < iterable.length - 1) {
|
|
64
|
-
// errValue.push('...'); // TODO: make special mark, not string!
|
|
65
|
-
// }
|
|
66
|
-
|
|
67
|
-
return (0, _result.err)((0, _annotate.annotate)(clone));
|
|
68
|
-
}
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
return (0, _result.ok)(results);
|
|
72
|
-
}
|
|
73
|
-
/**
|
|
74
|
-
* Given a T, builds a decoder that assumes an array input and returns an
|
|
75
|
-
* Array<T>.
|
|
76
|
-
*/
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
function members(decoder) {
|
|
80
|
-
return function (blobs) {
|
|
81
|
-
var results = blobs.map(decoder);
|
|
82
|
-
var result = all(results, blobs);
|
|
83
|
-
return result;
|
|
84
|
-
};
|
|
85
|
-
}
|
|
86
|
-
/**
|
|
87
|
-
* Builds a Decoder that returns Ok for values of `Array<T>`, given a Decoder
|
|
88
|
-
* for `T`. Err otherwise.
|
|
89
|
-
*/
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
function array(decoder) {
|
|
93
|
-
return (0, _composition.compose)(poja, members(decoder));
|
|
94
|
-
}
|
|
95
|
-
/**
|
|
96
|
-
* Builds a Decoder that returns Ok for values of `Array<T>`, but will reject
|
|
97
|
-
* empty arrays.
|
|
98
|
-
*/
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
function nonEmptyArray(decoder) {
|
|
102
|
-
return (0, _composition.predicate)(array(decoder), function (arr) {
|
|
103
|
-
return arr.length > 0;
|
|
104
|
-
}, 'Must be non-empty array');
|
|
105
|
-
}
|
|
106
|
-
/**
|
|
107
|
-
* Similar to `array()`, but returns the result as an ES6 Set.
|
|
108
|
-
*/
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
function set(decoder) {
|
|
112
|
-
return (0, _composition.transform)(array(decoder), function (items) {
|
|
113
|
-
return new Set(items);
|
|
114
|
-
});
|
|
115
|
-
}
|
package/core/array.js.flow
DELETED
|
@@ -1,107 +0,0 @@
|
|
|
1
|
-
// @flow strict
|
|
2
|
-
|
|
3
|
-
import { annotate } from '../annotate';
|
|
4
|
-
import { compose, predicate, transform } from './composition';
|
|
5
|
-
import { err, ok } from '../result';
|
|
6
|
-
import type { Decoder, DecodeResult } from '../_types';
|
|
7
|
-
|
|
8
|
-
/**
|
|
9
|
-
* Like a "Plain Old JavaScript Object", but for arrays: "Plain Old JavaScript
|
|
10
|
-
* Array" ^_^
|
|
11
|
-
*/
|
|
12
|
-
export const poja: Decoder<Array<mixed>> = (blob: mixed) => {
|
|
13
|
-
if (!Array.isArray(blob)) {
|
|
14
|
-
return err(annotate(blob, 'Must be an array'));
|
|
15
|
-
}
|
|
16
|
-
return ok(
|
|
17
|
-
// NOTE: Since Flow 0.98, Array.isArray() returns $ReadOnlyArray<mixed>
|
|
18
|
-
// instead of Array<mixed>. For rationale, see
|
|
19
|
-
// https://github.com/facebook/flow/issues/7684. In this case, we
|
|
20
|
-
// don't want to output read-only types because it's up to the user of
|
|
21
|
-
// decoders to determine what they want to do with the decoded output.
|
|
22
|
-
// If they want to write items into the array, that's fine!
|
|
23
|
-
// The fastest way to turn a read-only array into a normal array in
|
|
24
|
-
// Javascript is to use .slice() on it, see this benchmark:
|
|
25
|
-
// http://jsben.ch/lO6C5
|
|
26
|
-
blob.slice(),
|
|
27
|
-
);
|
|
28
|
-
};
|
|
29
|
-
|
|
30
|
-
/**
|
|
31
|
-
* Given an array of Result instances, loop over them all and return:
|
|
32
|
-
* - An [index, err] tuple, indicating the (index of the) first Err instance
|
|
33
|
-
* encountered; or
|
|
34
|
-
* - a new Ok with an array of all unwrapped Ok'ed values
|
|
35
|
-
*/
|
|
36
|
-
function all<T>(
|
|
37
|
-
items: $ReadOnlyArray<DecodeResult<T>>,
|
|
38
|
-
blobs: $ReadOnlyArray<mixed>,
|
|
39
|
-
): DecodeResult<Array<T>> {
|
|
40
|
-
const results: Array<T> = [];
|
|
41
|
-
for (let index = 0; index < items.length; ++index) {
|
|
42
|
-
const result = items[index];
|
|
43
|
-
if (result.ok) {
|
|
44
|
-
results.push(result.value);
|
|
45
|
-
} else {
|
|
46
|
-
const ann = result.error;
|
|
47
|
-
|
|
48
|
-
// Rewrite the annotation to include the index information, and inject it into the original blob
|
|
49
|
-
const clone = [...blobs];
|
|
50
|
-
clone.splice(
|
|
51
|
-
index,
|
|
52
|
-
1,
|
|
53
|
-
annotate(
|
|
54
|
-
ann,
|
|
55
|
-
ann.text ? `${ann.text} (at index ${index})` : `index ${index}`,
|
|
56
|
-
),
|
|
57
|
-
);
|
|
58
|
-
|
|
59
|
-
// const errValue = [];
|
|
60
|
-
// if (index > 0) {
|
|
61
|
-
// errValue.push('...'); // TODO: make special mark, not string!
|
|
62
|
-
// }
|
|
63
|
-
// errValue.push(
|
|
64
|
-
// );
|
|
65
|
-
// if (index < iterable.length - 1) {
|
|
66
|
-
// errValue.push('...'); // TODO: make special mark, not string!
|
|
67
|
-
// }
|
|
68
|
-
return err(annotate(clone));
|
|
69
|
-
}
|
|
70
|
-
}
|
|
71
|
-
return ok(results);
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
/**
|
|
75
|
-
* Given a T, builds a decoder that assumes an array input and returns an
|
|
76
|
-
* Array<T>.
|
|
77
|
-
*/
|
|
78
|
-
function members<T>(decoder: Decoder<T>): Decoder<Array<T>, Array<mixed>> {
|
|
79
|
-
return (blobs: $ReadOnlyArray<mixed>) => {
|
|
80
|
-
const results = blobs.map(decoder);
|
|
81
|
-
const result = all(results, blobs);
|
|
82
|
-
return result;
|
|
83
|
-
};
|
|
84
|
-
}
|
|
85
|
-
|
|
86
|
-
/**
|
|
87
|
-
* Builds a Decoder that returns Ok for values of `Array<T>`, given a Decoder
|
|
88
|
-
* for `T`. Err otherwise.
|
|
89
|
-
*/
|
|
90
|
-
export function array<T>(decoder: Decoder<T>): Decoder<Array<T>> {
|
|
91
|
-
return compose(poja, members(decoder));
|
|
92
|
-
}
|
|
93
|
-
|
|
94
|
-
/**
|
|
95
|
-
* Builds a Decoder that returns Ok for values of `Array<T>`, but will reject
|
|
96
|
-
* empty arrays.
|
|
97
|
-
*/
|
|
98
|
-
export function nonEmptyArray<T>(decoder: Decoder<T>): Decoder<Array<T>> {
|
|
99
|
-
return predicate(array(decoder), (arr) => arr.length > 0, 'Must be non-empty array');
|
|
100
|
-
}
|
|
101
|
-
|
|
102
|
-
/**
|
|
103
|
-
* Similar to `array()`, but returns the result as an ES6 Set.
|
|
104
|
-
*/
|
|
105
|
-
export function set<T>(decoder: Decoder<T>): Decoder<Set<T>> {
|
|
106
|
-
return transform(array(decoder), (items) => new Set(items));
|
|
107
|
-
}
|
package/core/array.mjs
DELETED
|
@@ -1,100 +0,0 @@
|
|
|
1
|
-
import { annotate } from '../annotate.mjs';
|
|
2
|
-
import { compose, predicate, transform } from './composition.mjs';
|
|
3
|
-
import { err, ok } from '../result.mjs';
|
|
4
|
-
|
|
5
|
-
/**
|
|
6
|
-
* Like a "Plain Old JavaScript Object", but for arrays: "Plain Old JavaScript
|
|
7
|
-
* Array" ^_^
|
|
8
|
-
*/
|
|
9
|
-
export var poja = function poja(blob) {
|
|
10
|
-
if (!Array.isArray(blob)) {
|
|
11
|
-
return err(annotate(blob, 'Must be an array'));
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
return ok( // NOTE: Since Flow 0.98, Array.isArray() returns $ReadOnlyArray<mixed>
|
|
15
|
-
// instead of Array<mixed>. For rationale, see
|
|
16
|
-
// https://github.com/facebook/flow/issues/7684. In this case, we
|
|
17
|
-
// don't want to output read-only types because it's up to the user of
|
|
18
|
-
// decoders to determine what they want to do with the decoded output.
|
|
19
|
-
// If they want to write items into the array, that's fine!
|
|
20
|
-
// The fastest way to turn a read-only array into a normal array in
|
|
21
|
-
// Javascript is to use .slice() on it, see this benchmark:
|
|
22
|
-
// http://jsben.ch/lO6C5
|
|
23
|
-
blob.slice());
|
|
24
|
-
};
|
|
25
|
-
/**
|
|
26
|
-
* Given an array of Result instances, loop over them all and return:
|
|
27
|
-
* - An [index, err] tuple, indicating the (index of the) first Err instance
|
|
28
|
-
* encountered; or
|
|
29
|
-
* - a new Ok with an array of all unwrapped Ok'ed values
|
|
30
|
-
*/
|
|
31
|
-
|
|
32
|
-
function all(items, blobs) {
|
|
33
|
-
var results = [];
|
|
34
|
-
|
|
35
|
-
for (var index = 0; index < items.length; ++index) {
|
|
36
|
-
var result = items[index];
|
|
37
|
-
|
|
38
|
-
if (result.ok) {
|
|
39
|
-
results.push(result.value);
|
|
40
|
-
} else {
|
|
41
|
-
var ann = result.error; // Rewrite the annotation to include the index information, and inject it into the original blob
|
|
42
|
-
|
|
43
|
-
var clone = [].concat(blobs);
|
|
44
|
-
clone.splice(index, 1, annotate(ann, ann.text ? ann.text + " (at index " + index + ")" : "index " + index)); // const errValue = [];
|
|
45
|
-
// if (index > 0) {
|
|
46
|
-
// errValue.push('...'); // TODO: make special mark, not string!
|
|
47
|
-
// }
|
|
48
|
-
// errValue.push(
|
|
49
|
-
// );
|
|
50
|
-
// if (index < iterable.length - 1) {
|
|
51
|
-
// errValue.push('...'); // TODO: make special mark, not string!
|
|
52
|
-
// }
|
|
53
|
-
|
|
54
|
-
return err(annotate(clone));
|
|
55
|
-
}
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
return ok(results);
|
|
59
|
-
}
|
|
60
|
-
/**
|
|
61
|
-
* Given a T, builds a decoder that assumes an array input and returns an
|
|
62
|
-
* Array<T>.
|
|
63
|
-
*/
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
function members(decoder) {
|
|
67
|
-
return function (blobs) {
|
|
68
|
-
var results = blobs.map(decoder);
|
|
69
|
-
var result = all(results, blobs);
|
|
70
|
-
return result;
|
|
71
|
-
};
|
|
72
|
-
}
|
|
73
|
-
/**
|
|
74
|
-
* Builds a Decoder that returns Ok for values of `Array<T>`, given a Decoder
|
|
75
|
-
* for `T`. Err otherwise.
|
|
76
|
-
*/
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
export function array(decoder) {
|
|
80
|
-
return compose(poja, members(decoder));
|
|
81
|
-
}
|
|
82
|
-
/**
|
|
83
|
-
* Builds a Decoder that returns Ok for values of `Array<T>`, but will reject
|
|
84
|
-
* empty arrays.
|
|
85
|
-
*/
|
|
86
|
-
|
|
87
|
-
export function nonEmptyArray(decoder) {
|
|
88
|
-
return predicate(array(decoder), function (arr) {
|
|
89
|
-
return arr.length > 0;
|
|
90
|
-
}, 'Must be non-empty array');
|
|
91
|
-
}
|
|
92
|
-
/**
|
|
93
|
-
* Similar to `array()`, but returns the result as an ES6 Set.
|
|
94
|
-
*/
|
|
95
|
-
|
|
96
|
-
export function set(decoder) {
|
|
97
|
-
return transform(array(decoder), function (items) {
|
|
98
|
-
return new Set(items);
|
|
99
|
-
});
|
|
100
|
-
}
|
package/core/boolean.d.ts
DELETED
package/core/boolean.js
DELETED
|
@@ -1,40 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
|
|
3
|
-
exports.__esModule = true;
|
|
4
|
-
exports.truthy = exports.numericBoolean = exports["boolean"] = void 0;
|
|
5
|
-
|
|
6
|
-
var _annotate = require("../annotate");
|
|
7
|
-
|
|
8
|
-
var _result = require("../result");
|
|
9
|
-
|
|
10
|
-
var _number = require("./number");
|
|
11
|
-
|
|
12
|
-
var _composition = require("./composition");
|
|
13
|
-
|
|
14
|
-
/**
|
|
15
|
-
* Decoder that only returns Ok for boolean inputs. Err otherwise.
|
|
16
|
-
*/
|
|
17
|
-
var _boolean = function _boolean(blob) {
|
|
18
|
-
return typeof blob === 'boolean' ? (0, _result.ok)(blob) : (0, _result.err)((0, _annotate.annotate)(blob, 'Must be boolean'));
|
|
19
|
-
};
|
|
20
|
-
/**
|
|
21
|
-
* Decoder that returns true for all truthy values, and false otherwise. Never fails.
|
|
22
|
-
*/
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
exports["boolean"] = _boolean;
|
|
26
|
-
|
|
27
|
-
var truthy = function truthy(blob) {
|
|
28
|
-
return (0, _result.ok)(!!blob);
|
|
29
|
-
};
|
|
30
|
-
/**
|
|
31
|
-
* Decoder that only returns Ok for numeric input values representing booleans.
|
|
32
|
-
* Returns their boolean representation. Err otherwise.
|
|
33
|
-
*/
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
exports.truthy = truthy;
|
|
37
|
-
var numericBoolean = (0, _composition.transform)(_number.number, function (n) {
|
|
38
|
-
return !!n;
|
|
39
|
-
});
|
|
40
|
-
exports.numericBoolean = numericBoolean;
|
package/core/boolean.js.flow
DELETED
|
@@ -1,27 +0,0 @@
|
|
|
1
|
-
// @flow strict
|
|
2
|
-
|
|
3
|
-
import { annotate } from '../annotate';
|
|
4
|
-
import { err, ok } from '../result';
|
|
5
|
-
import { number } from './number';
|
|
6
|
-
import { transform } from './composition';
|
|
7
|
-
import type { Decoder } from '../_types';
|
|
8
|
-
|
|
9
|
-
/**
|
|
10
|
-
* Decoder that only returns Ok for boolean inputs. Err otherwise.
|
|
11
|
-
*/
|
|
12
|
-
export const boolean: Decoder<boolean> = (blob: mixed) => {
|
|
13
|
-
return typeof blob === 'boolean' ? ok(blob) : err(annotate(blob, 'Must be boolean'));
|
|
14
|
-
};
|
|
15
|
-
|
|
16
|
-
/**
|
|
17
|
-
* Decoder that returns true for all truthy values, and false otherwise. Never fails.
|
|
18
|
-
*/
|
|
19
|
-
export const truthy: Decoder<boolean> = (blob: mixed) => {
|
|
20
|
-
return ok(!!blob);
|
|
21
|
-
};
|
|
22
|
-
|
|
23
|
-
/**
|
|
24
|
-
* Decoder that only returns Ok for numeric input values representing booleans.
|
|
25
|
-
* Returns their boolean representation. Err otherwise.
|
|
26
|
-
*/
|
|
27
|
-
export const numericBoolean: Decoder<boolean> = transform(number, (n) => !!n);
|
package/core/boolean.mjs
DELETED
|
@@ -1,28 +0,0 @@
|
|
|
1
|
-
import { annotate } from '../annotate.mjs';
|
|
2
|
-
import { err, ok } from '../result.mjs';
|
|
3
|
-
import { number } from './number.mjs';
|
|
4
|
-
import { transform } from './composition.mjs';
|
|
5
|
-
|
|
6
|
-
/**
|
|
7
|
-
* Decoder that only returns Ok for boolean inputs. Err otherwise.
|
|
8
|
-
*/
|
|
9
|
-
var _boolean = function _boolean(blob) {
|
|
10
|
-
return typeof blob === 'boolean' ? ok(blob) : err(annotate(blob, 'Must be boolean'));
|
|
11
|
-
};
|
|
12
|
-
/**
|
|
13
|
-
* Decoder that returns true for all truthy values, and false otherwise. Never fails.
|
|
14
|
-
*/
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
export { _boolean as boolean };
|
|
18
|
-
export var truthy = function truthy(blob) {
|
|
19
|
-
return ok(!!blob);
|
|
20
|
-
};
|
|
21
|
-
/**
|
|
22
|
-
* Decoder that only returns Ok for numeric input values representing booleans.
|
|
23
|
-
* Returns their boolean representation. Err otherwise.
|
|
24
|
-
*/
|
|
25
|
-
|
|
26
|
-
export var numericBoolean = transform(number, function (n) {
|
|
27
|
-
return !!n;
|
|
28
|
-
});
|
package/core/composition.d.ts
DELETED
|
@@ -1,18 +0,0 @@
|
|
|
1
|
-
import { Decoder } from '../_types';
|
|
2
|
-
|
|
3
|
-
export function transform<T, V>(decoder: Decoder<T>, mapper: (value: T) => V): Decoder<V>;
|
|
4
|
-
export function compose<T, V>(decoder: Decoder<T>, next: Decoder<V, T>): Decoder<V>;
|
|
5
|
-
export function predicate<T, N extends T>(
|
|
6
|
-
decoder: Decoder<T>,
|
|
7
|
-
predicate: (value: T) => value is N,
|
|
8
|
-
msg: string,
|
|
9
|
-
): Decoder<N>;
|
|
10
|
-
export function predicate<T>(
|
|
11
|
-
decoder: Decoder<T>,
|
|
12
|
-
predicate: (value: T) => boolean,
|
|
13
|
-
msg: string,
|
|
14
|
-
): Decoder<T>;
|
|
15
|
-
export function prep<T, I>(
|
|
16
|
-
mapperFn: (blob: unknown) => I,
|
|
17
|
-
decoder: Decoder<T, I>,
|
|
18
|
-
): Decoder<T>;
|