decoders 1.26.0-beta2 → 2.0.0-beta3
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 +21 -5
- package/_esm/_guard.js +15 -0
- package/_esm/_guard.js.flow +20 -0
- package/_esm/_types.js +0 -0
- package/_esm/_types.js.flow +20 -0
- package/_esm/_utils.js +93 -0
- package/_esm/_utils.js.flow +97 -0
- package/_esm/annotate.js +144 -0
- package/_esm/annotate.js.flow +218 -0
- package/_esm/core/array.js +91 -0
- package/{cjs → _esm/core}/array.js.flow +22 -25
- package/_esm/core/boolean.js +28 -0
- package/{cjs → _esm/core}/boolean.js.flow +8 -7
- package/_esm/core/composition.js +42 -0
- package/{cjs/utils.js.flow → _esm/core/composition.js.flow} +7 -22
- package/_esm/core/constants.js +46 -0
- package/{cjs → _esm/core}/constants.js.flow +13 -12
- package/_esm/core/date.js +28 -0
- package/{cjs → _esm/core}/date.js.flow +9 -7
- package/{cjs → _esm/core}/describe.js +5 -11
- package/{cjs → _esm/core}/describe.js.flow +4 -4
- package/{cjs → _esm/core}/dispatch.js +8 -15
- package/{cjs → _esm/core}/dispatch.js.flow +6 -5
- package/_esm/core/either.js +90 -0
- package/{cjs → _esm/core}/either.js.flow +55 -35
- package/_esm/core/fail.js +11 -0
- package/_esm/core/fail.js.flow +12 -0
- package/_esm/core/instanceOf.js +8 -0
- package/{cjs → _esm/core}/instanceOf.js.flow +7 -8
- package/_esm/core/json.js +15 -0
- package/{cjs → _esm/core}/json.js.flow +4 -4
- package/_esm/core/lazy.js +11 -0
- package/{cjs → _esm/core}/lazy.js.flow +1 -1
- package/_esm/core/mapping.js +54 -0
- package/_esm/core/mapping.js.flow +54 -0
- package/_esm/core/number.js +25 -0
- package/{cjs → _esm/core}/number.js.flow +10 -11
- package/_esm/core/object.js +175 -0
- package/{cjs → _esm/core}/object.js.flow +44 -52
- package/_esm/core/optional.js +38 -0
- package/{cjs → _esm/core}/optional.js.flow +6 -7
- package/{cjs → _esm/core}/string.js +18 -35
- package/{cjs → _esm/core}/string.js.flow +13 -12
- package/_esm/core/tuple.js +155 -0
- package/{cjs → _esm/core}/tuple.js.flow +62 -63
- package/_esm/format/index.js +2 -0
- package/_esm/format/index.js.flow +4 -0
- package/_esm/format/inline.js +137 -0
- package/_esm/format/inline.js.flow +122 -0
- package/_esm/format/short.js +4 -0
- package/_esm/format/short.js.flow +8 -0
- package/_esm/index.js +37 -0
- package/{cjs → _esm}/index.js.flow +31 -24
- package/_esm/result.js +139 -0
- package/_esm/result.js.flow +166 -0
- package/_guard.js +26 -0
- package/_guard.js.flow +20 -0
- package/_types.js +1 -0
- package/_types.js.flow +20 -0
- package/_utils.js +108 -0
- package/_utils.js.flow +97 -0
- package/annotate.js +161 -0
- package/annotate.js.flow +218 -0
- package/core/array.js +108 -0
- package/core/array.js.flow +103 -0
- package/core/boolean.js +44 -0
- package/core/boolean.js.flow +29 -0
- package/core/composition.js +56 -0
- package/core/composition.js.flow +43 -0
- package/core/constants.js +69 -0
- package/core/constants.js.flow +46 -0
- package/core/date.js +46 -0
- package/core/date.js.flow +40 -0
- package/core/describe.js +26 -0
- package/core/describe.js.flow +17 -0
- package/core/dispatch.js +62 -0
- package/core/dispatch.js.flow +58 -0
- package/core/either.js +117 -0
- package/core/either.js.flow +151 -0
- package/core/fail.js +21 -0
- package/core/fail.js.flow +12 -0
- package/core/instanceOf.js +19 -0
- package/core/instanceOf.js.flow +20 -0
- package/{cjs → core}/json.js +3 -5
- package/core/json.js.flow +28 -0
- package/{cjs → core}/lazy.js +1 -3
- package/core/lazy.js.flow +15 -0
- package/core/mapping.js +67 -0
- package/core/mapping.js.flow +54 -0
- package/core/number.js +40 -0
- package/core/number.js.flow +34 -0
- package/core/object.js +194 -0
- package/core/object.js.flow +203 -0
- package/core/optional.js +54 -0
- package/core/optional.js.flow +41 -0
- package/core/string.js +98 -0
- package/core/string.js.flow +82 -0
- package/core/tuple.js +173 -0
- package/core/tuple.js.flow +220 -0
- package/format/index.js +12 -0
- package/format/index.js.flow +4 -0
- package/format/inline.js +146 -0
- package/format/inline.js.flow +122 -0
- package/format/short.js +10 -0
- package/format/short.js.flow +8 -0
- package/index.js +120 -0
- package/index.js.flow +63 -0
- package/package.json +3 -11
- package/result.js +172 -0
- package/result.js.flow +166 -0
- package/cjs/array.js +0 -133
- package/cjs/boolean.js +0 -42
- package/cjs/constants.js +0 -67
- package/cjs/date.js +0 -42
- package/cjs/either.js +0 -85
- package/cjs/fail.js +0 -19
- package/cjs/fail.js.flow +0 -13
- package/cjs/guard.js +0 -30
- package/cjs/guard.js.flow +0 -36
- package/cjs/index.js +0 -397
- package/cjs/instanceOf.js +0 -17
- package/cjs/mapping.js +0 -113
- package/cjs/mapping.js.flow +0 -71
- package/cjs/number.js +0 -38
- package/cjs/object.js +0 -254
- package/cjs/optional.js +0 -52
- package/cjs/tuple.js +0 -199
- package/cjs/types.js +0 -3
- package/cjs/types.js.flow +0 -26
- package/cjs/utils.js +0 -70
- package/es/index.js +0 -1039
- package/ts/array.d.ts +0 -5
- package/ts/boolean.d.ts +0 -5
- package/ts/constants.d.ts +0 -11
- package/ts/date.d.ts +0 -4
- package/ts/describe.d.ts +0 -3
- package/ts/dispatch.d.ts +0 -8
- package/ts/either.d.ts +0 -61
- package/ts/fail.d.ts +0 -3
- package/ts/guard.d.ts +0 -7
- package/ts/helpers.d.ts +0 -79
- package/ts/index.d.ts +0 -38
- package/ts/instanceOf.d.ts +0 -3
- package/ts/json.d.ts +0 -11
- package/ts/lazy.d.ts +0 -3
- package/ts/mapping.d.ts +0 -4
- package/ts/number.d.ts +0 -6
- package/ts/object.d.ts +0 -33
- package/ts/optional.d.ts +0 -5
- package/ts/string.d.ts +0 -7
- package/ts/tuple.d.ts +0 -30
- package/ts/types.d.ts +0 -18
- package/ts/utils.d.ts +0 -13
package/CHANGELOG.md
CHANGED
|
@@ -1,6 +1,22 @@
|
|
|
1
|
-
##
|
|
1
|
+
## v2.0.0-beta
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Potentially breaking changes:
|
|
4
|
+
|
|
5
|
+
- Drop support for all Node versions below 12.x
|
|
6
|
+
- Drop support for Flow versions below 0.142.0
|
|
7
|
+
- Drop all package dependencies
|
|
8
|
+
|
|
9
|
+
New features:
|
|
10
|
+
|
|
11
|
+
- Include ES modules in published NPM builds (yay tree-shaking! 🍃)
|
|
12
|
+
- Much smaller total bundle size
|
|
13
|
+
- Better error messages for nested `either`s
|
|
14
|
+
- Guard API now has a simpler way to specify formatters
|
|
15
|
+
|
|
16
|
+
Implementation changes:
|
|
17
|
+
|
|
18
|
+
- Major reorganization of internal module structure
|
|
19
|
+
- Various simplification of internals
|
|
4
20
|
|
|
5
21
|
## v1.25.5
|
|
6
22
|
|
|
@@ -326,7 +342,7 @@ to upgrade:
|
|
|
326
342
|
```javascript
|
|
327
343
|
const mydecoder: Decoder<string> = predicate(
|
|
328
344
|
(s) => s.startsWith('x'),
|
|
329
|
-
'Must start with "x"'
|
|
345
|
+
'Must start with "x"',
|
|
330
346
|
);
|
|
331
347
|
```
|
|
332
348
|
|
|
@@ -336,7 +352,7 @@ to upgrade:
|
|
|
336
352
|
const mydecoder: Decoder<string, string> = predicate(
|
|
337
353
|
// ^^^^^^ Provide the input type to predicate() decoders
|
|
338
354
|
(s) => s.startsWith('x'),
|
|
339
|
-
'Must start with "x"'
|
|
355
|
+
'Must start with "x"',
|
|
340
356
|
);
|
|
341
357
|
```
|
|
342
358
|
|
|
@@ -560,6 +576,6 @@ to upgrade:
|
|
|
560
576
|
object({
|
|
561
577
|
name: string,
|
|
562
578
|
age: number,
|
|
563
|
-
})
|
|
579
|
+
}),
|
|
564
580
|
);
|
|
565
581
|
```
|
package/_esm/_guard.js
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import * as Result from './result';
|
|
2
|
+
import { formatInline } from './format';
|
|
3
|
+
export function guard(decoder, formatter) {
|
|
4
|
+
if (formatter === void 0) {
|
|
5
|
+
formatter = formatInline;
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
return function (blob) {
|
|
9
|
+
return Result.unwrap(Result.mapError(decoder(blob), function (annotation) {
|
|
10
|
+
var err = new Error('\n' + formatter(annotation));
|
|
11
|
+
err.name = 'Decoding error';
|
|
12
|
+
return err;
|
|
13
|
+
}));
|
|
14
|
+
};
|
|
15
|
+
}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
// @flow strict
|
|
2
|
+
|
|
3
|
+
import * as Result from './result';
|
|
4
|
+
import { formatInline } from './format';
|
|
5
|
+
import type { Annotation } from './annotate';
|
|
6
|
+
import type { Decoder, Guard } from './_types';
|
|
7
|
+
|
|
8
|
+
export function guard<T>(
|
|
9
|
+
decoder: Decoder<T>,
|
|
10
|
+
formatter: (Annotation) => string = formatInline,
|
|
11
|
+
): Guard<T> {
|
|
12
|
+
return (blob: mixed) =>
|
|
13
|
+
Result.unwrap(
|
|
14
|
+
Result.mapError(decoder(blob), (annotation) => {
|
|
15
|
+
const err = new Error('\n' + formatter(annotation));
|
|
16
|
+
err.name = 'Decoding error';
|
|
17
|
+
return err;
|
|
18
|
+
}),
|
|
19
|
+
);
|
|
20
|
+
}
|
package/_esm/_types.js
ADDED
|
File without changes
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
// @flow strict
|
|
2
|
+
|
|
3
|
+
import type { Annotation } from './annotate';
|
|
4
|
+
import type { Result } from './result';
|
|
5
|
+
|
|
6
|
+
export type Scalar = string | number | boolean | symbol | void | null;
|
|
7
|
+
|
|
8
|
+
export type Predicate<T> = (T) => boolean;
|
|
9
|
+
export type DecodeResult<T> = Result<T, Annotation>;
|
|
10
|
+
|
|
11
|
+
export type Decoder<T, F = mixed> = (F) => DecodeResult<T>;
|
|
12
|
+
export type Guard<T> = (mixed) => T;
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* A "type function" which informs Flow about how a type will be modified at runtime.
|
|
16
|
+
* Read this as "given a Guard of type T, I can produce a value of type T". This
|
|
17
|
+
* definition helps construct $ObjMap types.
|
|
18
|
+
*/
|
|
19
|
+
export type DecoderType = <T>(Decoder<T>) => T;
|
|
20
|
+
export type GuardType = <T>(Guard<T>) => T;
|
package/_esm/_utils.js
ADDED
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
// $FlowFixMe[unclear-type] - deliberate casting
|
|
2
|
+
// Two spaces of indentation
|
|
3
|
+
export var INDENT = ' ';
|
|
4
|
+
/**
|
|
5
|
+
* `x instanceof Date` checks are unreliable across stack frames (that information
|
|
6
|
+
* might get lost by the JS runtime), so we'll have to reside to more runtime
|
|
7
|
+
* inspection checks.
|
|
8
|
+
*
|
|
9
|
+
* Taken from https://stackoverflow.com/a/44198641
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
export function isDate(value) {
|
|
13
|
+
return !!value && // $FlowFixMe[method-unbinding]
|
|
14
|
+
Object.prototype.toString.call(value) === '[object Date]' && !isNaN(value);
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* Is value is a valid Date instance, then return that. If not, then return
|
|
18
|
+
* null.
|
|
19
|
+
*/
|
|
20
|
+
|
|
21
|
+
export function asDate(value) {
|
|
22
|
+
return isDate(value) ? value : null;
|
|
23
|
+
}
|
|
24
|
+
export function isMultiline(s) {
|
|
25
|
+
return s.indexOf('\n') >= 0;
|
|
26
|
+
}
|
|
27
|
+
export function indent(s, prefix) {
|
|
28
|
+
if (prefix === void 0) {
|
|
29
|
+
prefix = INDENT;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
if (isMultiline(s)) {
|
|
33
|
+
return s.split('\n').map(function (line) {
|
|
34
|
+
return prefix + line;
|
|
35
|
+
}).join('\n');
|
|
36
|
+
} else {
|
|
37
|
+
return prefix + s;
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* Walks the annotation tree and emits the annotation's key path within the
|
|
42
|
+
* object tree, and the message as a series of messages (array of strings).
|
|
43
|
+
*/
|
|
44
|
+
|
|
45
|
+
export function summarize(ann, keypath) {
|
|
46
|
+
if (keypath === void 0) {
|
|
47
|
+
keypath = [];
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
var result = [];
|
|
51
|
+
|
|
52
|
+
if (ann.type === 'array') {
|
|
53
|
+
var items = ann.items;
|
|
54
|
+
var index = 0;
|
|
55
|
+
items.forEach(function (ann) {
|
|
56
|
+
summarize(ann, [].concat(keypath, [index++])).forEach(function (item) {
|
|
57
|
+
return (// Collect to results
|
|
58
|
+
result.push(item)
|
|
59
|
+
);
|
|
60
|
+
});
|
|
61
|
+
});
|
|
62
|
+
} else if (ann.type === 'object') {
|
|
63
|
+
var fields = ann.fields;
|
|
64
|
+
Object.keys(fields).forEach(function (key) {
|
|
65
|
+
var value = fields[key];
|
|
66
|
+
summarize(value, [].concat(keypath, [key])).forEach(function (item) {
|
|
67
|
+
return (// Collect to results
|
|
68
|
+
result.push(item)
|
|
69
|
+
);
|
|
70
|
+
});
|
|
71
|
+
});
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
var text = ann.text;
|
|
75
|
+
|
|
76
|
+
if (!text) {
|
|
77
|
+
return result;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
var prefix;
|
|
81
|
+
|
|
82
|
+
if (keypath.length === 0) {
|
|
83
|
+
prefix = '';
|
|
84
|
+
} else if (keypath.length === 1) {
|
|
85
|
+
prefix = typeof keypath[0] === 'number' ? "Value at index " + keypath[0] + ": " : "Value at key " + JSON.stringify(keypath[0]) + ": ";
|
|
86
|
+
} else {
|
|
87
|
+
prefix = "Value at keypath " + keypath.map(function (x) {
|
|
88
|
+
return x.toString();
|
|
89
|
+
}).join('.') + ": ";
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
return [].concat(result, [prefix + text]);
|
|
93
|
+
}
|
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
// @flow strict
|
|
2
|
+
|
|
3
|
+
import type { Annotation } from './annotate';
|
|
4
|
+
|
|
5
|
+
// $FlowFixMe[unclear-type] - deliberate casting
|
|
6
|
+
type cast = any;
|
|
7
|
+
|
|
8
|
+
// Two spaces of indentation
|
|
9
|
+
export const INDENT = ' ';
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* `x instanceof Date` checks are unreliable across stack frames (that information
|
|
13
|
+
* might get lost by the JS runtime), so we'll have to reside to more runtime
|
|
14
|
+
* inspection checks.
|
|
15
|
+
*
|
|
16
|
+
* Taken from https://stackoverflow.com/a/44198641
|
|
17
|
+
*/
|
|
18
|
+
export function isDate(value: mixed): boolean {
|
|
19
|
+
return (
|
|
20
|
+
!!value &&
|
|
21
|
+
// $FlowFixMe[method-unbinding]
|
|
22
|
+
Object.prototype.toString.call(value) === '[object Date]' &&
|
|
23
|
+
!isNaN(value)
|
|
24
|
+
);
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* Is value is a valid Date instance, then return that. If not, then return
|
|
29
|
+
* null.
|
|
30
|
+
*/
|
|
31
|
+
export function asDate(value: mixed): Date | null {
|
|
32
|
+
return isDate(value) ? ((value: cast): Date) : null;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
export function isMultiline(s: string): boolean {
|
|
36
|
+
return s.indexOf('\n') >= 0;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
export function indent(s: string, prefix: string = INDENT): string {
|
|
40
|
+
if (isMultiline(s)) {
|
|
41
|
+
return s
|
|
42
|
+
.split('\n')
|
|
43
|
+
.map((line) => prefix + line)
|
|
44
|
+
.join('\n');
|
|
45
|
+
} else {
|
|
46
|
+
return prefix + s;
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* Walks the annotation tree and emits the annotation's key path within the
|
|
52
|
+
* object tree, and the message as a series of messages (array of strings).
|
|
53
|
+
*/
|
|
54
|
+
export function summarize(
|
|
55
|
+
ann: Annotation,
|
|
56
|
+
keypath: $ReadOnlyArray<number | string> = [],
|
|
57
|
+
): Array<string> {
|
|
58
|
+
const result: Array<string> = [];
|
|
59
|
+
|
|
60
|
+
if (ann.type === 'array') {
|
|
61
|
+
const items = ann.items;
|
|
62
|
+
let index = 0;
|
|
63
|
+
items.forEach((ann) => {
|
|
64
|
+
summarize(ann, [...keypath, index++]).forEach((item) =>
|
|
65
|
+
// Collect to results
|
|
66
|
+
result.push(item),
|
|
67
|
+
);
|
|
68
|
+
});
|
|
69
|
+
} else if (ann.type === 'object') {
|
|
70
|
+
const fields = ann.fields;
|
|
71
|
+
Object.keys(fields).forEach((key) => {
|
|
72
|
+
const value = fields[key];
|
|
73
|
+
summarize(value, [...keypath, key]).forEach((item) =>
|
|
74
|
+
// Collect to results
|
|
75
|
+
result.push(item),
|
|
76
|
+
);
|
|
77
|
+
});
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
const text = ann.text;
|
|
81
|
+
if (!text) {
|
|
82
|
+
return result;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
let prefix: string;
|
|
86
|
+
if (keypath.length === 0) {
|
|
87
|
+
prefix = '';
|
|
88
|
+
} else if (keypath.length === 1) {
|
|
89
|
+
prefix =
|
|
90
|
+
typeof keypath[0] === 'number'
|
|
91
|
+
? `Value at index ${keypath[0]}: `
|
|
92
|
+
: `Value at key ${JSON.stringify(keypath[0])}: `;
|
|
93
|
+
} else {
|
|
94
|
+
prefix = `Value at keypath ${keypath.map((x) => x.toString()).join('.')}: `;
|
|
95
|
+
}
|
|
96
|
+
return [...result, prefix + text];
|
|
97
|
+
}
|
package/_esm/annotate.js
ADDED
|
@@ -0,0 +1,144 @@
|
|
|
1
|
+
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); }
|
|
2
|
+
|
|
3
|
+
var _register = new WeakSet();
|
|
4
|
+
|
|
5
|
+
function brand(ann) {
|
|
6
|
+
_register.add(ann);
|
|
7
|
+
|
|
8
|
+
return ann;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export function object(fields, text) {
|
|
12
|
+
return brand({
|
|
13
|
+
type: 'object',
|
|
14
|
+
fields: fields,
|
|
15
|
+
text: text
|
|
16
|
+
});
|
|
17
|
+
}
|
|
18
|
+
export function array(items, text) {
|
|
19
|
+
return brand({
|
|
20
|
+
type: 'array',
|
|
21
|
+
items: items,
|
|
22
|
+
text: text
|
|
23
|
+
});
|
|
24
|
+
}
|
|
25
|
+
export function func(text) {
|
|
26
|
+
return brand({
|
|
27
|
+
type: 'function',
|
|
28
|
+
text: text
|
|
29
|
+
});
|
|
30
|
+
}
|
|
31
|
+
export function unknown(value, text) {
|
|
32
|
+
return brand({
|
|
33
|
+
type: 'unknown',
|
|
34
|
+
value: value,
|
|
35
|
+
text: text
|
|
36
|
+
});
|
|
37
|
+
}
|
|
38
|
+
export function scalar(value, text) {
|
|
39
|
+
return brand({
|
|
40
|
+
type: 'scalar',
|
|
41
|
+
value: value,
|
|
42
|
+
text: text
|
|
43
|
+
});
|
|
44
|
+
}
|
|
45
|
+
export function circularRef(text) {
|
|
46
|
+
return brand({
|
|
47
|
+
type: 'circular-ref',
|
|
48
|
+
text: text
|
|
49
|
+
});
|
|
50
|
+
}
|
|
51
|
+
/**
|
|
52
|
+
* Given an existing Annotation, set the annotation's text to a new value.
|
|
53
|
+
*/
|
|
54
|
+
|
|
55
|
+
export function updateText(annotation, text) {
|
|
56
|
+
if (text !== undefined) {
|
|
57
|
+
return brand(_extends({}, annotation, {
|
|
58
|
+
text: text
|
|
59
|
+
}));
|
|
60
|
+
} else {
|
|
61
|
+
return annotation;
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
/**
|
|
65
|
+
* Given an existing ObjectAnnotation, merges new Annotations in there.
|
|
66
|
+
*/
|
|
67
|
+
|
|
68
|
+
export function merge(objAnnotation, fields) {
|
|
69
|
+
var newFields = _extends({}, objAnnotation.fields, fields);
|
|
70
|
+
|
|
71
|
+
return object(newFields, objAnnotation.text);
|
|
72
|
+
}
|
|
73
|
+
export function asAnnotation(thing) {
|
|
74
|
+
return typeof thing === 'object' && thing !== null && _register.has(thing) ? thing : undefined;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
function annotateArray(value, text, seen) {
|
|
78
|
+
seen.add(value);
|
|
79
|
+
var items = value.map(function (v) {
|
|
80
|
+
return annotate(v, undefined, seen);
|
|
81
|
+
});
|
|
82
|
+
return array(items, text);
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
function annotateObject(obj, text, seen) {
|
|
86
|
+
seen.add(obj);
|
|
87
|
+
var fields = {};
|
|
88
|
+
Object.keys(obj).forEach(function (key) {
|
|
89
|
+
var value = obj[key];
|
|
90
|
+
fields[key] = annotate(value, undefined, seen);
|
|
91
|
+
});
|
|
92
|
+
return object(fields, text);
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
function annotate(value, text, seen) {
|
|
96
|
+
if (value === null || value === undefined || typeof value === 'string' || typeof value === 'number' || typeof value === 'boolean' || typeof value === 'symbol' || typeof value.getMonth === 'function') {
|
|
97
|
+
return scalar(value, text);
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
var ann = asAnnotation(value);
|
|
101
|
+
|
|
102
|
+
if (ann) {
|
|
103
|
+
return updateText(ann, text);
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
if (Array.isArray(value)) {
|
|
107
|
+
// "Circular references" can only exist in objects or arrays
|
|
108
|
+
if (seen.has(value)) {
|
|
109
|
+
return circularRef(text);
|
|
110
|
+
} else {
|
|
111
|
+
return annotateArray(value, text, seen);
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
if (typeof value === 'object') {
|
|
116
|
+
// "Circular references" can only exist in objects or arrays
|
|
117
|
+
if (seen.has(value)) {
|
|
118
|
+
return circularRef(text);
|
|
119
|
+
} else {
|
|
120
|
+
return annotateObject(value, text, seen);
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
if (typeof value === 'function') {
|
|
125
|
+
return func(text);
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
return unknown(value, text);
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
function public_annotate(value, text) {
|
|
132
|
+
return annotate(value, text, new WeakSet());
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
function public_annotateObject(obj, text) {
|
|
136
|
+
return annotateObject(obj, text, new WeakSet());
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
export { // This construct just ensures the "seen" weakmap (used for circular
|
|
140
|
+
// reference detection) isn't made part of the public API.
|
|
141
|
+
public_annotate as annotate, public_annotateObject as annotateObject, //
|
|
142
|
+
// NOTE: Don't acces theses private APIs directly. They are only exported here
|
|
143
|
+
// to better enable unit testing.
|
|
144
|
+
annotate as __private_annotate };
|
|
@@ -0,0 +1,218 @@
|
|
|
1
|
+
// @flow strict
|
|
2
|
+
|
|
3
|
+
type cast = $FlowFixMe;
|
|
4
|
+
|
|
5
|
+
const _register: WeakSet<{ ... }> = new WeakSet();
|
|
6
|
+
|
|
7
|
+
export type ObjectAnnotation = {|
|
|
8
|
+
+type: 'object',
|
|
9
|
+
+fields: { +[key: string]: Annotation },
|
|
10
|
+
+text?: string,
|
|
11
|
+
|};
|
|
12
|
+
|
|
13
|
+
export type ArrayAnnotation = {|
|
|
14
|
+
+type: 'array',
|
|
15
|
+
+items: $ReadOnlyArray<Annotation>,
|
|
16
|
+
+text?: string,
|
|
17
|
+
|};
|
|
18
|
+
|
|
19
|
+
export type ScalarAnnotation = {|
|
|
20
|
+
+type: 'scalar',
|
|
21
|
+
+value: mixed,
|
|
22
|
+
+text?: string,
|
|
23
|
+
|};
|
|
24
|
+
|
|
25
|
+
export type FunctionAnnotation = {|
|
|
26
|
+
+type: 'function',
|
|
27
|
+
+text?: string,
|
|
28
|
+
|};
|
|
29
|
+
|
|
30
|
+
export type CircularRefAnnotation = {|
|
|
31
|
+
+type: 'circular-ref',
|
|
32
|
+
+text?: string,
|
|
33
|
+
|};
|
|
34
|
+
|
|
35
|
+
export type UnknownAnnotation = {|
|
|
36
|
+
+type: 'unknown',
|
|
37
|
+
+value: mixed,
|
|
38
|
+
+text?: string,
|
|
39
|
+
|};
|
|
40
|
+
|
|
41
|
+
export type Annotation =
|
|
42
|
+
| ObjectAnnotation
|
|
43
|
+
| ArrayAnnotation
|
|
44
|
+
| ScalarAnnotation
|
|
45
|
+
| FunctionAnnotation
|
|
46
|
+
| CircularRefAnnotation
|
|
47
|
+
| UnknownAnnotation;
|
|
48
|
+
|
|
49
|
+
function brand<A: Annotation>(ann: A): A {
|
|
50
|
+
_register.add(ann);
|
|
51
|
+
return ann;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
export function object(
|
|
55
|
+
fields: { +[key: string]: Annotation },
|
|
56
|
+
text?: string,
|
|
57
|
+
): ObjectAnnotation {
|
|
58
|
+
return brand({ type: 'object', fields, text });
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
export function array(items: $ReadOnlyArray<Annotation>, text?: string): ArrayAnnotation {
|
|
62
|
+
return brand({
|
|
63
|
+
type: 'array',
|
|
64
|
+
items,
|
|
65
|
+
text,
|
|
66
|
+
});
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
export function func(text?: string): FunctionAnnotation {
|
|
70
|
+
return brand({
|
|
71
|
+
type: 'function',
|
|
72
|
+
text,
|
|
73
|
+
});
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
export function unknown(value: mixed, text?: string): UnknownAnnotation {
|
|
77
|
+
return brand({
|
|
78
|
+
type: 'unknown',
|
|
79
|
+
value,
|
|
80
|
+
text,
|
|
81
|
+
});
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
export function scalar(value: mixed, text?: string): ScalarAnnotation {
|
|
85
|
+
return brand({
|
|
86
|
+
type: 'scalar',
|
|
87
|
+
value,
|
|
88
|
+
text,
|
|
89
|
+
});
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
export function circularRef(text?: string): CircularRefAnnotation {
|
|
93
|
+
return brand({
|
|
94
|
+
type: 'circular-ref',
|
|
95
|
+
text,
|
|
96
|
+
});
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
/**
|
|
100
|
+
* Given an existing Annotation, set the annotation's text to a new value.
|
|
101
|
+
*/
|
|
102
|
+
export function updateText<A: Annotation>(annotation: A, text?: string): A {
|
|
103
|
+
if (text !== undefined) {
|
|
104
|
+
return brand({ ...annotation, text });
|
|
105
|
+
} else {
|
|
106
|
+
return annotation;
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
/**
|
|
111
|
+
* Given an existing ObjectAnnotation, merges new Annotations in there.
|
|
112
|
+
*/
|
|
113
|
+
export function merge(
|
|
114
|
+
objAnnotation: ObjectAnnotation,
|
|
115
|
+
fields: { +[key: string]: Annotation },
|
|
116
|
+
): ObjectAnnotation {
|
|
117
|
+
const newFields = { ...objAnnotation.fields, ...fields };
|
|
118
|
+
return object(newFields, objAnnotation.text);
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
export function asAnnotation(thing: mixed): Annotation | void {
|
|
122
|
+
return typeof thing === 'object' && thing !== null && _register.has(thing)
|
|
123
|
+
? ((thing: cast): Annotation)
|
|
124
|
+
: undefined;
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
type RefSet = WeakSet<{ ... } | $ReadOnlyArray<mixed>>;
|
|
128
|
+
|
|
129
|
+
function annotateArray(
|
|
130
|
+
value: $ReadOnlyArray<mixed>,
|
|
131
|
+
text?: string,
|
|
132
|
+
seen: RefSet,
|
|
133
|
+
): ArrayAnnotation | CircularRefAnnotation {
|
|
134
|
+
seen.add(value);
|
|
135
|
+
|
|
136
|
+
const items = value.map((v) => annotate(v, undefined, seen));
|
|
137
|
+
return array(items, text);
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
function annotateObject(
|
|
141
|
+
obj: { +[string]: mixed },
|
|
142
|
+
text?: string,
|
|
143
|
+
seen: RefSet,
|
|
144
|
+
): ObjectAnnotation {
|
|
145
|
+
seen.add(obj);
|
|
146
|
+
|
|
147
|
+
const fields = {};
|
|
148
|
+
Object.keys(obj).forEach((key) => {
|
|
149
|
+
const value = obj[key];
|
|
150
|
+
fields[key] = annotate(value, undefined, seen);
|
|
151
|
+
});
|
|
152
|
+
return object(fields, text);
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
function annotate(value: mixed, text?: string, seen: RefSet): Annotation {
|
|
156
|
+
if (
|
|
157
|
+
value === null ||
|
|
158
|
+
value === undefined ||
|
|
159
|
+
typeof value === 'string' ||
|
|
160
|
+
typeof value === 'number' ||
|
|
161
|
+
typeof value === 'boolean' ||
|
|
162
|
+
typeof value === 'symbol' ||
|
|
163
|
+
typeof value.getMonth === 'function'
|
|
164
|
+
) {
|
|
165
|
+
return scalar(value, text);
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
const ann = asAnnotation(value);
|
|
169
|
+
if (ann) {
|
|
170
|
+
return updateText(ann, text);
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
if (Array.isArray(value)) {
|
|
174
|
+
// "Circular references" can only exist in objects or arrays
|
|
175
|
+
if (seen.has(value)) {
|
|
176
|
+
return circularRef(text);
|
|
177
|
+
} else {
|
|
178
|
+
return annotateArray(value, text, seen);
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
if (typeof value === 'object') {
|
|
183
|
+
// "Circular references" can only exist in objects or arrays
|
|
184
|
+
if (seen.has(value)) {
|
|
185
|
+
return circularRef(text);
|
|
186
|
+
} else {
|
|
187
|
+
return annotateObject(value, text, seen);
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
if (typeof value === 'function') {
|
|
192
|
+
return func(text);
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
return unknown(value, text);
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
function public_annotate(value: mixed, text?: string): Annotation {
|
|
199
|
+
return annotate(value, text, new WeakSet());
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
function public_annotateObject(
|
|
203
|
+
obj: { +[string]: mixed },
|
|
204
|
+
text?: string,
|
|
205
|
+
): ObjectAnnotation {
|
|
206
|
+
return annotateObject(obj, text, new WeakSet());
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
export {
|
|
210
|
+
// This construct just ensures the "seen" weakmap (used for circular
|
|
211
|
+
// reference detection) isn't made part of the public API.
|
|
212
|
+
public_annotate as annotate,
|
|
213
|
+
public_annotateObject as annotateObject,
|
|
214
|
+
//
|
|
215
|
+
// NOTE: Don't acces theses private APIs directly. They are only exported here
|
|
216
|
+
// to better enable unit testing.
|
|
217
|
+
annotate as __private_annotate,
|
|
218
|
+
};
|