decoders 1.25.4 → 2.0.0-beta1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +30 -3
- package/cjs/_guard.js +26 -0
- package/cjs/_guard.js.flow +20 -0
- package/cjs/_types.js +1 -0
- package/cjs/_types.js.flow +20 -0
- package/cjs/_utils.js +108 -0
- package/cjs/_utils.js.flow +97 -0
- package/cjs/annotate.js +161 -0
- package/cjs/annotate.js.flow +218 -0
- package/cjs/format/index.js +12 -0
- package/cjs/format/index.js.flow +4 -0
- package/cjs/format/inline.js +146 -0
- package/cjs/format/inline.js.flow +122 -0
- package/cjs/format/short.js +10 -0
- package/cjs/format/short.js.flow +8 -0
- package/cjs/index.js +120 -0
- package/{index.js.flow → cjs/index.js.flow} +31 -24
- package/cjs/result.js +172 -0
- package/cjs/result.js.flow +166 -0
- package/cjs/stdlib/array.js +108 -0
- package/{array.js.flow → cjs/stdlib/array.js.flow} +22 -25
- package/cjs/stdlib/boolean.js +44 -0
- package/{boolean.js.flow → cjs/stdlib/boolean.js.flow} +8 -7
- package/cjs/stdlib/composition.js +56 -0
- package/{utils.js.flow → cjs/stdlib/composition.js.flow} +7 -22
- package/cjs/stdlib/constants.js +69 -0
- package/{constants.js.flow → cjs/stdlib/constants.js.flow} +13 -12
- package/cjs/stdlib/date.js +46 -0
- package/{date.js.flow → cjs/stdlib/date.js.flow} +9 -7
- package/cjs/stdlib/describe.js +26 -0
- package/{describe.js.flow → cjs/stdlib/describe.js.flow} +4 -4
- package/cjs/stdlib/dispatch.js +62 -0
- package/{dispatch.js.flow → cjs/stdlib/dispatch.js.flow} +6 -5
- package/cjs/stdlib/either.js +117 -0
- package/{either.js.flow → cjs/stdlib/either.js.flow} +55 -35
- package/cjs/stdlib/fail.js +21 -0
- package/cjs/stdlib/fail.js.flow +12 -0
- package/cjs/stdlib/instanceOf.js +19 -0
- package/{instanceOf.js.flow → cjs/stdlib/instanceOf.js.flow} +7 -8
- package/{json.js → cjs/stdlib/json.js} +3 -5
- package/{json.js.flow → cjs/stdlib/json.js.flow} +4 -4
- package/{lazy.js → cjs/stdlib/lazy.js} +1 -3
- package/{lazy.js.flow → cjs/stdlib/lazy.js.flow} +1 -1
- package/cjs/stdlib/mapping.js +67 -0
- package/cjs/stdlib/mapping.js.flow +54 -0
- package/cjs/stdlib/number.js +40 -0
- package/{number.js.flow → cjs/stdlib/number.js.flow} +10 -11
- package/cjs/stdlib/object.js +194 -0
- package/{object.js.flow → cjs/stdlib/object.js.flow} +44 -52
- package/cjs/stdlib/optional.js +54 -0
- package/{optional.js.flow → cjs/stdlib/optional.js.flow} +6 -7
- package/cjs/stdlib/string.js +98 -0
- package/{string.js.flow → cjs/stdlib/string.js.flow} +13 -12
- package/cjs/stdlib/tuple.js +173 -0
- package/{tuple.js.flow → cjs/stdlib/tuple.js.flow} +62 -63
- package/es/_guard.js +15 -0
- package/es/_types.js +0 -0
- package/es/_utils.js +93 -0
- package/es/annotate.js +144 -0
- package/es/format/index.js +2 -0
- package/es/format/inline.js +137 -0
- package/es/format/short.js +4 -0
- package/es/index.js +37 -0
- package/es/result.js +139 -0
- package/es/stdlib/array.js +91 -0
- package/es/stdlib/boolean.js +28 -0
- package/es/stdlib/composition.js +42 -0
- package/es/stdlib/constants.js +46 -0
- package/es/stdlib/date.js +28 -0
- package/{describe.js → es/stdlib/describe.js} +5 -11
- package/{dispatch.js → es/stdlib/dispatch.js} +8 -15
- package/es/stdlib/either.js +90 -0
- package/es/stdlib/fail.js +11 -0
- package/es/stdlib/instanceOf.js +8 -0
- package/es/stdlib/json.js +15 -0
- package/es/stdlib/lazy.js +11 -0
- package/es/stdlib/mapping.js +54 -0
- package/es/stdlib/number.js +25 -0
- package/es/stdlib/object.js +175 -0
- package/es/stdlib/optional.js +38 -0
- package/{string.js → es/stdlib/string.js} +18 -35
- package/es/stdlib/tuple.js +155 -0
- package/package.json +12 -7
- package/ts/_guard.d.ts +7 -0
- package/ts/_helpers.d.ts +79 -0
- package/{types.d.ts → ts/_types.d.ts} +3 -5
- package/{utils.d.ts → ts/_utils.d.ts} +3 -3
- package/ts/annotate.d.ts +58 -0
- package/{array.d.ts → ts/array.d.ts} +1 -1
- package/{boolean.d.ts → ts/boolean.d.ts} +1 -1
- package/{constants.d.ts → ts/constants.d.ts} +1 -1
- package/{date.d.ts → ts/date.d.ts} +1 -1
- package/{describe.d.ts → ts/describe.d.ts} +1 -1
- package/{dispatch.d.ts → ts/dispatch.d.ts} +3 -3
- package/{either.d.ts → ts/either.d.ts} +8 -8
- package/{fail.d.ts → ts/fail.d.ts} +1 -1
- package/ts/index.d.ts +42 -0
- package/ts/inline.d.ts +3 -0
- package/{instanceOf.d.ts → ts/instanceOf.d.ts} +1 -1
- package/{json.d.ts → ts/json.d.ts} +1 -1
- package/{lazy.d.ts → ts/lazy.d.ts} +1 -1
- package/{mapping.d.ts → ts/mapping.d.ts} +1 -1
- package/{number.d.ts → ts/number.d.ts} +1 -1
- package/{object.d.ts → ts/object.d.ts} +8 -8
- package/{optional.d.ts → ts/optional.d.ts} +1 -1
- package/ts/result.d.ts +39 -0
- package/ts/short.d.ts +3 -0
- package/{string.d.ts → ts/string.d.ts} +1 -1
- package/{tuple.d.ts → ts/tuple.d.ts} +5 -5
- package/array.js +0 -133
- package/boolean.js +0 -42
- package/constants.js +0 -67
- package/date.js +0 -42
- package/either.js +0 -85
- package/fail.js +0 -19
- package/fail.js.flow +0 -13
- package/guard.d.ts +0 -7
- package/guard.js +0 -30
- package/guard.js.flow +0 -36
- package/helpers.d.ts +0 -62
- package/index.d.ts +0 -38
- package/index.js +0 -397
- package/instanceOf.js +0 -17
- package/mapping.js +0 -113
- package/mapping.js.flow +0 -71
- package/number.js +0 -38
- package/object.js +0 -254
- package/optional.js +0 -52
- package/tuple.js +0 -199
- package/types.js +0 -5
- package/types.js.flow +0 -26
- package/utils.js +0 -70
|
@@ -1,21 +1,19 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
value: true
|
|
5
|
-
});
|
|
3
|
+
exports.__esModule = true;
|
|
6
4
|
exports.jsonObject = exports.jsonArray = exports.json = void 0;
|
|
7
5
|
|
|
8
6
|
var _array = require("./array");
|
|
9
7
|
|
|
10
8
|
var _boolean2 = require("./boolean");
|
|
11
9
|
|
|
12
|
-
var
|
|
10
|
+
var _mapping = require("./mapping");
|
|
13
11
|
|
|
14
12
|
var _either = require("./either");
|
|
15
13
|
|
|
16
14
|
var _lazy = require("./lazy");
|
|
17
15
|
|
|
18
|
-
var
|
|
16
|
+
var _constants = require("./constants");
|
|
19
17
|
|
|
20
18
|
var _number = require("./number");
|
|
21
19
|
|
|
@@ -2,13 +2,13 @@
|
|
|
2
2
|
|
|
3
3
|
import { array } from './array';
|
|
4
4
|
import { boolean } from './boolean';
|
|
5
|
-
import {
|
|
5
|
+
import { dict } from './mapping';
|
|
6
6
|
import { either6 } from './either';
|
|
7
7
|
import { lazy } from './lazy';
|
|
8
|
-
import {
|
|
8
|
+
import { null_ } from './constants';
|
|
9
9
|
import { number } from './number';
|
|
10
10
|
import { string } from './string';
|
|
11
|
-
import type { Decoder } from '
|
|
11
|
+
import type { Decoder } from '../_types';
|
|
12
12
|
|
|
13
13
|
export type JSONValue = null | string | number | boolean | JSONObject | JSONArray;
|
|
14
14
|
export type JSONObject = { [string]: JSONValue };
|
|
@@ -24,5 +24,5 @@ export const json: Decoder<JSONValue> = either6(
|
|
|
24
24
|
number,
|
|
25
25
|
boolean,
|
|
26
26
|
jsonObject,
|
|
27
|
-
jsonArray
|
|
27
|
+
jsonArray,
|
|
28
28
|
);
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
exports.__esModule = true;
|
|
4
|
+
exports.dict = dict;
|
|
5
|
+
exports.mapping = mapping;
|
|
6
|
+
|
|
7
|
+
var Result = _interopRequireWildcard(require("../result"));
|
|
8
|
+
|
|
9
|
+
var _annotate = require("../annotate");
|
|
10
|
+
|
|
11
|
+
var _composition = require("./composition");
|
|
12
|
+
|
|
13
|
+
var _object = require("./object");
|
|
14
|
+
|
|
15
|
+
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); }
|
|
16
|
+
|
|
17
|
+
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; }
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* Given an object, will decode a Map of string keys to whatever values.
|
|
21
|
+
*
|
|
22
|
+
* For example, given a decoder for a Person, we can verify a Person lookup
|
|
23
|
+
* table structure (of type Map<string, Person>) like so:
|
|
24
|
+
*
|
|
25
|
+
* mapping(person)
|
|
26
|
+
*
|
|
27
|
+
*/
|
|
28
|
+
function mapping(decoder) {
|
|
29
|
+
return (0, _composition.compose)(_object.pojo, function (blob) {
|
|
30
|
+
var tuples = [];
|
|
31
|
+
var errors = null;
|
|
32
|
+
Object.keys(blob).forEach(function (key) {
|
|
33
|
+
var value = blob[key];
|
|
34
|
+
var result = decoder(value);
|
|
35
|
+
|
|
36
|
+
if (result.type === 'ok') {
|
|
37
|
+
if (errors === null) {
|
|
38
|
+
tuples.push([key, result.value]);
|
|
39
|
+
}
|
|
40
|
+
} else {
|
|
41
|
+
tuples.length = 0; // Clear the tuples array
|
|
42
|
+
|
|
43
|
+
if (errors === null) {
|
|
44
|
+
errors = {};
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
errors[key] = result.error;
|
|
48
|
+
}
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
if (errors !== null) {
|
|
52
|
+
return Result.err((0, _annotate.merge)((0, _annotate.annotateObject)(blob), errors));
|
|
53
|
+
} else {
|
|
54
|
+
return Result.ok(new Map(tuples));
|
|
55
|
+
}
|
|
56
|
+
});
|
|
57
|
+
}
|
|
58
|
+
/**
|
|
59
|
+
* Like mapping(), but returns an object rather than a Map instance.
|
|
60
|
+
*/
|
|
61
|
+
|
|
62
|
+
|
|
63
|
+
function dict(decoder) {
|
|
64
|
+
return (0, _composition.map)(mapping(decoder), function (m) {
|
|
65
|
+
return Object.fromEntries(m);
|
|
66
|
+
});
|
|
67
|
+
}
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
// @flow strict
|
|
2
|
+
|
|
3
|
+
import * as Result from '../result';
|
|
4
|
+
import { annotateObject } from '../annotate';
|
|
5
|
+
import { compose, map } from './composition';
|
|
6
|
+
import { merge } from '../annotate';
|
|
7
|
+
import { pojo } from './object';
|
|
8
|
+
import type { Annotation } from '../annotate';
|
|
9
|
+
import type { Decoder } from '../_types';
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* Given an object, will decode a Map of string keys to whatever values.
|
|
13
|
+
*
|
|
14
|
+
* For example, given a decoder for a Person, we can verify a Person lookup
|
|
15
|
+
* table structure (of type Map<string, Person>) like so:
|
|
16
|
+
*
|
|
17
|
+
* mapping(person)
|
|
18
|
+
*
|
|
19
|
+
*/
|
|
20
|
+
export function mapping<T>(decoder: Decoder<T>): Decoder<Map<string, T>> {
|
|
21
|
+
return compose(pojo, (blob: { +[key: string]: mixed }) => {
|
|
22
|
+
let tuples: Array<[string, T]> = [];
|
|
23
|
+
let errors: { [key: string]: Annotation } | null = null;
|
|
24
|
+
|
|
25
|
+
Object.keys(blob).forEach((key: string) => {
|
|
26
|
+
const value = blob[key];
|
|
27
|
+
const result = decoder(value);
|
|
28
|
+
if (result.type === 'ok') {
|
|
29
|
+
if (errors === null) {
|
|
30
|
+
tuples.push([key, result.value]);
|
|
31
|
+
}
|
|
32
|
+
} else {
|
|
33
|
+
tuples.length = 0; // Clear the tuples array
|
|
34
|
+
if (errors === null) {
|
|
35
|
+
errors = {};
|
|
36
|
+
}
|
|
37
|
+
errors[key] = result.error;
|
|
38
|
+
}
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
if (errors !== null) {
|
|
42
|
+
return Result.err(merge(annotateObject(blob), errors));
|
|
43
|
+
} else {
|
|
44
|
+
return Result.ok(new Map(tuples));
|
|
45
|
+
}
|
|
46
|
+
});
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* Like mapping(), but returns an object rather than a Map instance.
|
|
51
|
+
*/
|
|
52
|
+
export function dict<T>(decoder: Decoder<T>): Decoder<{ [string]: T }> {
|
|
53
|
+
return map(mapping(decoder), (m) => Object.fromEntries(m));
|
|
54
|
+
}
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
exports.__esModule = true;
|
|
4
|
+
exports.positiveNumber = exports.positiveInteger = exports.number = exports.integer = void 0;
|
|
5
|
+
|
|
6
|
+
var Result = _interopRequireWildcard(require("../result"));
|
|
7
|
+
|
|
8
|
+
var _annotate = require("../annotate");
|
|
9
|
+
|
|
10
|
+
var _composition = require("./composition");
|
|
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
|
+
var anyNumber = function anyNumber(blob) {
|
|
17
|
+
return typeof blob === 'number' && !Number.isNaN(blob) ? Result.ok(blob) : Result.err((0, _annotate.annotate)(blob, 'Must be number'));
|
|
18
|
+
};
|
|
19
|
+
|
|
20
|
+
var isInteger = function isInteger(n) {
|
|
21
|
+
return Number.isInteger(n);
|
|
22
|
+
};
|
|
23
|
+
|
|
24
|
+
var isFinite = function isFinite(n) {
|
|
25
|
+
return Number.isFinite(n);
|
|
26
|
+
};
|
|
27
|
+
|
|
28
|
+
var number = (0, _composition.compose)(anyNumber, (0, _composition.predicate)(isFinite, 'Number must be finite'));
|
|
29
|
+
exports.number = number;
|
|
30
|
+
var positiveNumber = (0, _composition.compose)(number, (0, _composition.predicate)(function (n) {
|
|
31
|
+
return n >= 0;
|
|
32
|
+
}, 'Number must be positive')); // Integers
|
|
33
|
+
|
|
34
|
+
exports.positiveNumber = positiveNumber;
|
|
35
|
+
var integer = (0, _composition.compose)(number, (0, _composition.predicate)(isInteger, 'Number must be an integer'));
|
|
36
|
+
exports.integer = integer;
|
|
37
|
+
var positiveInteger = (0, _composition.compose)(integer, (0, _composition.predicate)(function (n) {
|
|
38
|
+
return n >= 0;
|
|
39
|
+
}, 'Number must be positive'));
|
|
40
|
+
exports.positiveInteger = positiveInteger;
|
|
@@ -1,15 +1,14 @@
|
|
|
1
1
|
// @flow strict
|
|
2
2
|
|
|
3
|
-
import
|
|
4
|
-
import {
|
|
5
|
-
|
|
6
|
-
import type { Decoder } from '
|
|
7
|
-
import { compose, predicate } from './utils';
|
|
3
|
+
import * as Result from '../result';
|
|
4
|
+
import { annotate } from '../annotate';
|
|
5
|
+
import { compose, predicate } from './composition';
|
|
6
|
+
import type { Decoder } from '../_types';
|
|
8
7
|
|
|
9
8
|
const anyNumber: Decoder<number> = (blob: mixed) => {
|
|
10
9
|
return typeof blob === 'number' && !Number.isNaN(blob)
|
|
11
|
-
?
|
|
12
|
-
:
|
|
10
|
+
? Result.ok(blob)
|
|
11
|
+
: Result.err(annotate(blob, 'Must be number'));
|
|
13
12
|
};
|
|
14
13
|
|
|
15
14
|
const isInteger = (n: number) => Number.isInteger(n);
|
|
@@ -17,19 +16,19 @@ const isFinite = (n: number) => Number.isFinite(n);
|
|
|
17
16
|
|
|
18
17
|
export const number: Decoder<number> = compose(
|
|
19
18
|
anyNumber,
|
|
20
|
-
predicate(isFinite, 'Number must be finite')
|
|
19
|
+
predicate(isFinite, 'Number must be finite'),
|
|
21
20
|
);
|
|
22
21
|
export const positiveNumber: Decoder<number> = compose(
|
|
23
22
|
number,
|
|
24
|
-
predicate((n) => n >= 0, 'Number must be positive')
|
|
23
|
+
predicate((n) => n >= 0, 'Number must be positive'),
|
|
25
24
|
);
|
|
26
25
|
|
|
27
26
|
// Integers
|
|
28
27
|
export const integer: Decoder<number> = compose(
|
|
29
28
|
number,
|
|
30
|
-
predicate(isInteger, 'Number must be an integer')
|
|
29
|
+
predicate(isInteger, 'Number must be an integer'),
|
|
31
30
|
);
|
|
32
31
|
export const positiveInteger: Decoder<number> = compose(
|
|
33
32
|
integer,
|
|
34
|
-
predicate((n) => n >= 0, 'Number must be positive')
|
|
33
|
+
predicate((n) => n >= 0, 'Number must be positive'),
|
|
35
34
|
);
|
|
@@ -0,0 +1,194 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
exports.__esModule = true;
|
|
4
|
+
exports.exact = exact;
|
|
5
|
+
exports.inexact = inexact;
|
|
6
|
+
exports.object = object;
|
|
7
|
+
exports.pojo = void 0;
|
|
8
|
+
|
|
9
|
+
var Result = _interopRequireWildcard(require("../result"));
|
|
10
|
+
|
|
11
|
+
var _annotate = require("../annotate");
|
|
12
|
+
|
|
13
|
+
var _composition = require("./composition");
|
|
14
|
+
|
|
15
|
+
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); }
|
|
16
|
+
|
|
17
|
+
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; }
|
|
18
|
+
|
|
19
|
+
function _extends() { _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }
|
|
20
|
+
|
|
21
|
+
function isPojo(o) {
|
|
22
|
+
return o !== null && o !== undefined && typeof o === 'object' && // This still seems to be the only reliable way to determine whether
|
|
23
|
+
// something is a pojo... ¯\_(ツ)_/¯
|
|
24
|
+
// $FlowFixMe[method-unbinding]
|
|
25
|
+
Object.prototype.toString.call(o) === '[object Object]';
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
function subtract(xs, ys) {
|
|
29
|
+
var result = new Set();
|
|
30
|
+
xs.forEach(function (x) {
|
|
31
|
+
if (!ys.has(x)) {
|
|
32
|
+
result.add(x);
|
|
33
|
+
}
|
|
34
|
+
});
|
|
35
|
+
return result;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
var pojo = function pojo(blob) {
|
|
39
|
+
return isPojo(blob) ? Result.ok( // NOTE:
|
|
40
|
+
// Since Flow 0.98, typeof o === 'object' refines to
|
|
41
|
+
// {| +[string]: mixed |}
|
|
42
|
+
// instead of
|
|
43
|
+
// {| [string]: mixed |}
|
|
44
|
+
//
|
|
45
|
+
// For rationale, see https://github.com/facebook/flow/issues/7685.
|
|
46
|
+
// In this case, we don't want to output a read-only version of
|
|
47
|
+
// the object because it's up to the user of decoders to
|
|
48
|
+
// determine what they want to do with the decoded output. If they
|
|
49
|
+
// want to write items into the array, that's fine! The fastest
|
|
50
|
+
// way to turn a read-only Object to a writeable one in ES6 seems
|
|
51
|
+
// to be to use object-spread. (Going off this benchmark:
|
|
52
|
+
// https://thecodebarbarian.com/object-assign-vs-object-spread.html)
|
|
53
|
+
_extends({}, blob)) : Result.err((0, _annotate.annotate)(blob, 'Must be an object'));
|
|
54
|
+
};
|
|
55
|
+
/**
|
|
56
|
+
* Given a mapping of fields-to-decoders, builds a decoder for an object type.
|
|
57
|
+
*
|
|
58
|
+
* For example, given decoders for a number and a string, we can construct an
|
|
59
|
+
* "object description" like so:
|
|
60
|
+
*
|
|
61
|
+
* { id: number, name: string }
|
|
62
|
+
*
|
|
63
|
+
* Which is of type:
|
|
64
|
+
*
|
|
65
|
+
* { id: Decoder<number>, name: Decoder<string> }
|
|
66
|
+
*
|
|
67
|
+
* Passing this to object() will produce the following return type:
|
|
68
|
+
*
|
|
69
|
+
* Decoder<{ id: number, name: string }>
|
|
70
|
+
*
|
|
71
|
+
* Put simply: it'll "peel off" all of the nested Decoders, puts them together
|
|
72
|
+
* in an object, and wraps it in a Decoder<...>.
|
|
73
|
+
*/
|
|
74
|
+
|
|
75
|
+
|
|
76
|
+
exports.pojo = pojo;
|
|
77
|
+
|
|
78
|
+
function object(mapping) {
|
|
79
|
+
var known = new Set(Object.keys(mapping));
|
|
80
|
+
return (0, _composition.compose)(pojo, function (blob) {
|
|
81
|
+
var actual = new Set(Object.keys(blob)); // At this point, "missing" will also include all fields that may
|
|
82
|
+
// validly be optional. We'll let the underlying decoder decide and
|
|
83
|
+
// remove the key from this missing set if the decoder accepts the
|
|
84
|
+
// value.
|
|
85
|
+
|
|
86
|
+
var missing = subtract(known, actual);
|
|
87
|
+
var record = {};
|
|
88
|
+
var errors = null;
|
|
89
|
+
Object.keys(mapping).forEach(function (key) {
|
|
90
|
+
var decoder = mapping[key];
|
|
91
|
+
var rawValue = blob[key];
|
|
92
|
+
var result = decoder(rawValue);
|
|
93
|
+
|
|
94
|
+
if (result.type === 'ok') {
|
|
95
|
+
var value = result.value;
|
|
96
|
+
|
|
97
|
+
if (value !== undefined) {
|
|
98
|
+
record[key] = value;
|
|
99
|
+
} // If this succeeded, remove the key from the missing keys
|
|
100
|
+
// tracker
|
|
101
|
+
|
|
102
|
+
|
|
103
|
+
missing["delete"](key);
|
|
104
|
+
} else {
|
|
105
|
+
var ann = result.error; // Keep track of the annotation, but don't return just yet. We
|
|
106
|
+
// want to collect more error information.
|
|
107
|
+
|
|
108
|
+
if (rawValue === undefined) {
|
|
109
|
+
// Explicitly add it to the missing set if the value is
|
|
110
|
+
// undefined. This covers explicit undefineds to be
|
|
111
|
+
// treated the same as implicit undefineds (aka missing
|
|
112
|
+
// keys).
|
|
113
|
+
missing.add(key);
|
|
114
|
+
} else {
|
|
115
|
+
if (errors === null) {
|
|
116
|
+
errors = {};
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
errors[key] = ann;
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
}); // Deal with errors now. There are two classes of errors we want to
|
|
123
|
+
// report. First of all, we want to report any inline errors in this
|
|
124
|
+
// object. Lastly, any fields that are missing should be annotated on
|
|
125
|
+
// the outer object itself.
|
|
126
|
+
|
|
127
|
+
if (errors || missing.size > 0) {
|
|
128
|
+
var objAnn = (0, _annotate.annotateObject)(blob);
|
|
129
|
+
|
|
130
|
+
if (errors) {
|
|
131
|
+
objAnn = (0, _annotate.merge)(objAnn, errors);
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
if (missing.size > 0) {
|
|
135
|
+
var errMsg = Array.from(missing).map(function (key) {
|
|
136
|
+
return "\"" + key + "\"";
|
|
137
|
+
}).join(', ');
|
|
138
|
+
var pluralized = missing.size > 1 ? 'keys' : 'key';
|
|
139
|
+
objAnn = (0, _annotate.updateText)(objAnn, "Missing " + pluralized + ": " + errMsg);
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
return Result.err(objAnn);
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
return Result.ok(record);
|
|
146
|
+
});
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
function exact(mapping) {
|
|
150
|
+
// Check the inputted object for any superfluous keys
|
|
151
|
+
var allowed = new Set(Object.keys(mapping));
|
|
152
|
+
var checked = (0, _composition.compose)(pojo, function (blob) {
|
|
153
|
+
var actual = new Set(Object.keys(blob));
|
|
154
|
+
var superfluous = subtract(actual, allowed);
|
|
155
|
+
|
|
156
|
+
if (superfluous.size > 0) {
|
|
157
|
+
return Result.err((0, _annotate.annotate)(blob, "Superfluous keys: " + Array.from(superfluous).join(', ')));
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
return Result.ok(blob);
|
|
161
|
+
}); // Defer to the "object" decoder for doing the real decoding work. Since
|
|
162
|
+
// we made sure there are no superfluous keys in this structure, it's now
|
|
163
|
+
// safe to force-cast it to an $Exact<> type.
|
|
164
|
+
|
|
165
|
+
var decoder = object(mapping);
|
|
166
|
+
return (0, _composition.compose)(checked, decoder);
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
function inexact(mapping) {
|
|
170
|
+
return (0, _composition.compose)(pojo, function (blob) {
|
|
171
|
+
var allkeys = new Set(Object.keys(blob));
|
|
172
|
+
var decoder = (0, _composition.map)(object(mapping), function (safepart) {
|
|
173
|
+
var safekeys = new Set(Object.keys(mapping)); // To account for hard-coded keys that aren't part of the input
|
|
174
|
+
|
|
175
|
+
safekeys.forEach(function (k) {
|
|
176
|
+
return allkeys.add(k);
|
|
177
|
+
});
|
|
178
|
+
var rv = {};
|
|
179
|
+
allkeys.forEach(function (k) {
|
|
180
|
+
if (safekeys.has(k)) {
|
|
181
|
+
var value = safepart[k];
|
|
182
|
+
|
|
183
|
+
if (value !== undefined) {
|
|
184
|
+
rv[k] = value;
|
|
185
|
+
}
|
|
186
|
+
} else {
|
|
187
|
+
rv[k] = blob[k];
|
|
188
|
+
}
|
|
189
|
+
});
|
|
190
|
+
return rv;
|
|
191
|
+
});
|
|
192
|
+
return decoder(blob);
|
|
193
|
+
});
|
|
194
|
+
}
|