decoders 2.0.0-beta6 → 2.0.0-beta7
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/NotSupportedTSVersion.d.ts +1 -0
- package/_guard.d.ts +7 -0
- package/_guard.js +2 -6
- package/_guard.js.flow +3 -3
- package/{_esm/_guard.js → _guard.mjs} +3 -3
- package/_types.d.ts +13 -0
- package/{_esm/_types.js → _types.mjs} +0 -0
- package/_utils.d.ts +10 -0
- package/{_esm/_utils.js → _utils.mjs} +0 -0
- package/annotate.d.ts +62 -0
- package/{_esm/annotate.js → annotate.mjs} +0 -0
- package/core/_helpers.d.ts +79 -0
- package/core/array.d.ts +5 -0
- package/core/array.js +5 -9
- package/core/array.js.flow +5 -5
- package/{_esm/core/array.js → core/array.mjs} +7 -7
- package/core/boolean.d.ts +5 -0
- package/core/boolean.js +4 -8
- package/core/boolean.js.flow +3 -5
- package/{_esm/core/boolean.js → core/boolean.mjs} +6 -6
- package/core/composition.d.ts +12 -0
- package/core/composition.js +6 -10
- package/core/composition.js.flow +6 -6
- package/{_esm/core/composition.js → core/composition.mjs} +7 -7
- package/core/constants.d.ts +11 -0
- package/core/constants.js +6 -10
- package/core/constants.js.flow +7 -9
- package/{_esm/core/constants.js → core/constants.mjs} +7 -7
- package/core/date.d.ts +4 -0
- package/core/date.js +3 -7
- package/core/date.js.flow +2 -4
- package/{_esm/core/date.js → core/date.mjs} +6 -6
- package/core/describe.d.ts +3 -0
- package/core/describe.js +2 -6
- package/core/describe.js.flow +2 -2
- package/{_esm/core/describe.js → core/describe.mjs} +3 -3
- package/core/dispatch.d.ts +8 -0
- package/core/dispatch.js +2 -6
- package/core/dispatch.js.flow +2 -2
- package/{_esm/core/dispatch.js → core/dispatch.mjs} +4 -4
- package/core/either.d.ts +61 -0
- package/core/either.js +7 -11
- package/core/either.js.flow +6 -6
- package/{_esm/core/either.js → core/either.mjs} +8 -8
- package/core/fail.d.ts +3 -0
- package/core/fail.js +2 -6
- package/core/fail.js.flow +2 -2
- package/{_esm/core/fail.js → core/fail.mjs} +3 -3
- package/core/instanceOf.d.ts +3 -0
- package/core/instanceOf.js +2 -6
- package/core/instanceOf.js.flow +3 -3
- package/core/instanceOf.mjs +8 -0
- package/core/json.d.ts +11 -0
- package/core/json.mjs +15 -0
- package/core/lazy.d.ts +3 -0
- package/{_esm/core/lazy.js → core/lazy.mjs} +0 -0
- package/core/mapping.d.ts +6 -0
- package/core/mapping.js +4 -8
- package/core/mapping.js.flow +3 -3
- package/{_esm/core/mapping.js → core/mapping.mjs} +7 -7
- package/core/number.d.ts +6 -0
- package/core/number.js +2 -6
- package/core/number.js.flow +3 -3
- package/{_esm/core/number.js → core/number.mjs} +4 -4
- package/core/object.d.ts +33 -0
- package/core/object.js +7 -11
- package/core/object.js.flow +7 -7
- package/{_esm/core/object.js → core/object.mjs} +9 -9
- package/core/optional.d.ts +5 -0
- package/core/optional.js +4 -8
- package/core/optional.js.flow +3 -3
- package/{_esm/core/optional.js → core/optional.mjs} +6 -6
- package/core/string.d.ts +7 -0
- package/core/string.js +5 -9
- package/core/string.js.flow +5 -7
- package/{_esm/core/string.js → core/string.mjs} +7 -7
- package/core/tuple.d.ts +30 -0
- package/{_esm/core/tuple.js → core/tuple.mjs} +4 -4
- package/format.d.ts +4 -0
- package/{format/inline.js → format.js} +6 -1
- package/{_esm/format/inline.js.flow → format.js.flow} +6 -2
- package/{_esm/format/inline.js → format.mjs} +4 -1
- package/{_esm/index.js.flow → index.d.ts} +3 -23
- package/{_esm/index.js → index.mjs} +19 -19
- package/package.json +15 -3
- package/result.d.ts +39 -0
- package/result.js +0 -62
- package/result.js.flow +0 -60
- package/{_esm/result.js → result.mjs} +0 -54
- package/_esm/_guard.js.flow +0 -20
- package/_esm/_types.js.flow +0 -20
- package/_esm/_utils.js.flow +0 -97
- package/_esm/annotate.js.flow +0 -218
- package/_esm/core/array.js.flow +0 -103
- package/_esm/core/boolean.js.flow +0 -29
- package/_esm/core/composition.js.flow +0 -43
- package/_esm/core/constants.js.flow +0 -46
- package/_esm/core/date.js.flow +0 -40
- package/_esm/core/describe.js.flow +0 -17
- package/_esm/core/dispatch.js.flow +0 -58
- package/_esm/core/either.js.flow +0 -151
- package/_esm/core/fail.js.flow +0 -12
- package/_esm/core/instanceOf.js +0 -8
- package/_esm/core/instanceOf.js.flow +0 -20
- package/_esm/core/json.js +0 -15
- package/_esm/core/json.js.flow +0 -28
- package/_esm/core/lazy.js.flow +0 -15
- package/_esm/core/mapping.js.flow +0 -54
- package/_esm/core/number.js.flow +0 -34
- package/_esm/core/object.js.flow +0 -203
- package/_esm/core/optional.js.flow +0 -41
- package/_esm/core/string.js.flow +0 -82
- package/_esm/core/tuple.js.flow +0 -214
- package/_esm/format/index.js +0 -2
- package/_esm/format/index.js.flow +0 -4
- package/_esm/format/short.js +0 -4
- package/_esm/format/short.js.flow +0 -8
- package/_esm/result.js.flow +0 -158
- package/format/index.js +0 -12
- package/format/index.js.flow +0 -4
- package/format/inline.js.flow +0 -122
- package/format/short.js +0 -10
- package/format/short.js.flow +0 -8
|
@@ -0,0 +1 @@
|
|
|
1
|
+
"Package `decoders` requires TypeScript >= 4.1.0"
|
package/_guard.d.ts
ADDED
package/_guard.js
CHANGED
|
@@ -3,13 +3,9 @@
|
|
|
3
3
|
exports.__esModule = true;
|
|
4
4
|
exports.guard = guard;
|
|
5
5
|
|
|
6
|
-
var Result = _interopRequireWildcard(require("./result"));
|
|
7
|
-
|
|
8
6
|
var _format = require("./format");
|
|
9
7
|
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { "default": obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj["default"] = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
|
|
8
|
+
var _result = require("./result");
|
|
13
9
|
|
|
14
10
|
function guard(decoder, formatter) {
|
|
15
11
|
if (formatter === void 0) {
|
|
@@ -17,7 +13,7 @@ function guard(decoder, formatter) {
|
|
|
17
13
|
}
|
|
18
14
|
|
|
19
15
|
return function (blob) {
|
|
20
|
-
return
|
|
16
|
+
return (0, _result.unwrap)((0, _result.mapError)(decoder(blob), function (annotation) {
|
|
21
17
|
var err = new Error('\n' + formatter(annotation));
|
|
22
18
|
err.name = 'Decoding error';
|
|
23
19
|
return err;
|
package/_guard.js.flow
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
// @flow strict
|
|
2
2
|
|
|
3
|
-
import * as Result from './result';
|
|
4
3
|
import { formatInline } from './format';
|
|
4
|
+
import { mapError, unwrap } from './result';
|
|
5
5
|
import type { Annotation } from './annotate';
|
|
6
6
|
import type { Decoder, Guard } from './_types';
|
|
7
7
|
|
|
@@ -10,8 +10,8 @@ export function guard<T>(
|
|
|
10
10
|
formatter: (Annotation) => string = formatInline,
|
|
11
11
|
): Guard<T> {
|
|
12
12
|
return (blob: mixed) =>
|
|
13
|
-
|
|
14
|
-
|
|
13
|
+
unwrap(
|
|
14
|
+
mapError(decoder(blob), (annotation) => {
|
|
15
15
|
const err = new Error('\n' + formatter(annotation));
|
|
16
16
|
err.name = 'Decoding error';
|
|
17
17
|
return err;
|
|
@@ -1,12 +1,12 @@
|
|
|
1
|
-
import
|
|
2
|
-
import {
|
|
1
|
+
import { formatInline } from './format.mjs';
|
|
2
|
+
import { mapError, unwrap } from './result.mjs';
|
|
3
3
|
export function guard(decoder, formatter) {
|
|
4
4
|
if (formatter === void 0) {
|
|
5
5
|
formatter = formatInline;
|
|
6
6
|
}
|
|
7
7
|
|
|
8
8
|
return function (blob) {
|
|
9
|
-
return
|
|
9
|
+
return unwrap(mapError(decoder(blob), function (annotation) {
|
|
10
10
|
var err = new Error('\n' + formatter(annotation));
|
|
11
11
|
err.name = 'Decoding error';
|
|
12
12
|
return err;
|
package/_types.d.ts
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
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;
|
|
File without changes
|
package/_utils.d.ts
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { Annotation } from './annotate';
|
|
2
|
+
|
|
3
|
+
export function isDate(value: unknown): boolean;
|
|
4
|
+
export function asDate(value: unknown): Date | null;
|
|
5
|
+
export function isMultiline(s: string): boolean;
|
|
6
|
+
export function indent(s: string, prefix?: string): string;
|
|
7
|
+
export function summarize(
|
|
8
|
+
ann: Annotation,
|
|
9
|
+
keypath?: ReadonlyArray<number | string>,
|
|
10
|
+
): string[];
|
|
File without changes
|
package/annotate.d.ts
ADDED
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
export interface ObjectAnnotation {
|
|
2
|
+
readonly type: 'object';
|
|
3
|
+
readonly fields: { readonly [key: string]: Annotation };
|
|
4
|
+
readonly text?: string;
|
|
5
|
+
}
|
|
6
|
+
|
|
7
|
+
export interface ArrayAnnotation {
|
|
8
|
+
readonly type: 'array';
|
|
9
|
+
readonly items: readonly Annotation[];
|
|
10
|
+
readonly text?: string;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export interface ScalarAnnotation {
|
|
14
|
+
readonly type: 'scalar';
|
|
15
|
+
readonly value: unknown;
|
|
16
|
+
readonly text?: string;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export interface FunctionAnnotation {
|
|
20
|
+
readonly type: 'function';
|
|
21
|
+
readonly text?: string;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export interface CircularRefAnnotation {
|
|
25
|
+
readonly type: 'circular-ref';
|
|
26
|
+
readonly text?: string;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export interface UnknownAnnotation {
|
|
30
|
+
readonly type: 'unknown';
|
|
31
|
+
readonly value: unknown;
|
|
32
|
+
readonly text?: string;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
export type Annotation =
|
|
36
|
+
| ObjectAnnotation
|
|
37
|
+
| ArrayAnnotation
|
|
38
|
+
| ScalarAnnotation
|
|
39
|
+
| FunctionAnnotation
|
|
40
|
+
| CircularRefAnnotation
|
|
41
|
+
| UnknownAnnotation;
|
|
42
|
+
|
|
43
|
+
export function object(
|
|
44
|
+
fields: { readonly [key: string]: Annotation },
|
|
45
|
+
text?: string,
|
|
46
|
+
): ObjectAnnotation;
|
|
47
|
+
export function array(items: readonly Annotation[], text?: string): ArrayAnnotation;
|
|
48
|
+
export function func(text?: string): FunctionAnnotation;
|
|
49
|
+
export function unknown(value: unknown, text?: string): UnknownAnnotation;
|
|
50
|
+
export function scalar(value: unknown, text?: string): ScalarAnnotation;
|
|
51
|
+
export function circularRef(text?: string): CircularRefAnnotation;
|
|
52
|
+
export function updateText<A extends Annotation>(annotation: A, text?: string): A;
|
|
53
|
+
export function merge(
|
|
54
|
+
objAnnotation: ObjectAnnotation,
|
|
55
|
+
fields: { readonly [key: string]: Annotation },
|
|
56
|
+
): ObjectAnnotation;
|
|
57
|
+
export function asAnnotation(thing: unknown): Annotation | void;
|
|
58
|
+
export function annotate(value: unknown, text?: string): Annotation;
|
|
59
|
+
export function annotateObject(
|
|
60
|
+
obj: { readonly [field: string]: unknown },
|
|
61
|
+
text?: string,
|
|
62
|
+
): ObjectAnnotation;
|
|
File without changes
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Given a type like:
|
|
3
|
+
*
|
|
4
|
+
* {
|
|
5
|
+
* a: string;
|
|
6
|
+
* b: number | undefined;
|
|
7
|
+
* c: null | undefined;
|
|
8
|
+
* d: null;
|
|
9
|
+
* e: undefined;
|
|
10
|
+
* }
|
|
11
|
+
*
|
|
12
|
+
* Will drop all the "undefined" types. In this case, only "e":
|
|
13
|
+
*
|
|
14
|
+
* {
|
|
15
|
+
* a: string;
|
|
16
|
+
* b: number | undefined;
|
|
17
|
+
* c: null | undefined;
|
|
18
|
+
* d: null;
|
|
19
|
+
* }
|
|
20
|
+
*
|
|
21
|
+
*/
|
|
22
|
+
type Compact<T> = { [K in IsDefined<T, keyof T>]: T[K] };
|
|
23
|
+
|
|
24
|
+
type IsDefined<T, K extends keyof T> = K extends any
|
|
25
|
+
? T[K] extends undefined
|
|
26
|
+
? never
|
|
27
|
+
: K
|
|
28
|
+
: never;
|
|
29
|
+
|
|
30
|
+
//
|
|
31
|
+
// HACK:
|
|
32
|
+
// These weird conditionals test whether TypeScript is configured with the
|
|
33
|
+
// `strictNullChecks` compiler option. We use these definitions to influence
|
|
34
|
+
// what's considered a "required" vs an "optional" key for the AllowImplicit
|
|
35
|
+
// type.
|
|
36
|
+
//
|
|
37
|
+
// If strictNullChecks is false, then we should not be emitting any `?` fields
|
|
38
|
+
// and consider all fields "required" because everything is optional by default
|
|
39
|
+
// in that mode anyway.
|
|
40
|
+
//
|
|
41
|
+
type NoStrictNullChecks = undefined extends string ? 1 : undefined;
|
|
42
|
+
// ^^^^^^^^^^^^^^^^^^^^^^^^
|
|
43
|
+
type StrictNullChecks = undefined extends string ? undefined : 1;
|
|
44
|
+
// ^^^^^^^^^^^^^^^^^^^^^^^^
|
|
45
|
+
|
|
46
|
+
export type RequiredKeys<T> = keyof Compact<{
|
|
47
|
+
[K in keyof T]: undefined extends T[K] ? NoStrictNullChecks : 1;
|
|
48
|
+
}>;
|
|
49
|
+
|
|
50
|
+
export type OptionalKeys<T> = keyof Compact<{
|
|
51
|
+
[K in keyof T]: undefined extends T[K] ? 1 : StrictNullChecks;
|
|
52
|
+
}>;
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* Transforms an object type, by marking all fields that contain "undefined"
|
|
56
|
+
* with a question mark, i.e. allowing implicit-undefineds when
|
|
57
|
+
* explicit-undefined are also allowed.
|
|
58
|
+
*
|
|
59
|
+
* For example, if:
|
|
60
|
+
*
|
|
61
|
+
* type User = {
|
|
62
|
+
* name: string;
|
|
63
|
+
* age: number | null | undefined;
|
|
64
|
+
* }
|
|
65
|
+
*
|
|
66
|
+
* Then AllowImplicit<User> will become equivalent to:
|
|
67
|
+
*
|
|
68
|
+
* {
|
|
69
|
+
* name: string;
|
|
70
|
+
* age?: number | null;
|
|
71
|
+
* ^
|
|
72
|
+
* Note the question mark
|
|
73
|
+
* }
|
|
74
|
+
*/
|
|
75
|
+
type AllowImplicit<T> = { [K in RequiredKeys<T>]-?: T[K] } & {
|
|
76
|
+
[K in OptionalKeys<T>]+?: Exclude<T[K], undefined>;
|
|
77
|
+
};
|
|
78
|
+
|
|
79
|
+
export { AllowImplicit };
|
package/core/array.d.ts
ADDED
package/core/array.js
CHANGED
|
@@ -5,15 +5,11 @@ exports.array = array;
|
|
|
5
5
|
exports.nonEmptyArray = nonEmptyArray;
|
|
6
6
|
exports.poja = void 0;
|
|
7
7
|
|
|
8
|
-
var Result = _interopRequireWildcard(require("../result"));
|
|
9
|
-
|
|
10
8
|
var _annotate = require("../annotate");
|
|
11
9
|
|
|
12
10
|
var _composition = require("./composition");
|
|
13
11
|
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { "default": obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj["default"] = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
|
|
12
|
+
var _result = require("../result");
|
|
17
13
|
|
|
18
14
|
/**
|
|
19
15
|
* Like a "Plain Old JavaScript Object", but for arrays: "Plain Old JavaScript
|
|
@@ -21,10 +17,10 @@ function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj &&
|
|
|
21
17
|
*/
|
|
22
18
|
var poja = function poja(blob) {
|
|
23
19
|
if (!Array.isArray(blob)) {
|
|
24
|
-
return
|
|
20
|
+
return (0, _result.err)((0, _annotate.annotate)(blob, 'Must be an array'));
|
|
25
21
|
}
|
|
26
22
|
|
|
27
|
-
return
|
|
23
|
+
return (0, _result.ok)( // NOTE: Since Flow 0.98, Array.isArray() returns $ReadOnlyArray<mixed>
|
|
28
24
|
// instead of Array<mixed>. For rationale, see
|
|
29
25
|
// https://github.com/facebook/flow/issues/7684. In this case, we
|
|
30
26
|
// don't want to output read-only types because it's up to the user of
|
|
@@ -67,11 +63,11 @@ function all(items, blobs) {
|
|
|
67
63
|
// errValue.push('...'); // TODO: make special mark, not string!
|
|
68
64
|
// }
|
|
69
65
|
|
|
70
|
-
return
|
|
66
|
+
return (0, _result.err)((0, _annotate.annotate)(clone));
|
|
71
67
|
}
|
|
72
68
|
}
|
|
73
69
|
|
|
74
|
-
return
|
|
70
|
+
return (0, _result.ok)(results);
|
|
75
71
|
}
|
|
76
72
|
/**
|
|
77
73
|
* Given a T, builds a decoder that assumes an array input and returns an
|
package/core/array.js.flow
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
// @flow strict
|
|
2
2
|
|
|
3
|
-
import * as Result from '../result';
|
|
4
3
|
import { annotate } from '../annotate';
|
|
5
4
|
import { compose, predicate } from './composition';
|
|
5
|
+
import { err, ok } from '../result';
|
|
6
6
|
import type { Decoder, DecodeResult } from '../_types';
|
|
7
7
|
|
|
8
8
|
/**
|
|
@@ -11,9 +11,9 @@ import type { Decoder, DecodeResult } from '../_types';
|
|
|
11
11
|
*/
|
|
12
12
|
export const poja: Decoder<Array<mixed>> = (blob: mixed) => {
|
|
13
13
|
if (!Array.isArray(blob)) {
|
|
14
|
-
return
|
|
14
|
+
return err(annotate(blob, 'Must be an array'));
|
|
15
15
|
}
|
|
16
|
-
return
|
|
16
|
+
return ok(
|
|
17
17
|
// NOTE: Since Flow 0.98, Array.isArray() returns $ReadOnlyArray<mixed>
|
|
18
18
|
// instead of Array<mixed>. For rationale, see
|
|
19
19
|
// https://github.com/facebook/flow/issues/7684. In this case, we
|
|
@@ -65,10 +65,10 @@ function all<T>(
|
|
|
65
65
|
// if (index < iterable.length - 1) {
|
|
66
66
|
// errValue.push('...'); // TODO: make special mark, not string!
|
|
67
67
|
// }
|
|
68
|
-
return
|
|
68
|
+
return err(annotate(clone));
|
|
69
69
|
}
|
|
70
70
|
}
|
|
71
|
-
return
|
|
71
|
+
return ok(results);
|
|
72
72
|
}
|
|
73
73
|
|
|
74
74
|
/**
|
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
1
|
+
import { annotate } from '../annotate.mjs';
|
|
2
|
+
import { compose, predicate } from './composition.mjs';
|
|
3
|
+
import { err, ok } from '../result.mjs';
|
|
4
4
|
|
|
5
5
|
/**
|
|
6
6
|
* Like a "Plain Old JavaScript Object", but for arrays: "Plain Old JavaScript
|
|
@@ -8,10 +8,10 @@ import { compose, predicate } from './composition';
|
|
|
8
8
|
*/
|
|
9
9
|
export var poja = function poja(blob) {
|
|
10
10
|
if (!Array.isArray(blob)) {
|
|
11
|
-
return
|
|
11
|
+
return err(annotate(blob, 'Must be an array'));
|
|
12
12
|
}
|
|
13
13
|
|
|
14
|
-
return
|
|
14
|
+
return ok( // NOTE: Since Flow 0.98, Array.isArray() returns $ReadOnlyArray<mixed>
|
|
15
15
|
// instead of Array<mixed>. For rationale, see
|
|
16
16
|
// https://github.com/facebook/flow/issues/7684. In this case, we
|
|
17
17
|
// don't want to output read-only types because it's up to the user of
|
|
@@ -51,11 +51,11 @@ function all(items, blobs) {
|
|
|
51
51
|
// errValue.push('...'); // TODO: make special mark, not string!
|
|
52
52
|
// }
|
|
53
53
|
|
|
54
|
-
return
|
|
54
|
+
return err(annotate(clone));
|
|
55
55
|
}
|
|
56
56
|
}
|
|
57
57
|
|
|
58
|
-
return
|
|
58
|
+
return ok(results);
|
|
59
59
|
}
|
|
60
60
|
/**
|
|
61
61
|
* Given a T, builds a decoder that assumes an array input and returns an
|
package/core/boolean.js
CHANGED
|
@@ -3,23 +3,19 @@
|
|
|
3
3
|
exports.__esModule = true;
|
|
4
4
|
exports.truthy = exports.numericBoolean = exports["boolean"] = void 0;
|
|
5
5
|
|
|
6
|
-
var Result = _interopRequireWildcard(require("../result"));
|
|
7
|
-
|
|
8
6
|
var _annotate = require("../annotate");
|
|
9
7
|
|
|
8
|
+
var _result = require("../result");
|
|
9
|
+
|
|
10
10
|
var _composition = require("./composition");
|
|
11
11
|
|
|
12
12
|
var _number = require("./number");
|
|
13
13
|
|
|
14
|
-
function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
|
|
15
|
-
|
|
16
|
-
function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { "default": obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj["default"] = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
|
|
17
|
-
|
|
18
14
|
/**
|
|
19
15
|
* Decoder that only returns Ok for boolean inputs. Err otherwise.
|
|
20
16
|
*/
|
|
21
17
|
var _boolean = function _boolean(blob) {
|
|
22
|
-
return typeof blob === 'boolean' ?
|
|
18
|
+
return typeof blob === 'boolean' ? (0, _result.ok)(blob) : (0, _result.err)((0, _annotate.annotate)(blob, 'Must be boolean'));
|
|
23
19
|
};
|
|
24
20
|
/**
|
|
25
21
|
* Decoder that returns true for all truthy values, and false otherwise. Never fails.
|
|
@@ -29,7 +25,7 @@ var _boolean = function _boolean(blob) {
|
|
|
29
25
|
exports["boolean"] = _boolean;
|
|
30
26
|
|
|
31
27
|
var truthy = function truthy(blob) {
|
|
32
|
-
return
|
|
28
|
+
return (0, _result.ok)(!!blob);
|
|
33
29
|
};
|
|
34
30
|
/**
|
|
35
31
|
* Decoder that only returns Ok for numeric input values representing booleans.
|
package/core/boolean.js.flow
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
// @flow strict
|
|
2
2
|
|
|
3
|
-
import * as Result from '../result';
|
|
4
3
|
import { annotate } from '../annotate';
|
|
4
|
+
import { err, ok } from '../result';
|
|
5
5
|
import { map } from './composition';
|
|
6
6
|
import { number } from './number';
|
|
7
7
|
import type { Decoder } from '../_types';
|
|
@@ -10,16 +10,14 @@ import type { Decoder } from '../_types';
|
|
|
10
10
|
* Decoder that only returns Ok for boolean inputs. Err otherwise.
|
|
11
11
|
*/
|
|
12
12
|
export const boolean: Decoder<boolean> = (blob: mixed) => {
|
|
13
|
-
return typeof blob === 'boolean'
|
|
14
|
-
? Result.ok(blob)
|
|
15
|
-
: Result.err(annotate(blob, 'Must be boolean'));
|
|
13
|
+
return typeof blob === 'boolean' ? ok(blob) : err(annotate(blob, 'Must be boolean'));
|
|
16
14
|
};
|
|
17
15
|
|
|
18
16
|
/**
|
|
19
17
|
* Decoder that returns true for all truthy values, and false otherwise. Never fails.
|
|
20
18
|
*/
|
|
21
19
|
export const truthy: Decoder<boolean> = (blob: mixed) => {
|
|
22
|
-
return
|
|
20
|
+
return ok(!!blob);
|
|
23
21
|
};
|
|
24
22
|
|
|
25
23
|
/**
|
|
@@ -1,13 +1,13 @@
|
|
|
1
|
-
import
|
|
2
|
-
import {
|
|
3
|
-
import { map } from './composition';
|
|
4
|
-
import { number } from './number';
|
|
1
|
+
import { annotate } from '../annotate.mjs';
|
|
2
|
+
import { err, ok } from '../result.mjs';
|
|
3
|
+
import { map } from './composition.mjs';
|
|
4
|
+
import { number } from './number.mjs';
|
|
5
5
|
|
|
6
6
|
/**
|
|
7
7
|
* Decoder that only returns Ok for boolean inputs. Err otherwise.
|
|
8
8
|
*/
|
|
9
9
|
var _boolean = function _boolean(blob) {
|
|
10
|
-
return typeof blob === 'boolean' ?
|
|
10
|
+
return typeof blob === 'boolean' ? ok(blob) : err(annotate(blob, 'Must be boolean'));
|
|
11
11
|
};
|
|
12
12
|
/**
|
|
13
13
|
* Decoder that returns true for all truthy values, and false otherwise. Never fails.
|
|
@@ -16,7 +16,7 @@ var _boolean = function _boolean(blob) {
|
|
|
16
16
|
|
|
17
17
|
export { _boolean as boolean };
|
|
18
18
|
export var truthy = function truthy(blob) {
|
|
19
|
-
return
|
|
19
|
+
return ok(!!blob);
|
|
20
20
|
};
|
|
21
21
|
/**
|
|
22
22
|
* Decoder that only returns Ok for numeric input values representing booleans.
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { Decoder } from '../_types';
|
|
2
|
+
|
|
3
|
+
export function map<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 extends F, F = unknown>(
|
|
6
|
+
predicate: (value: F) => value is T,
|
|
7
|
+
msg: string,
|
|
8
|
+
): Decoder<T, F>;
|
|
9
|
+
export function predicate<T>(
|
|
10
|
+
predicate: (value: T) => boolean,
|
|
11
|
+
msg: string,
|
|
12
|
+
): Decoder<T, T>;
|
package/core/composition.js
CHANGED
|
@@ -5,14 +5,10 @@ exports.compose = compose;
|
|
|
5
5
|
exports.map = map;
|
|
6
6
|
exports.predicate = predicate;
|
|
7
7
|
|
|
8
|
-
var
|
|
8
|
+
var _result = require("../result");
|
|
9
9
|
|
|
10
10
|
var _annotate = require("../annotate");
|
|
11
11
|
|
|
12
|
-
function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
|
|
13
|
-
|
|
14
|
-
function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { "default": obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj["default"] = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
|
|
15
|
-
|
|
16
12
|
/**
|
|
17
13
|
* Given a decoder T and a mapping function from T's to V's, returns a decoder
|
|
18
14
|
* for V's. This is useful to change the original input data.
|
|
@@ -20,9 +16,9 @@ function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj &&
|
|
|
20
16
|
function map(decoder, mapper) {
|
|
21
17
|
return compose(decoder, function (x) {
|
|
22
18
|
try {
|
|
23
|
-
return
|
|
19
|
+
return (0, _result.ok)(mapper(x));
|
|
24
20
|
} catch (e) {
|
|
25
|
-
return
|
|
21
|
+
return (0, _result.err)((0, _annotate.annotate)(x, e instanceof Error ? e.message : String(e)));
|
|
26
22
|
}
|
|
27
23
|
});
|
|
28
24
|
}
|
|
@@ -40,7 +36,7 @@ function map(decoder, mapper) {
|
|
|
40
36
|
|
|
41
37
|
function compose(decoder, next) {
|
|
42
38
|
return function (blob) {
|
|
43
|
-
return
|
|
39
|
+
return (0, _result.andThen)(decoder(blob), next);
|
|
44
40
|
};
|
|
45
41
|
}
|
|
46
42
|
/**
|
|
@@ -49,8 +45,8 @@ function compose(decoder, next) {
|
|
|
49
45
|
*/
|
|
50
46
|
|
|
51
47
|
|
|
52
|
-
function predicate(
|
|
48
|
+
function predicate(predicateFn, msg) {
|
|
53
49
|
return function (value) {
|
|
54
|
-
return
|
|
50
|
+
return predicateFn(value) ? (0, _result.ok)(value) : (0, _result.err)((0, _annotate.annotate)(value, msg));
|
|
55
51
|
};
|
|
56
52
|
}
|
package/core/composition.js.flow
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
// @flow strict
|
|
2
2
|
|
|
3
|
-
import
|
|
3
|
+
import { andThen, err, ok } from '../result';
|
|
4
4
|
import { annotate } from '../annotate';
|
|
5
5
|
import type { Decoder } from '../_types';
|
|
6
6
|
|
|
@@ -11,9 +11,9 @@ import type { Decoder } from '../_types';
|
|
|
11
11
|
export function map<T, V>(decoder: Decoder<T>, mapper: (T) => V): Decoder<V> {
|
|
12
12
|
return compose(decoder, (x) => {
|
|
13
13
|
try {
|
|
14
|
-
return
|
|
14
|
+
return ok(mapper(x));
|
|
15
15
|
} catch (e) {
|
|
16
|
-
return
|
|
16
|
+
return err(annotate(x, e instanceof Error ? e.message : String(e)));
|
|
17
17
|
}
|
|
18
18
|
});
|
|
19
19
|
}
|
|
@@ -29,15 +29,15 @@ export function map<T, V>(decoder: Decoder<T>, mapper: (T) => V): Decoder<V> {
|
|
|
29
29
|
* argument.
|
|
30
30
|
*/
|
|
31
31
|
export function compose<T, V>(decoder: Decoder<T>, next: Decoder<V, T>): Decoder<V> {
|
|
32
|
-
return (blob: mixed) =>
|
|
32
|
+
return (blob: mixed) => andThen(decoder(blob), next);
|
|
33
33
|
}
|
|
34
34
|
|
|
35
35
|
/**
|
|
36
36
|
* Factory function returning a Decoder<T>, given a predicate function that
|
|
37
37
|
* accepts/rejects the input of type T.
|
|
38
38
|
*/
|
|
39
|
-
export function predicate<T>(
|
|
39
|
+
export function predicate<T>(predicateFn: (T) => boolean, msg: string): Decoder<T, T> {
|
|
40
40
|
return (value: T) => {
|
|
41
|
-
return
|
|
41
|
+
return predicateFn(value) ? ok(value) : err(annotate(value, msg));
|
|
42
42
|
};
|
|
43
43
|
}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import
|
|
2
|
-
import { annotate } from '../annotate';
|
|
1
|
+
import { andThen, err, ok } from '../result.mjs';
|
|
2
|
+
import { annotate } from '../annotate.mjs';
|
|
3
3
|
|
|
4
4
|
/**
|
|
5
5
|
* Given a decoder T and a mapping function from T's to V's, returns a decoder
|
|
@@ -8,9 +8,9 @@ import { annotate } from '../annotate';
|
|
|
8
8
|
export function map(decoder, mapper) {
|
|
9
9
|
return compose(decoder, function (x) {
|
|
10
10
|
try {
|
|
11
|
-
return
|
|
11
|
+
return ok(mapper(x));
|
|
12
12
|
} catch (e) {
|
|
13
|
-
return
|
|
13
|
+
return err(annotate(x, e instanceof Error ? e.message : String(e)));
|
|
14
14
|
}
|
|
15
15
|
});
|
|
16
16
|
}
|
|
@@ -27,7 +27,7 @@ export function map(decoder, mapper) {
|
|
|
27
27
|
|
|
28
28
|
export function compose(decoder, next) {
|
|
29
29
|
return function (blob) {
|
|
30
|
-
return
|
|
30
|
+
return andThen(decoder(blob), next);
|
|
31
31
|
};
|
|
32
32
|
}
|
|
33
33
|
/**
|
|
@@ -35,8 +35,8 @@ export function compose(decoder, next) {
|
|
|
35
35
|
* accepts/rejects the input of type T.
|
|
36
36
|
*/
|
|
37
37
|
|
|
38
|
-
export function predicate(
|
|
38
|
+
export function predicate(predicateFn, msg) {
|
|
39
39
|
return function (value) {
|
|
40
|
-
return
|
|
40
|
+
return predicateFn(value) ? ok(value) : err(annotate(value, msg));
|
|
41
41
|
};
|
|
42
42
|
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { Decoder, Scalar } from '../_types';
|
|
2
|
+
|
|
3
|
+
// Constants
|
|
4
|
+
|
|
5
|
+
export const null_: Decoder<null>;
|
|
6
|
+
export const undefined_: Decoder<undefined>;
|
|
7
|
+
export function constant<T extends Scalar>(value: T): Decoder<T>;
|
|
8
|
+
export function hardcoded<T extends Scalar>(value: T): Decoder<T>;
|
|
9
|
+
export function hardcoded<T>(value: T): Decoder<T>;
|
|
10
|
+
export const mixed: Decoder<unknown>;
|
|
11
|
+
export const unknown: Decoder<unknown>;
|