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/_utils.js.flow
DELETED
|
@@ -1,107 +0,0 @@
|
|
|
1
|
-
// @flow strict
|
|
2
|
-
|
|
3
|
-
import type { Annotation } from './annotate';
|
|
4
|
-
import type { Scalar } from './Decoder';
|
|
5
|
-
|
|
6
|
-
// $FlowFixMe[unclear-type] - deliberate use of `any`
|
|
7
|
-
export type _Any = any;
|
|
8
|
-
|
|
9
|
-
// Two spaces of indentation
|
|
10
|
-
export const INDENT = ' ';
|
|
11
|
-
|
|
12
|
-
/**
|
|
13
|
-
* Subtract two sets. Why isn't this a standard method on Sets?
|
|
14
|
-
*/
|
|
15
|
-
export function subtract<T: Scalar>(xs: Set<T>, ys: Set<T>): Set<T> {
|
|
16
|
-
const result = new Set();
|
|
17
|
-
xs.forEach((x) => {
|
|
18
|
-
if (!ys.has(x)) {
|
|
19
|
-
result.add(x);
|
|
20
|
-
}
|
|
21
|
-
});
|
|
22
|
-
return result;
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
/**
|
|
26
|
-
* Is value is a valid Date instance, then return that. If not, then return
|
|
27
|
-
* null.
|
|
28
|
-
*/
|
|
29
|
-
export function asDate(value: mixed): Date | null {
|
|
30
|
-
//
|
|
31
|
-
// `x instanceof Date` checks are unreliable across stack frames (that
|
|
32
|
-
// information might get lost by the JS runtime), so we'll have to reside
|
|
33
|
-
// to more runtime inspection checks.
|
|
34
|
-
//
|
|
35
|
-
// Taken from https://stackoverflow.com/a/44198641
|
|
36
|
-
//
|
|
37
|
-
return !!value &&
|
|
38
|
-
// $FlowFixMe[method-unbinding]
|
|
39
|
-
Object.prototype.toString.call(value) === '[object Date]' &&
|
|
40
|
-
!isNaN(value)
|
|
41
|
-
? ((value: _Any): Date)
|
|
42
|
-
: null;
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
export function isMultiline(s: string): boolean {
|
|
46
|
-
return s.indexOf('\n') >= 0;
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
export function indent(s: string, prefix: string = INDENT): string {
|
|
50
|
-
if (isMultiline(s)) {
|
|
51
|
-
return s
|
|
52
|
-
.split('\n')
|
|
53
|
-
.map((line) => `${prefix}${line}`)
|
|
54
|
-
.join('\n');
|
|
55
|
-
} else {
|
|
56
|
-
return `${prefix}${s}`;
|
|
57
|
-
}
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
/**
|
|
61
|
-
* Walks the annotation tree and emits the annotation's key path within the
|
|
62
|
-
* object tree, and the message as a series of messages (array of strings).
|
|
63
|
-
*/
|
|
64
|
-
export function summarize(
|
|
65
|
-
ann: Annotation,
|
|
66
|
-
keypath: $ReadOnlyArray<number | string> = [],
|
|
67
|
-
): Array<string> {
|
|
68
|
-
const result: Array<string> = [];
|
|
69
|
-
|
|
70
|
-
if (ann.type === 'array') {
|
|
71
|
-
const items = ann.items;
|
|
72
|
-
let index = 0;
|
|
73
|
-
items.forEach((ann) => {
|
|
74
|
-
summarize(ann, [...keypath, index++]).forEach((item) =>
|
|
75
|
-
// Collect to results
|
|
76
|
-
result.push(item),
|
|
77
|
-
);
|
|
78
|
-
});
|
|
79
|
-
} else if (ann.type === 'object') {
|
|
80
|
-
const fields = ann.fields;
|
|
81
|
-
Object.keys(fields).forEach((key) => {
|
|
82
|
-
const value = fields[key];
|
|
83
|
-
summarize(value, [...keypath, key]).forEach((item) =>
|
|
84
|
-
// Collect to results
|
|
85
|
-
result.push(item),
|
|
86
|
-
);
|
|
87
|
-
});
|
|
88
|
-
}
|
|
89
|
-
|
|
90
|
-
const text = ann.text;
|
|
91
|
-
if (!text) {
|
|
92
|
-
return result;
|
|
93
|
-
}
|
|
94
|
-
|
|
95
|
-
let prefix: string;
|
|
96
|
-
if (keypath.length === 0) {
|
|
97
|
-
prefix = '';
|
|
98
|
-
} else if (keypath.length === 1) {
|
|
99
|
-
prefix =
|
|
100
|
-
typeof keypath[0] === 'number'
|
|
101
|
-
? `Value at index ${keypath[0]}: `
|
|
102
|
-
: `Value at key ${JSON.stringify(keypath[0])}: `;
|
|
103
|
-
} else {
|
|
104
|
-
prefix = `Value at keypath ${keypath.map(String).join('.')}: `;
|
|
105
|
-
}
|
|
106
|
-
return [...result, `${prefix}${text}`];
|
|
107
|
-
}
|
package/_utils.mjs
DELETED
|
@@ -1,70 +0,0 @@
|
|
|
1
|
-
export var INDENT = ' '
|
|
2
|
-
|
|
3
|
-
export function subtract(xs, ys) {
|
|
4
|
-
var result = new Set()
|
|
5
|
-
xs.forEach(function (x) {
|
|
6
|
-
if (!ys.has(x)) {
|
|
7
|
-
result.add(x)
|
|
8
|
-
}
|
|
9
|
-
})
|
|
10
|
-
return result
|
|
11
|
-
}
|
|
12
|
-
|
|
13
|
-
export function asDate(value) {
|
|
14
|
-
return !!value && Object.prototype.toString.call(value) === '[object Date]' && !isNaN(value) ? value : null
|
|
15
|
-
}
|
|
16
|
-
export function isMultiline(s) {
|
|
17
|
-
return s.indexOf('\n') >= 0
|
|
18
|
-
}
|
|
19
|
-
export function indent(s, prefix) {
|
|
20
|
-
if (prefix === void 0) {
|
|
21
|
-
prefix = INDENT
|
|
22
|
-
}
|
|
23
|
-
if (isMultiline(s)) {
|
|
24
|
-
return s
|
|
25
|
-
.split('\n')
|
|
26
|
-
.map(function (line) {
|
|
27
|
-
return '' + prefix + line
|
|
28
|
-
})
|
|
29
|
-
.join('\n')
|
|
30
|
-
} else {
|
|
31
|
-
return '' + prefix + s
|
|
32
|
-
}
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
export function summarize(ann, keypath) {
|
|
36
|
-
if (keypath === void 0) {
|
|
37
|
-
keypath = []
|
|
38
|
-
}
|
|
39
|
-
var result = []
|
|
40
|
-
if (ann.type === 'array') {
|
|
41
|
-
var items = ann.items
|
|
42
|
-
var index = 0
|
|
43
|
-
items.forEach(function (ann) {
|
|
44
|
-
summarize(ann, [].concat(keypath, [index++])).forEach(function (item) {
|
|
45
|
-
return result.push(item)
|
|
46
|
-
})
|
|
47
|
-
})
|
|
48
|
-
} else if (ann.type === 'object') {
|
|
49
|
-
var fields = ann.fields
|
|
50
|
-
Object.keys(fields).forEach(function (key) {
|
|
51
|
-
var value = fields[key]
|
|
52
|
-
summarize(value, [].concat(keypath, [key])).forEach(function (item) {
|
|
53
|
-
return result.push(item)
|
|
54
|
-
})
|
|
55
|
-
})
|
|
56
|
-
}
|
|
57
|
-
var text = ann.text
|
|
58
|
-
if (!text) {
|
|
59
|
-
return result
|
|
60
|
-
}
|
|
61
|
-
var prefix
|
|
62
|
-
if (keypath.length === 0) {
|
|
63
|
-
prefix = ''
|
|
64
|
-
} else if (keypath.length === 1) {
|
|
65
|
-
prefix = typeof keypath[0] === 'number' ? 'Value at index ' + keypath[0] + ': ' : 'Value at key ' + JSON.stringify(keypath[0]) + ': '
|
|
66
|
-
} else {
|
|
67
|
-
prefix = 'Value at keypath ' + keypath.map(String).join('.') + ': '
|
|
68
|
-
}
|
|
69
|
-
return [].concat(result, ['' + prefix + text])
|
|
70
|
-
}
|
package/annotate.d.ts
DELETED
|
@@ -1,62 +0,0 @@
|
|
|
1
|
-
export interface ObjectAnnotation {
|
|
2
|
-
readonly type: 'object';
|
|
3
|
-
readonly fields: { readonly [key: string]: Annotation };
|
|
4
|
-
readonly text?: string;
|
|
5
|
-
}
|
|
6
|
-
|
|
7
|
-
export interface ArrayAnnotation {
|
|
8
|
-
readonly type: 'array';
|
|
9
|
-
readonly items: readonly Annotation[];
|
|
10
|
-
readonly text?: string;
|
|
11
|
-
}
|
|
12
|
-
|
|
13
|
-
export interface ScalarAnnotation {
|
|
14
|
-
readonly type: 'scalar';
|
|
15
|
-
readonly value: unknown;
|
|
16
|
-
readonly text?: string;
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
export interface FunctionAnnotation {
|
|
20
|
-
readonly type: 'function';
|
|
21
|
-
readonly text?: string;
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
export interface CircularRefAnnotation {
|
|
25
|
-
readonly type: 'circular-ref';
|
|
26
|
-
readonly text?: string;
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
export interface UnknownAnnotation {
|
|
30
|
-
readonly type: 'unknown';
|
|
31
|
-
readonly value: unknown;
|
|
32
|
-
readonly text?: string;
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
export type Annotation =
|
|
36
|
-
| ObjectAnnotation
|
|
37
|
-
| ArrayAnnotation
|
|
38
|
-
| ScalarAnnotation
|
|
39
|
-
| FunctionAnnotation
|
|
40
|
-
| CircularRefAnnotation
|
|
41
|
-
| UnknownAnnotation;
|
|
42
|
-
|
|
43
|
-
export function object(
|
|
44
|
-
fields: { readonly [key: string]: Annotation },
|
|
45
|
-
text?: string,
|
|
46
|
-
): ObjectAnnotation;
|
|
47
|
-
export function array(items: readonly Annotation[], text?: string): ArrayAnnotation;
|
|
48
|
-
export function func(text?: string): FunctionAnnotation;
|
|
49
|
-
export function unknown(value: unknown, text?: string): UnknownAnnotation;
|
|
50
|
-
export function scalar(value: unknown, text?: string): ScalarAnnotation;
|
|
51
|
-
export function circularRef(text?: string): CircularRefAnnotation;
|
|
52
|
-
export function updateText<A extends Annotation>(annotation: A, text?: string): A;
|
|
53
|
-
export function merge(
|
|
54
|
-
objAnnotation: ObjectAnnotation,
|
|
55
|
-
fields: { readonly [key: string]: Annotation },
|
|
56
|
-
): ObjectAnnotation;
|
|
57
|
-
export function asAnnotation(thing: unknown): Annotation | void;
|
|
58
|
-
export function annotate(value: unknown, text?: string): Annotation;
|
|
59
|
-
export function annotateObject(
|
|
60
|
-
obj: { readonly [field: string]: unknown },
|
|
61
|
-
text?: string,
|
|
62
|
-
): ObjectAnnotation;
|
package/annotate.js
DELETED
|
@@ -1,145 +0,0 @@
|
|
|
1
|
-
'use strict'
|
|
2
|
-
|
|
3
|
-
exports.__esModule = true
|
|
4
|
-
exports.__private_annotate = annotate
|
|
5
|
-
exports.annotate = public_annotate
|
|
6
|
-
exports.annotateObject = public_annotateObject
|
|
7
|
-
exports.array = array
|
|
8
|
-
exports.asAnnotation = asAnnotation
|
|
9
|
-
exports.circularRef = circularRef
|
|
10
|
-
exports.func = func
|
|
11
|
-
exports.merge = merge
|
|
12
|
-
exports.object = object
|
|
13
|
-
exports.scalar = scalar
|
|
14
|
-
exports.unknown = unknown
|
|
15
|
-
exports.updateText = updateText
|
|
16
|
-
function _extends() {
|
|
17
|
-
_extends = Object.assign
|
|
18
|
-
? Object.assign.bind()
|
|
19
|
-
: function (target) {
|
|
20
|
-
for (var i = 1; i < arguments.length; i++) {
|
|
21
|
-
var source = arguments[i]
|
|
22
|
-
for (var key in source) {
|
|
23
|
-
if (Object.prototype.hasOwnProperty.call(source, key)) {
|
|
24
|
-
target[key] = source[key]
|
|
25
|
-
}
|
|
26
|
-
}
|
|
27
|
-
}
|
|
28
|
-
return target
|
|
29
|
-
}
|
|
30
|
-
return _extends.apply(this, arguments)
|
|
31
|
-
}
|
|
32
|
-
var _register = new WeakSet()
|
|
33
|
-
function brand(ann) {
|
|
34
|
-
_register.add(ann)
|
|
35
|
-
return ann
|
|
36
|
-
}
|
|
37
|
-
function object(fields, text) {
|
|
38
|
-
return brand({
|
|
39
|
-
type: 'object',
|
|
40
|
-
fields: fields,
|
|
41
|
-
text: text,
|
|
42
|
-
})
|
|
43
|
-
}
|
|
44
|
-
function array(items, text) {
|
|
45
|
-
return brand({
|
|
46
|
-
type: 'array',
|
|
47
|
-
items: items,
|
|
48
|
-
text: text,
|
|
49
|
-
})
|
|
50
|
-
}
|
|
51
|
-
function func(text) {
|
|
52
|
-
return brand({
|
|
53
|
-
type: 'function',
|
|
54
|
-
text: text,
|
|
55
|
-
})
|
|
56
|
-
}
|
|
57
|
-
function unknown(value, text) {
|
|
58
|
-
return brand({
|
|
59
|
-
type: 'unknown',
|
|
60
|
-
value: value,
|
|
61
|
-
text: text,
|
|
62
|
-
})
|
|
63
|
-
}
|
|
64
|
-
function scalar(value, text) {
|
|
65
|
-
return brand({
|
|
66
|
-
type: 'scalar',
|
|
67
|
-
value: value,
|
|
68
|
-
text: text,
|
|
69
|
-
})
|
|
70
|
-
}
|
|
71
|
-
function circularRef(text) {
|
|
72
|
-
return brand({
|
|
73
|
-
type: 'circular-ref',
|
|
74
|
-
text: text,
|
|
75
|
-
})
|
|
76
|
-
}
|
|
77
|
-
|
|
78
|
-
function updateText(annotation, text) {
|
|
79
|
-
if (text !== undefined) {
|
|
80
|
-
return brand(
|
|
81
|
-
_extends({}, annotation, {
|
|
82
|
-
text: text,
|
|
83
|
-
})
|
|
84
|
-
)
|
|
85
|
-
} else {
|
|
86
|
-
return annotation
|
|
87
|
-
}
|
|
88
|
-
}
|
|
89
|
-
|
|
90
|
-
function merge(objAnnotation, fields) {
|
|
91
|
-
var newFields = _extends({}, objAnnotation.fields, fields)
|
|
92
|
-
return object(newFields, objAnnotation.text)
|
|
93
|
-
}
|
|
94
|
-
function asAnnotation(thing) {
|
|
95
|
-
return typeof thing === 'object' && thing !== null && _register.has(thing) ? thing : undefined
|
|
96
|
-
}
|
|
97
|
-
function annotateArray(value, text, seen) {
|
|
98
|
-
seen.add(value)
|
|
99
|
-
var items = value.map(function (v) {
|
|
100
|
-
return annotate(v, undefined, seen)
|
|
101
|
-
})
|
|
102
|
-
return array(items, text)
|
|
103
|
-
}
|
|
104
|
-
function annotateObject(obj, text, seen) {
|
|
105
|
-
seen.add(obj)
|
|
106
|
-
var fields = {}
|
|
107
|
-
Object.keys(obj).forEach(function (key) {
|
|
108
|
-
var value = obj[key]
|
|
109
|
-
fields[key] = annotate(value, undefined, seen)
|
|
110
|
-
})
|
|
111
|
-
return object(fields, text)
|
|
112
|
-
}
|
|
113
|
-
function annotate(value, text, seen) {
|
|
114
|
-
if (value === null || value === undefined || typeof value === 'string' || typeof value === 'number' || typeof value === 'boolean' || typeof value === 'symbol' || typeof value.getMonth === 'function') {
|
|
115
|
-
return scalar(value, text)
|
|
116
|
-
}
|
|
117
|
-
var ann = asAnnotation(value)
|
|
118
|
-
if (ann) {
|
|
119
|
-
return updateText(ann, text)
|
|
120
|
-
}
|
|
121
|
-
if (Array.isArray(value)) {
|
|
122
|
-
if (seen.has(value)) {
|
|
123
|
-
return circularRef(text)
|
|
124
|
-
} else {
|
|
125
|
-
return annotateArray(value, text, seen)
|
|
126
|
-
}
|
|
127
|
-
}
|
|
128
|
-
if (typeof value === 'object') {
|
|
129
|
-
if (seen.has(value)) {
|
|
130
|
-
return circularRef(text)
|
|
131
|
-
} else {
|
|
132
|
-
return annotateObject(value, text, seen)
|
|
133
|
-
}
|
|
134
|
-
}
|
|
135
|
-
if (typeof value === 'function') {
|
|
136
|
-
return func(text)
|
|
137
|
-
}
|
|
138
|
-
return unknown(value, text)
|
|
139
|
-
}
|
|
140
|
-
function public_annotate(value, text) {
|
|
141
|
-
return annotate(value, text, new WeakSet())
|
|
142
|
-
}
|
|
143
|
-
function public_annotateObject(obj, text) {
|
|
144
|
-
return annotateObject(obj, text, new WeakSet())
|
|
145
|
-
}
|
package/annotate.js.flow
DELETED
|
@@ -1,218 +0,0 @@
|
|
|
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
|
-
};
|