decoders 2.1.0 → 2.2.0-test2
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/annotate-0PUmWHxH.d.ts +33 -0
- package/chunk-BPSZE2VX.js +13 -0
- package/chunk-RUMDX66L.js +179 -0
- package/dist/annotate-0PUmWHxH.d.mts +33 -0
- package/dist/annotate-0PUmWHxH.d.ts +33 -0
- package/dist/chunk-BPSZE2VX.js +13 -0
- package/dist/chunk-BPSZE2VX.js.map +1 -0
- package/dist/chunk-HBFFQIIN.mjs +13 -0
- package/dist/chunk-HBFFQIIN.mjs.map +1 -0
- package/dist/chunk-RUMDX66L.js +179 -0
- package/dist/chunk-RUMDX66L.js.map +1 -0
- package/dist/chunk-ZTKFAKRL.mjs +179 -0
- package/dist/chunk-ZTKFAKRL.mjs.map +1 -0
- package/dist/format.d.mts +9 -0
- package/dist/format.d.ts +9 -0
- package/dist/format.js +13 -0
- package/dist/format.js.map +1 -0
- package/dist/format.mjs +13 -0
- package/dist/format.mjs.map +1 -0
- package/dist/index.d.mts +454 -0
- package/dist/index.d.ts +454 -0
- package/dist/index.js +659 -0
- package/dist/index.js.map +1 -0
- package/dist/index.mjs +659 -0
- package/dist/index.mjs.map +1 -0
- package/dist/result.d.mts +26 -0
- package/dist/result.d.ts +26 -0
- package/dist/result.js +9 -0
- package/dist/result.js.map +1 -0
- package/dist/result.mjs +9 -0
- package/dist/result.mjs.map +1 -0
- package/format.d.ts +7 -4
- package/format.js +12 -116
- package/index.d.ts +454 -40
- package/index.js +659 -67
- package/package.json +163 -100
- package/result.d.ts +25 -15
- package/result.js +6 -18
- package/CHANGELOG.md +0 -670
- 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.js.flow +0 -128
- package/format.mjs +0 -110
- 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 -54
- 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.js.flow +0 -26
- package/result.mjs +0 -15
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
|
package/lib/basics.js.flow
DELETED
|
@@ -1,124 +0,0 @@
|
|
|
1
|
-
// @flow strict
|
|
2
|
-
|
|
3
|
-
import { define } from '../Decoder';
|
|
4
|
-
import { either } from './unions';
|
|
5
|
-
import type { _Any } from '../_utils';
|
|
6
|
-
import type { Decoder, Scalar } from '../Decoder';
|
|
7
|
-
|
|
8
|
-
/**
|
|
9
|
-
* Accepts and returns only the literal `null` value.
|
|
10
|
-
*/
|
|
11
|
-
export const null_: Decoder<null> = define((blob, ok, err) =>
|
|
12
|
-
blob === null ? ok(blob) : err('Must be null'),
|
|
13
|
-
);
|
|
14
|
-
|
|
15
|
-
/**
|
|
16
|
-
* Accepts and returns only the literal `undefined` value.
|
|
17
|
-
*/
|
|
18
|
-
export const undefined_: Decoder<void> = define((blob, ok, err) =>
|
|
19
|
-
blob === undefined ? ok(blob) : err('Must be undefined'),
|
|
20
|
-
);
|
|
21
|
-
|
|
22
|
-
const undefined_or_null: Decoder<null | void> = define((blob, ok, err) =>
|
|
23
|
-
blob === undefined || blob === null
|
|
24
|
-
? ok(blob)
|
|
25
|
-
: // Combine error message into a single line for readability
|
|
26
|
-
err('Must be undefined or null'),
|
|
27
|
-
);
|
|
28
|
-
|
|
29
|
-
interface Maybeish<E> {
|
|
30
|
-
<T>(decoder: Decoder<T>): Decoder<E | T>;
|
|
31
|
-
<T, V>(
|
|
32
|
-
decoder: Decoder<T>,
|
|
33
|
-
defaultValue: (() => V) | V,
|
|
34
|
-
): Decoder<$NonMaybeType<T> | V>;
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
function _maybeish<E>(emptyCase: Decoder<E>): Maybeish<E> {
|
|
38
|
-
function _inner(decoder /* defaultValue */) {
|
|
39
|
-
const rv = either(emptyCase, decoder);
|
|
40
|
-
if (
|
|
41
|
-
// If a default value is provided...
|
|
42
|
-
arguments.length >= 2
|
|
43
|
-
) {
|
|
44
|
-
// ...then return the default value
|
|
45
|
-
const _defaultValue = arguments[1];
|
|
46
|
-
const defaultValue =
|
|
47
|
-
typeof _defaultValue === 'function' ? _defaultValue() : _defaultValue;
|
|
48
|
-
return rv.transform((value) => value ?? defaultValue);
|
|
49
|
-
} else {
|
|
50
|
-
// Otherwise the "normal" empty case
|
|
51
|
-
return rv;
|
|
52
|
-
}
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
return (_inner: _Any);
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
/**
|
|
59
|
-
* Accepts whatever the given decoder accepts, or `null`.
|
|
60
|
-
*
|
|
61
|
-
* If a default value is explicitly provided, return that instead in the `null`
|
|
62
|
-
* case.
|
|
63
|
-
*/
|
|
64
|
-
export const nullable: Maybeish<null> = _maybeish(null_);
|
|
65
|
-
|
|
66
|
-
/**
|
|
67
|
-
* Accepts whatever the given decoder accepts, or `undefined`.
|
|
68
|
-
*
|
|
69
|
-
* If a default value is explicitly provided, return that instead in the
|
|
70
|
-
* `undefined` case.
|
|
71
|
-
*/
|
|
72
|
-
export const optional: Maybeish<void> = _maybeish(undefined_);
|
|
73
|
-
|
|
74
|
-
/**
|
|
75
|
-
* Accepts whatever the given decoder accepts, or `null`, or `undefined`.
|
|
76
|
-
*
|
|
77
|
-
* If a default value is explicitly provided, return that instead in the
|
|
78
|
-
* `null`/`undefined` case.
|
|
79
|
-
*/
|
|
80
|
-
export const maybe: Maybeish<null | void> = _maybeish(undefined_or_null);
|
|
81
|
-
|
|
82
|
-
/**
|
|
83
|
-
* Accepts only the given constant value.
|
|
84
|
-
*/
|
|
85
|
-
export function constant<T: Scalar>(value: T): Decoder<T> {
|
|
86
|
-
return define((blob, ok, err) =>
|
|
87
|
-
blob === value ? ok(value) : err(`Must be constant ${String(value)}`),
|
|
88
|
-
);
|
|
89
|
-
}
|
|
90
|
-
|
|
91
|
-
/**
|
|
92
|
-
* Accepts anything, completely ignores it, and always returns the provided
|
|
93
|
-
* value instead.
|
|
94
|
-
*
|
|
95
|
-
* This is useful to manually add extra fields to object decoders.
|
|
96
|
-
*/
|
|
97
|
-
export function always<T>(value: (() => T) | T): Decoder<T> {
|
|
98
|
-
return define(
|
|
99
|
-
typeof value === 'function'
|
|
100
|
-
? (blob /* ignored */, ok, _) =>
|
|
101
|
-
// $FlowFixMe[incompatible-use]
|
|
102
|
-
ok(value())
|
|
103
|
-
: (blob /* ignored */, ok, _) => ok(value),
|
|
104
|
-
);
|
|
105
|
-
}
|
|
106
|
-
|
|
107
|
-
/**
|
|
108
|
-
* Alias of always.
|
|
109
|
-
*/
|
|
110
|
-
export const hardcoded: <T>(T) => Decoder<T> = always;
|
|
111
|
-
|
|
112
|
-
/**
|
|
113
|
-
* Accepts anything and returns it unchanged.
|
|
114
|
-
*
|
|
115
|
-
* Useful for situation in which you don't know or expect a specific type. Of
|
|
116
|
-
* course, the downside is that you won't know the type of the value statically
|
|
117
|
-
* and you'll have to further refine it yourself.
|
|
118
|
-
*/
|
|
119
|
-
export const unknown: Decoder<mixed> = define((blob, ok, _) => ok(blob));
|
|
120
|
-
|
|
121
|
-
/**
|
|
122
|
-
* Alias of unknown.
|
|
123
|
-
*/
|
|
124
|
-
export const mixed: Decoder<mixed> = unknown;
|
package/lib/basics.mjs
DELETED
|
@@ -1,60 +0,0 @@
|
|
|
1
|
-
import { define } from '../Decoder.mjs'
|
|
2
|
-
import { either } from './unions.mjs'
|
|
3
|
-
|
|
4
|
-
export var null_ = define(function (blob, ok, err) {
|
|
5
|
-
return blob === null ? ok(blob) : err('Must be null')
|
|
6
|
-
})
|
|
7
|
-
|
|
8
|
-
export var undefined_ = define(function (blob, ok, err) {
|
|
9
|
-
return blob === undefined ? ok(blob) : err('Must be undefined')
|
|
10
|
-
})
|
|
11
|
-
var undefined_or_null = define(function (blob, ok, err) {
|
|
12
|
-
return blob === undefined || blob === null ? ok(blob) : err('Must be undefined or null')
|
|
13
|
-
})
|
|
14
|
-
function _maybeish(emptyCase) {
|
|
15
|
-
function _inner(decoder) {
|
|
16
|
-
var rv = either(emptyCase, decoder)
|
|
17
|
-
if (arguments.length >= 2) {
|
|
18
|
-
var _defaultValue = arguments[1]
|
|
19
|
-
var _defaultValue2 = typeof _defaultValue === 'function' ? _defaultValue() : _defaultValue
|
|
20
|
-
return rv.transform(function (value) {
|
|
21
|
-
return value != null ? value : _defaultValue2
|
|
22
|
-
})
|
|
23
|
-
} else {
|
|
24
|
-
return rv
|
|
25
|
-
}
|
|
26
|
-
}
|
|
27
|
-
return _inner
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
export var nullable = _maybeish(null_)
|
|
31
|
-
|
|
32
|
-
export var optional = _maybeish(undefined_)
|
|
33
|
-
|
|
34
|
-
export var maybe = _maybeish(undefined_or_null)
|
|
35
|
-
|
|
36
|
-
export function constant(value) {
|
|
37
|
-
return define(function (blob, ok, err) {
|
|
38
|
-
return blob === value ? ok(value) : err('Must be constant ' + String(value))
|
|
39
|
-
})
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
export function always(value) {
|
|
43
|
-
return define(
|
|
44
|
-
typeof value === 'function'
|
|
45
|
-
? function (blob, ok, _) {
|
|
46
|
-
return ok(value())
|
|
47
|
-
}
|
|
48
|
-
: function (blob, ok, _) {
|
|
49
|
-
return ok(value)
|
|
50
|
-
}
|
|
51
|
-
)
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
export var hardcoded = always
|
|
55
|
-
|
|
56
|
-
export var unknown = define(function (blob, ok, _) {
|
|
57
|
-
return ok(blob)
|
|
58
|
-
})
|
|
59
|
-
|
|
60
|
-
export var mixed = unknown
|
package/lib/booleans.d.ts
DELETED
|
@@ -1,16 +0,0 @@
|
|
|
1
|
-
import { Decoder } from '../Decoder';
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* Accepts and returns booleans.
|
|
5
|
-
*/
|
|
6
|
-
export const boolean: Decoder<boolean>;
|
|
7
|
-
|
|
8
|
-
/**
|
|
9
|
-
* Accepts anything and will return its "truth" value. Will never reject.
|
|
10
|
-
*/
|
|
11
|
-
export const truthy: Decoder<boolean>;
|
|
12
|
-
|
|
13
|
-
/**
|
|
14
|
-
* Accepts numbers, but return their boolean representation.
|
|
15
|
-
*/
|
|
16
|
-
export const numericBoolean: Decoder<boolean>;
|
package/lib/booleans.js
DELETED
|
@@ -1,21 +0,0 @@
|
|
|
1
|
-
'use strict'
|
|
2
|
-
|
|
3
|
-
exports.__esModule = true
|
|
4
|
-
exports.truthy = exports.numericBoolean = exports['boolean'] = void 0
|
|
5
|
-
var _Decoder = require('../Decoder')
|
|
6
|
-
var _numbers = require('./numbers')
|
|
7
|
-
|
|
8
|
-
var _boolean = (0, _Decoder.define)(function (blob, ok, err) {
|
|
9
|
-
return typeof blob === 'boolean' ? ok(blob) : err('Must be boolean')
|
|
10
|
-
})
|
|
11
|
-
|
|
12
|
-
exports['boolean'] = _boolean
|
|
13
|
-
var truthy = (0, _Decoder.define)(function (blob, ok, _) {
|
|
14
|
-
return ok(!!blob)
|
|
15
|
-
})
|
|
16
|
-
|
|
17
|
-
exports.truthy = truthy
|
|
18
|
-
var numericBoolean = _numbers.number.transform(function (n) {
|
|
19
|
-
return !!n
|
|
20
|
-
})
|
|
21
|
-
exports.numericBoolean = numericBoolean
|
package/lib/booleans.js.flow
DELETED
|
@@ -1,22 +0,0 @@
|
|
|
1
|
-
// @flow strict
|
|
2
|
-
|
|
3
|
-
import { define } from '../Decoder';
|
|
4
|
-
import { number } from './numbers';
|
|
5
|
-
import type { Decoder } from '../Decoder';
|
|
6
|
-
|
|
7
|
-
/**
|
|
8
|
-
* Accepts and returns booleans.
|
|
9
|
-
*/
|
|
10
|
-
export const boolean: Decoder<boolean> = define((blob, ok, err) => {
|
|
11
|
-
return typeof blob === 'boolean' ? ok(blob) : err('Must be boolean');
|
|
12
|
-
});
|
|
13
|
-
|
|
14
|
-
/**
|
|
15
|
-
* Accepts anything and will return its "truth" value. Will never reject.
|
|
16
|
-
*/
|
|
17
|
-
export const truthy: Decoder<boolean> = define((blob, ok, _) => ok(!!blob));
|
|
18
|
-
|
|
19
|
-
/**
|
|
20
|
-
* Accepts numbers, but return their boolean representation.
|
|
21
|
-
*/
|
|
22
|
-
export const numericBoolean: Decoder<boolean> = number.transform((n) => !!n);
|
package/lib/booleans.mjs
DELETED
|
@@ -1,15 +0,0 @@
|
|
|
1
|
-
import { define } from '../Decoder.mjs'
|
|
2
|
-
import { number } from './numbers.mjs'
|
|
3
|
-
|
|
4
|
-
var _boolean = define(function (blob, ok, err) {
|
|
5
|
-
return typeof blob === 'boolean' ? ok(blob) : err('Must be boolean')
|
|
6
|
-
})
|
|
7
|
-
|
|
8
|
-
export { _boolean as boolean }
|
|
9
|
-
export var truthy = define(function (blob, ok, _) {
|
|
10
|
-
return ok(!!blob)
|
|
11
|
-
})
|
|
12
|
-
|
|
13
|
-
export var numericBoolean = number.transform(function (n) {
|
|
14
|
-
return !!n
|
|
15
|
-
})
|
package/lib/dates.d.ts
DELETED
|
@@ -1,15 +0,0 @@
|
|
|
1
|
-
import { Decoder } from '../Decoder';
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* Accepts and returns `Date` instances.
|
|
5
|
-
*/
|
|
6
|
-
export const date: Decoder<Date>;
|
|
7
|
-
|
|
8
|
-
/**
|
|
9
|
-
* Accepts [ISO8601](https://en.wikipedia.org/wiki/ISO_8601)-formatted strings,
|
|
10
|
-
* returns them as `Date` instances.
|
|
11
|
-
*
|
|
12
|
-
* This is very useful for working with dates in APIs: serialize them as
|
|
13
|
-
* `.toISOString()` when sending, decode them with `iso8601` when receiving.
|
|
14
|
-
*/
|
|
15
|
-
export const iso8601: Decoder<Date>;
|
package/lib/dates.js
DELETED
|
@@ -1,23 +0,0 @@
|
|
|
1
|
-
'use strict'
|
|
2
|
-
|
|
3
|
-
exports.__esModule = true
|
|
4
|
-
exports.iso8601 = exports.date = void 0
|
|
5
|
-
var _utils = require('../_utils')
|
|
6
|
-
var _Decoder = require('../Decoder')
|
|
7
|
-
var _strings = require('./strings')
|
|
8
|
-
var iso8601_re = /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(?:[.]\d+)?(?:Z|[+-]\d{2}:?\d{2})$/
|
|
9
|
-
|
|
10
|
-
var date = (0, _Decoder.define)(function (blob, ok, err) {
|
|
11
|
-
var date = (0, _utils.asDate)(blob)
|
|
12
|
-
return date !== null ? ok(date) : err('Must be a Date')
|
|
13
|
-
})
|
|
14
|
-
|
|
15
|
-
exports.date = date
|
|
16
|
-
var iso8601 = (0, _strings.regex)(iso8601_re, 'Must be ISO8601 format').transform(function (value) {
|
|
17
|
-
var date = new Date(value)
|
|
18
|
-
if (isNaN(date.getTime())) {
|
|
19
|
-
throw new Error('Must be valid date/time value')
|
|
20
|
-
}
|
|
21
|
-
return date
|
|
22
|
-
})
|
|
23
|
-
exports.iso8601 = iso8601
|