decoders 2.0.5 → 2.2.0-test
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/README.md +12 -12
- package/dist/annotate-0PUmWHxH.d.cts +33 -0
- package/dist/annotate-0PUmWHxH.d.ts +33 -0
- package/dist/chunk-2C72BP5L.cjs +179 -0
- package/dist/chunk-2C72BP5L.cjs.map +1 -0
- package/dist/chunk-BPSZE2VX.js +13 -0
- package/dist/chunk-BPSZE2VX.js.map +1 -0
- package/dist/chunk-Q3YXBCTD.cjs +13 -0
- package/dist/chunk-Q3YXBCTD.cjs.map +1 -0
- package/dist/chunk-RUMDX66L.js +179 -0
- package/dist/chunk-RUMDX66L.js.map +1 -0
- package/dist/format.cjs +13 -0
- package/dist/format.cjs.map +1 -0
- package/dist/format.d.cts +9 -0
- package/dist/format.d.ts +9 -0
- package/dist/format.js +13 -0
- package/dist/format.js.map +1 -0
- package/dist/index.cjs +659 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +454 -0
- package/dist/index.d.ts +454 -0
- package/dist/index.js +659 -0
- package/dist/index.js.map +1 -0
- package/dist/result.cjs +9 -0
- package/dist/result.cjs.map +1 -0
- package/dist/result.d.cts +26 -0
- package/dist/result.d.ts +26 -0
- package/dist/result.js +9 -0
- package/dist/result.js.map +1 -0
- package/format.cjs +13 -0
- package/package.json +163 -100
- package/result.cjs +9 -0
- package/CHANGELOG.md +0 -665
- package/Decoder.d.ts +0 -94
- package/Decoder.js +0 -105
- package/Decoder.js.flow +0 -286
- package/Decoder.mjs +0 -101
- package/NotSupportedTSVersion.d.ts +0 -1
- package/_utils.d.ts +0 -9
- package/_utils.js +0 -80
- package/_utils.js.flow +0 -107
- package/_utils.mjs +0 -70
- package/annotate.d.ts +0 -62
- package/annotate.js +0 -145
- package/annotate.js.flow +0 -218
- package/annotate.mjs +0 -131
- package/format.d.ts +0 -6
- package/format.js +0 -117
- package/format.js.flow +0 -128
- package/format.mjs +0 -110
- package/index.d.ts +0 -40
- package/index.js +0 -67
- package/index.js.flow +0 -44
- package/index.mjs +0 -11
- package/lib/_helpers.d.ts +0 -79
- package/lib/arrays.d.ts +0 -59
- package/lib/arrays.js +0 -85
- package/lib/arrays.js.flow +0 -138
- package/lib/arrays.mjs +0 -75
- package/lib/basics.d.ts +0 -93
- package/lib/basics.js +0 -74
- package/lib/basics.js.flow +0 -124
- package/lib/basics.mjs +0 -60
- package/lib/booleans.d.ts +0 -16
- package/lib/booleans.js +0 -21
- package/lib/booleans.js.flow +0 -22
- package/lib/booleans.mjs +0 -15
- package/lib/dates.d.ts +0 -15
- package/lib/dates.js +0 -23
- package/lib/dates.js.flow +0 -40
- package/lib/dates.mjs +0 -17
- package/lib/json.d.ts +0 -35
- package/lib/json.js +0 -25
- package/lib/json.js.flow +0 -50
- package/lib/json.mjs +0 -18
- package/lib/numbers.d.ts +0 -31
- package/lib/numbers.js +0 -34
- package/lib/numbers.js.flow +0 -46
- package/lib/numbers.mjs +0 -25
- package/lib/objects.d.ts +0 -76
- package/lib/objects.js +0 -138
- package/lib/objects.js.flow +0 -238
- package/lib/objects.mjs +0 -128
- package/lib/strings.d.ts +0 -56
- package/lib/strings.js +0 -54
- package/lib/strings.js.flow +0 -90
- package/lib/strings.mjs +0 -40
- package/lib/unions.d.ts +0 -55
- package/lib/unions.js +0 -82
- package/lib/unions.js.flow +0 -155
- package/lib/unions.mjs +0 -75
- package/lib/utilities.d.ts +0 -40
- package/lib/utilities.js +0 -44
- package/lib/utilities.js.flow +0 -65
- package/lib/utilities.mjs +0 -35
- package/result.d.ts +0 -16
- package/result.js +0 -21
- package/result.js.flow +0 -26
- package/result.mjs +0 -15
package/lib/_helpers.d.ts
DELETED
|
@@ -1,79 +0,0 @@
|
|
|
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/lib/arrays.d.ts
DELETED
|
@@ -1,59 +0,0 @@
|
|
|
1
|
-
/// <reference lib="es6" />
|
|
2
|
-
|
|
3
|
-
import { Decoder } from '../Decoder';
|
|
4
|
-
|
|
5
|
-
/**
|
|
6
|
-
* Accepts any array, but doesn't validate its items further.
|
|
7
|
-
*
|
|
8
|
-
* "poja" means "plain old JavaScript array", a play on `pojo()`.
|
|
9
|
-
*/
|
|
10
|
-
export const poja: Decoder<unknown[]>;
|
|
11
|
-
|
|
12
|
-
/**
|
|
13
|
-
* Accepts arrays of whatever the given decoder accepts.
|
|
14
|
-
*/
|
|
15
|
-
export function array<T>(decoder: Decoder<T>): Decoder<T[]>;
|
|
16
|
-
|
|
17
|
-
/**
|
|
18
|
-
* Like `array()`, but will reject arrays with 0 elements.
|
|
19
|
-
*/
|
|
20
|
-
export function nonEmptyArray<T>(decoder: Decoder<T>): Decoder<[T, ...T[]]>;
|
|
21
|
-
|
|
22
|
-
/**
|
|
23
|
-
* Similar to `array()`, but returns the result as an [ES6
|
|
24
|
-
* Set](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Set).
|
|
25
|
-
*/
|
|
26
|
-
export function set<T>(decoder: Decoder<T>): Decoder<Set<T>>;
|
|
27
|
-
|
|
28
|
-
/**
|
|
29
|
-
* Accepts a tuple (an array with exactly _n_ items) of values accepted by the
|
|
30
|
-
* _n_ given decoders.
|
|
31
|
-
*/
|
|
32
|
-
export function tuple<A>(a: Decoder<A>): Decoder<[A]>;
|
|
33
|
-
export function tuple<A, B>(a: Decoder<A>, b: Decoder<B>): Decoder<[A, B]>;
|
|
34
|
-
export function tuple<A, B, C>(
|
|
35
|
-
a: Decoder<A>,
|
|
36
|
-
b: Decoder<B>,
|
|
37
|
-
c: Decoder<C>,
|
|
38
|
-
): Decoder<[A, B, C]>;
|
|
39
|
-
export function tuple<A, B, C, D>(
|
|
40
|
-
a: Decoder<A>,
|
|
41
|
-
b: Decoder<B>,
|
|
42
|
-
c: Decoder<C>,
|
|
43
|
-
d: Decoder<D>,
|
|
44
|
-
): Decoder<[A, B, C, D]>;
|
|
45
|
-
export function tuple<A, B, C, D, E>(
|
|
46
|
-
a: Decoder<A>,
|
|
47
|
-
b: Decoder<B>,
|
|
48
|
-
c: Decoder<C>,
|
|
49
|
-
d: Decoder<D>,
|
|
50
|
-
e: Decoder<E>,
|
|
51
|
-
): Decoder<[A, B, C, D, E]>;
|
|
52
|
-
export function tuple<A, B, C, D, E, F>(
|
|
53
|
-
a: Decoder<A>,
|
|
54
|
-
b: Decoder<B>,
|
|
55
|
-
c: Decoder<C>,
|
|
56
|
-
d: Decoder<D>,
|
|
57
|
-
e: Decoder<E>,
|
|
58
|
-
f: Decoder<F>,
|
|
59
|
-
): Decoder<[A, B, C, D, E, F]>;
|
package/lib/arrays.js
DELETED
|
@@ -1,85 +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
|
-
exports.tuple = void 0
|
|
9
|
-
var _annotate = require('../annotate')
|
|
10
|
-
var _Decoder = require('../Decoder')
|
|
11
|
-
|
|
12
|
-
var poja = (0, _Decoder.define)(function (blob, ok, err) {
|
|
13
|
-
if (!Array.isArray(blob)) {
|
|
14
|
-
return err('Must be an array')
|
|
15
|
-
}
|
|
16
|
-
return ok(blob.slice())
|
|
17
|
-
})
|
|
18
|
-
|
|
19
|
-
exports.poja = poja
|
|
20
|
-
function all(items, blobs, ok, err) {
|
|
21
|
-
var results = []
|
|
22
|
-
for (var index = 0; index < items.length; ++index) {
|
|
23
|
-
var result = items[index]
|
|
24
|
-
if (result.ok) {
|
|
25
|
-
results.push(result.value)
|
|
26
|
-
} else {
|
|
27
|
-
var ann = result.error
|
|
28
|
-
var clone = [].concat(blobs)
|
|
29
|
-
clone.splice(index, 1, (0, _annotate.annotate)(ann, ann.text ? ann.text + ' (at index ' + index + ')' : 'index ' + index))
|
|
30
|
-
return err((0, _annotate.annotate)(clone))
|
|
31
|
-
}
|
|
32
|
-
}
|
|
33
|
-
return ok(results)
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
function array(decoder) {
|
|
37
|
-
return poja.then(function (blobs, ok, err) {
|
|
38
|
-
var results = blobs.map(decoder.decode)
|
|
39
|
-
return all(results, blobs, ok, err)
|
|
40
|
-
})
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
function nonEmptyArray(decoder) {
|
|
44
|
-
return array(decoder).refine(function (arr) {
|
|
45
|
-
return arr.length > 0
|
|
46
|
-
}, 'Must be non-empty array')
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
function set(decoder) {
|
|
50
|
-
return array(decoder).transform(function (items) {
|
|
51
|
-
return new Set(items)
|
|
52
|
-
})
|
|
53
|
-
}
|
|
54
|
-
var ntuple = function ntuple(n) {
|
|
55
|
-
return poja.refine(function (arr) {
|
|
56
|
-
return arr.length === n
|
|
57
|
-
}, 'Must be a ' + n + '-tuple')
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
function _tuple() {
|
|
61
|
-
for (var _len = arguments.length, decoders = new Array(_len), _key = 0; _key < _len; _key++) {
|
|
62
|
-
decoders[_key] = arguments[_key]
|
|
63
|
-
}
|
|
64
|
-
return ntuple(decoders.length).then(function (blobs, ok, err) {
|
|
65
|
-
var allOk = true
|
|
66
|
-
var rvs = decoders.map(function (decoder, i) {
|
|
67
|
-
var blob = blobs[i]
|
|
68
|
-
var result = decoder.decode(blob)
|
|
69
|
-
if (result.ok) {
|
|
70
|
-
return result.value
|
|
71
|
-
} else {
|
|
72
|
-
allOk = false
|
|
73
|
-
return result.error
|
|
74
|
-
}
|
|
75
|
-
})
|
|
76
|
-
if (allOk) {
|
|
77
|
-
return ok(rvs)
|
|
78
|
-
} else {
|
|
79
|
-
return err((0, _annotate.annotate)(rvs))
|
|
80
|
-
}
|
|
81
|
-
})
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
var tuple = _tuple
|
|
85
|
-
exports.tuple = tuple
|
package/lib/arrays.js.flow
DELETED
|
@@ -1,138 +0,0 @@
|
|
|
1
|
-
// @flow strict
|
|
2
|
-
|
|
3
|
-
import { annotate } from '../annotate';
|
|
4
|
-
import { define } from '../Decoder';
|
|
5
|
-
import type { _Any } from '../_utils';
|
|
6
|
-
import type { Annotation } from '../annotate';
|
|
7
|
-
import type { Decoder, DecodeResult } from '../Decoder';
|
|
8
|
-
|
|
9
|
-
/**
|
|
10
|
-
* Accepts any array, but doesn't validate its items further.
|
|
11
|
-
*
|
|
12
|
-
* "poja" means "plain old JavaScript array", a play on `pojo()`.
|
|
13
|
-
*/
|
|
14
|
-
export const poja: Decoder<Array<mixed>> = define((blob, ok, err) => {
|
|
15
|
-
if (!Array.isArray(blob)) {
|
|
16
|
-
return err('Must be an array');
|
|
17
|
-
}
|
|
18
|
-
return ok(
|
|
19
|
-
// NOTE: Since Flow 0.98, Array.isArray() returns $ReadOnlyArray<mixed>
|
|
20
|
-
// instead of Array<mixed>. For rationale, see
|
|
21
|
-
// https://github.com/facebook/flow/issues/7684. In this case, we
|
|
22
|
-
// don't want to output read-only types because it's up to the user of
|
|
23
|
-
// decoders to determine what they want to do with the decoded output.
|
|
24
|
-
// If they want to write items into the array, that's fine!
|
|
25
|
-
// The fastest way to turn a read-only array into a normal array in
|
|
26
|
-
// Javascript is to use .slice() on it, see this benchmark:
|
|
27
|
-
// http://jsben.ch/lO6C5
|
|
28
|
-
blob.slice(),
|
|
29
|
-
);
|
|
30
|
-
});
|
|
31
|
-
|
|
32
|
-
/**
|
|
33
|
-
* Given an array of Result instances, loop over them all and return:
|
|
34
|
-
* - An [index, err] tuple, indicating the (index of the) first Err instance
|
|
35
|
-
* encountered; or
|
|
36
|
-
* - a new Ok with an array of all unwrapped Ok'ed values
|
|
37
|
-
*/
|
|
38
|
-
function all<T>(
|
|
39
|
-
items: $ReadOnlyArray<DecodeResult<T>>,
|
|
40
|
-
blobs: $ReadOnlyArray<mixed>,
|
|
41
|
-
|
|
42
|
-
// TODO: Make this less ugly
|
|
43
|
-
ok: (Array<T>) => DecodeResult<Array<T>>,
|
|
44
|
-
err: (Annotation) => DecodeResult<Array<T>>,
|
|
45
|
-
): DecodeResult<Array<T>> {
|
|
46
|
-
const results: Array<T> = [];
|
|
47
|
-
for (let index = 0; index < items.length; ++index) {
|
|
48
|
-
const result = items[index];
|
|
49
|
-
if (result.ok) {
|
|
50
|
-
results.push(result.value);
|
|
51
|
-
} else {
|
|
52
|
-
const ann = result.error;
|
|
53
|
-
|
|
54
|
-
// Rewrite the annotation to include the index information, and inject it into the original blob
|
|
55
|
-
const clone = [...blobs];
|
|
56
|
-
clone.splice(
|
|
57
|
-
index,
|
|
58
|
-
1,
|
|
59
|
-
annotate(
|
|
60
|
-
ann,
|
|
61
|
-
ann.text ? `${ann.text} (at index ${index})` : `index ${index}`,
|
|
62
|
-
),
|
|
63
|
-
);
|
|
64
|
-
|
|
65
|
-
return err(annotate(clone));
|
|
66
|
-
}
|
|
67
|
-
}
|
|
68
|
-
return ok(results);
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
/**
|
|
72
|
-
* Accepts arrays of whatever the given decoder accepts.
|
|
73
|
-
*/
|
|
74
|
-
export function array<T>(decoder: Decoder<T>): Decoder<Array<T>> {
|
|
75
|
-
return poja.then((blobs: $ReadOnlyArray<mixed>, ok, err) => {
|
|
76
|
-
const results = blobs.map(decoder.decode);
|
|
77
|
-
return all(results, blobs, ok, err);
|
|
78
|
-
});
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
/**
|
|
82
|
-
* Like `array()`, but will reject arrays with 0 elements.
|
|
83
|
-
*/
|
|
84
|
-
export function nonEmptyArray<T>(decoder: Decoder<T>): Decoder<Array<T>> {
|
|
85
|
-
return array(decoder).refine((arr) => arr.length > 0, 'Must be non-empty array');
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
/**
|
|
89
|
-
* Similar to `array()`, but returns the result as an [ES6
|
|
90
|
-
* Set](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Set).
|
|
91
|
-
*/
|
|
92
|
-
export function set<T>(decoder: Decoder<T>): Decoder<Set<T>> {
|
|
93
|
-
return array(decoder).transform((items) => new Set(items));
|
|
94
|
-
}
|
|
95
|
-
|
|
96
|
-
const ntuple = (n: number) =>
|
|
97
|
-
poja.refine((arr) => arr.length === n, `Must be a ${n}-tuple`);
|
|
98
|
-
|
|
99
|
-
// prettier-ignore
|
|
100
|
-
interface TupleT {
|
|
101
|
-
<A>(a: Decoder<A>): Decoder<[A]>;
|
|
102
|
-
<A, B>(a: Decoder<A>, b: Decoder<B>): Decoder<[A, B]>;
|
|
103
|
-
<A, B, C>(a: Decoder<A>, b: Decoder<B>, c: Decoder<C>): Decoder<[A, B, C]>;
|
|
104
|
-
<A, B, C, D>(a: Decoder<A>, b: Decoder<B>, c: Decoder<C>, d: Decoder<D>): Decoder<[A, B, C, D]>;
|
|
105
|
-
<A, B, C, D, E>(a: Decoder<A>, b: Decoder<B>, c: Decoder<C>, d: Decoder<D>, e: Decoder<E>): Decoder<[A, B, C, D, E]>;
|
|
106
|
-
<A, B, C, D, E, F>(a: Decoder<A>, b: Decoder<B>, c: Decoder<C>, d: Decoder<D>, e: Decoder<E>, f: Decoder<F>): Decoder<[A, B, C, D, E, F]>;
|
|
107
|
-
}
|
|
108
|
-
|
|
109
|
-
function _tuple(...decoders: $ReadOnlyArray<Decoder<mixed>>): Decoder<Array<mixed>> {
|
|
110
|
-
return ntuple(decoders.length).then((blobs, ok, err) => {
|
|
111
|
-
let allOk = true;
|
|
112
|
-
|
|
113
|
-
const rvs = decoders.map((decoder, i) => {
|
|
114
|
-
const blob = blobs[i];
|
|
115
|
-
const result = decoder.decode(blob);
|
|
116
|
-
if (result.ok) {
|
|
117
|
-
return result.value;
|
|
118
|
-
} else {
|
|
119
|
-
allOk = false;
|
|
120
|
-
return result.error;
|
|
121
|
-
}
|
|
122
|
-
});
|
|
123
|
-
|
|
124
|
-
if (allOk) {
|
|
125
|
-
return ok(rvs);
|
|
126
|
-
} else {
|
|
127
|
-
// If a decoder error has happened while unwrapping all the
|
|
128
|
-
// results, try to construct a good error message
|
|
129
|
-
return err(annotate(rvs));
|
|
130
|
-
}
|
|
131
|
-
});
|
|
132
|
-
}
|
|
133
|
-
|
|
134
|
-
/**
|
|
135
|
-
* Accepts a tuple (an array with exactly _n_ items) of values accepted by the
|
|
136
|
-
* _n_ given decoders.
|
|
137
|
-
*/
|
|
138
|
-
export const tuple: TupleT = (_tuple: _Any);
|
package/lib/arrays.mjs
DELETED
|
@@ -1,75 +0,0 @@
|
|
|
1
|
-
import { annotate } from '../annotate.mjs'
|
|
2
|
-
import { define } from '../Decoder.mjs'
|
|
3
|
-
|
|
4
|
-
export var poja = define(function (blob, ok, err) {
|
|
5
|
-
if (!Array.isArray(blob)) {
|
|
6
|
-
return err('Must be an array')
|
|
7
|
-
}
|
|
8
|
-
return ok(blob.slice())
|
|
9
|
-
})
|
|
10
|
-
|
|
11
|
-
function all(items, blobs, ok, err) {
|
|
12
|
-
var results = []
|
|
13
|
-
for (var index = 0; index < items.length; ++index) {
|
|
14
|
-
var result = items[index]
|
|
15
|
-
if (result.ok) {
|
|
16
|
-
results.push(result.value)
|
|
17
|
-
} else {
|
|
18
|
-
var ann = result.error
|
|
19
|
-
var clone = [].concat(blobs)
|
|
20
|
-
clone.splice(index, 1, annotate(ann, ann.text ? ann.text + ' (at index ' + index + ')' : 'index ' + index))
|
|
21
|
-
return err(annotate(clone))
|
|
22
|
-
}
|
|
23
|
-
}
|
|
24
|
-
return ok(results)
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
export function array(decoder) {
|
|
28
|
-
return poja.then(function (blobs, ok, err) {
|
|
29
|
-
var results = blobs.map(decoder.decode)
|
|
30
|
-
return all(results, blobs, ok, err)
|
|
31
|
-
})
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
export function nonEmptyArray(decoder) {
|
|
35
|
-
return array(decoder).refine(function (arr) {
|
|
36
|
-
return arr.length > 0
|
|
37
|
-
}, 'Must be non-empty array')
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
export function set(decoder) {
|
|
41
|
-
return array(decoder).transform(function (items) {
|
|
42
|
-
return new Set(items)
|
|
43
|
-
})
|
|
44
|
-
}
|
|
45
|
-
var ntuple = function ntuple(n) {
|
|
46
|
-
return poja.refine(function (arr) {
|
|
47
|
-
return arr.length === n
|
|
48
|
-
}, 'Must be a ' + n + '-tuple')
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
function _tuple() {
|
|
52
|
-
for (var _len = arguments.length, decoders = new Array(_len), _key = 0; _key < _len; _key++) {
|
|
53
|
-
decoders[_key] = arguments[_key]
|
|
54
|
-
}
|
|
55
|
-
return ntuple(decoders.length).then(function (blobs, ok, err) {
|
|
56
|
-
var allOk = true
|
|
57
|
-
var rvs = decoders.map(function (decoder, i) {
|
|
58
|
-
var blob = blobs[i]
|
|
59
|
-
var result = decoder.decode(blob)
|
|
60
|
-
if (result.ok) {
|
|
61
|
-
return result.value
|
|
62
|
-
} else {
|
|
63
|
-
allOk = false
|
|
64
|
-
return result.error
|
|
65
|
-
}
|
|
66
|
-
})
|
|
67
|
-
if (allOk) {
|
|
68
|
-
return ok(rvs)
|
|
69
|
-
} else {
|
|
70
|
-
return err(annotate(rvs))
|
|
71
|
-
}
|
|
72
|
-
})
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
export var tuple = _tuple
|
package/lib/basics.d.ts
DELETED
|
@@ -1,93 +0,0 @@
|
|
|
1
|
-
import { Decoder, Scalar } from '../Decoder';
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* Accepts and returns only the literal `null` value.
|
|
5
|
-
*/
|
|
6
|
-
export const null_: Decoder<null>;
|
|
7
|
-
|
|
8
|
-
/**
|
|
9
|
-
* Accepts and returns only the literal `undefined` value.
|
|
10
|
-
*/
|
|
11
|
-
export const undefined_: Decoder<undefined>;
|
|
12
|
-
|
|
13
|
-
/**
|
|
14
|
-
* Accepts whatever the given decoder accepts, or `undefined`.
|
|
15
|
-
*
|
|
16
|
-
* If a default value is explicitly provided, return that instead in the
|
|
17
|
-
* `undefined` case.
|
|
18
|
-
*/
|
|
19
|
-
export function optional<T>(decoder: Decoder<T>): Decoder<T | undefined>;
|
|
20
|
-
export function optional<T, V extends Scalar>(
|
|
21
|
-
decoder: Decoder<T>,
|
|
22
|
-
defaultValue: (() => V) | V,
|
|
23
|
-
): Decoder<NonNullable<T> | V>;
|
|
24
|
-
export function optional<T, V>(
|
|
25
|
-
decoder: Decoder<T>,
|
|
26
|
-
defaultValue: (() => V) | V,
|
|
27
|
-
): Decoder<NonNullable<T> | V>;
|
|
28
|
-
|
|
29
|
-
/**
|
|
30
|
-
* Accepts whatever the given decoder accepts, or `null`.
|
|
31
|
-
*
|
|
32
|
-
* If a default value is explicitly provided, return that instead in the `null`
|
|
33
|
-
* case.
|
|
34
|
-
*/
|
|
35
|
-
export function nullable<T>(decoder: Decoder<T>): Decoder<T | null>;
|
|
36
|
-
export function nullable<T, V extends Scalar>(
|
|
37
|
-
decoder: Decoder<T>,
|
|
38
|
-
defaultValue: (() => V) | V,
|
|
39
|
-
): Decoder<NonNullable<T> | V>;
|
|
40
|
-
export function nullable<T, V>(
|
|
41
|
-
decoder: Decoder<T>,
|
|
42
|
-
defaultValue: (() => V) | V,
|
|
43
|
-
): Decoder<NonNullable<T> | V>;
|
|
44
|
-
|
|
45
|
-
/**
|
|
46
|
-
* Accepts whatever the given decoder accepts, or `null`, or `undefined`.
|
|
47
|
-
*
|
|
48
|
-
* If a default value is explicitly provided, return that instead in the
|
|
49
|
-
* `null`/`undefined` case.
|
|
50
|
-
*/
|
|
51
|
-
export function maybe<T>(decoder: Decoder<T>): Decoder<T | null | undefined>;
|
|
52
|
-
export function maybe<T, V extends Scalar>(
|
|
53
|
-
decoder: Decoder<T>,
|
|
54
|
-
defaultValue: (() => V) | V,
|
|
55
|
-
): Decoder<NonNullable<T> | V>;
|
|
56
|
-
export function maybe<T, V>(
|
|
57
|
-
decoder: Decoder<T>,
|
|
58
|
-
defaultValue: (() => V) | V,
|
|
59
|
-
): Decoder<NonNullable<T> | V>;
|
|
60
|
-
|
|
61
|
-
/**
|
|
62
|
-
* Accepts only the given constant value.
|
|
63
|
-
*/
|
|
64
|
-
export function constant<T extends Scalar>(value: T): Decoder<T>;
|
|
65
|
-
|
|
66
|
-
/**
|
|
67
|
-
* Accepts anything, completely ignores it, and always returns the provided
|
|
68
|
-
* value instead.
|
|
69
|
-
*
|
|
70
|
-
* This is useful to manually add extra fields to object decoders.
|
|
71
|
-
*/
|
|
72
|
-
export function always<T extends Scalar>(value: T): Decoder<T>;
|
|
73
|
-
export function always<T>(value: (() => T) | T): Decoder<T>;
|
|
74
|
-
|
|
75
|
-
/**
|
|
76
|
-
* Alias of always.
|
|
77
|
-
*/
|
|
78
|
-
export function hardcoded<T extends Scalar>(value: T): Decoder<T>;
|
|
79
|
-
export function hardcoded<T>(value: (() => T) | T): Decoder<T>;
|
|
80
|
-
|
|
81
|
-
/**
|
|
82
|
-
* Accepts anything and returns it unchanged.
|
|
83
|
-
*
|
|
84
|
-
* Useful for situation in which you don't know or expect a specific type. Of
|
|
85
|
-
* course, the downside is that you won't know the type of the value statically
|
|
86
|
-
* and you'll have to further refine it yourself.
|
|
87
|
-
*/
|
|
88
|
-
export const unknown: Decoder<unknown>;
|
|
89
|
-
|
|
90
|
-
/**
|
|
91
|
-
* Alias of unknown.
|
|
92
|
-
*/
|
|
93
|
-
export const mixed: Decoder<unknown>;
|
package/lib/basics.js
DELETED
|
@@ -1,74 +0,0 @@
|
|
|
1
|
-
'use strict'
|
|
2
|
-
|
|
3
|
-
exports.__esModule = true
|
|
4
|
-
exports.always = always
|
|
5
|
-
exports.constant = constant
|
|
6
|
-
exports.unknown = exports.undefined_ = exports.optional = exports.nullable = exports.null_ = exports.mixed = exports.maybe = exports.hardcoded = void 0
|
|
7
|
-
var _Decoder = require('../Decoder')
|
|
8
|
-
var _unions = require('./unions')
|
|
9
|
-
|
|
10
|
-
var null_ = (0, _Decoder.define)(function (blob, ok, err) {
|
|
11
|
-
return blob === null ? ok(blob) : err('Must be null')
|
|
12
|
-
})
|
|
13
|
-
|
|
14
|
-
exports.null_ = null_
|
|
15
|
-
var undefined_ = (0, _Decoder.define)(function (blob, ok, err) {
|
|
16
|
-
return blob === undefined ? ok(blob) : err('Must be undefined')
|
|
17
|
-
})
|
|
18
|
-
exports.undefined_ = undefined_
|
|
19
|
-
var undefined_or_null = (0, _Decoder.define)(function (blob, ok, err) {
|
|
20
|
-
return blob === undefined || blob === null ? ok(blob) : err('Must be undefined or null')
|
|
21
|
-
})
|
|
22
|
-
function _maybeish(emptyCase) {
|
|
23
|
-
function _inner(decoder) {
|
|
24
|
-
var rv = (0, _unions.either)(emptyCase, decoder)
|
|
25
|
-
if (arguments.length >= 2) {
|
|
26
|
-
var _defaultValue = arguments[1]
|
|
27
|
-
var _defaultValue2 = typeof _defaultValue === 'function' ? _defaultValue() : _defaultValue
|
|
28
|
-
return rv.transform(function (value) {
|
|
29
|
-
return value != null ? value : _defaultValue2
|
|
30
|
-
})
|
|
31
|
-
} else {
|
|
32
|
-
return rv
|
|
33
|
-
}
|
|
34
|
-
}
|
|
35
|
-
return _inner
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
var nullable = _maybeish(null_)
|
|
39
|
-
|
|
40
|
-
exports.nullable = nullable
|
|
41
|
-
var optional = _maybeish(undefined_)
|
|
42
|
-
|
|
43
|
-
exports.optional = optional
|
|
44
|
-
var maybe = _maybeish(undefined_or_null)
|
|
45
|
-
|
|
46
|
-
exports.maybe = maybe
|
|
47
|
-
function constant(value) {
|
|
48
|
-
return (0, _Decoder.define)(function (blob, ok, err) {
|
|
49
|
-
return blob === value ? ok(value) : err('Must be constant ' + String(value))
|
|
50
|
-
})
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
function always(value) {
|
|
54
|
-
return (0, _Decoder.define)(
|
|
55
|
-
typeof value === 'function'
|
|
56
|
-
? function (blob, ok, _) {
|
|
57
|
-
return ok(value())
|
|
58
|
-
}
|
|
59
|
-
: function (blob, ok, _) {
|
|
60
|
-
return ok(value)
|
|
61
|
-
}
|
|
62
|
-
)
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
var hardcoded = always
|
|
66
|
-
|
|
67
|
-
exports.hardcoded = hardcoded
|
|
68
|
-
var unknown = (0, _Decoder.define)(function (blob, ok, _) {
|
|
69
|
-
return ok(blob)
|
|
70
|
-
})
|
|
71
|
-
|
|
72
|
-
exports.unknown = unknown
|
|
73
|
-
var mixed = unknown
|
|
74
|
-
exports.mixed = mixed
|