decoders 1.25.5 → 2.0.0-beta10
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 +53 -3
- package/Decoder.d.ts +27 -0
- package/Decoder.js +199 -0
- package/Decoder.js.flow +224 -0
- package/Decoder.mjs +192 -0
- package/NotSupportedTSVersion.d.ts +1 -0
- package/README.md +105 -961
- package/_utils.d.ts +9 -0
- package/_utils.js +102 -0
- package/_utils.js.flow +93 -0
- package/_utils.mjs +89 -0
- package/annotate.d.ts +62 -0
- package/annotate.js +161 -0
- package/annotate.js.flow +218 -0
- package/annotate.mjs +144 -0
- package/format.d.ts +4 -0
- package/format.js +151 -0
- package/format.js.flow +126 -0
- package/format.mjs +140 -0
- package/index.d.ts +38 -36
- package/index.js +69 -376
- package/index.js.flow +39 -51
- package/index.mjs +11 -0
- package/{helpers.d.ts → lib/_helpers.d.ts} +0 -0
- package/lib/arrays.d.ts +37 -0
- package/lib/arrays.js +138 -0
- package/lib/arrays.js.flow +138 -0
- package/lib/arrays.mjs +123 -0
- package/lib/basics.d.ts +14 -0
- package/lib/basics.js +109 -0
- package/lib/basics.js.flow +85 -0
- package/lib/basics.mjs +85 -0
- package/{boolean.d.ts → lib/booleans.d.ts} +1 -1
- package/lib/booleans.js +35 -0
- package/lib/booleans.js.flow +22 -0
- package/lib/booleans.mjs +25 -0
- package/{date.d.ts → lib/dates.d.ts} +1 -1
- package/lib/dates.js +44 -0
- package/lib/dates.js.flow +40 -0
- package/lib/dates.mjs +34 -0
- package/{json.d.ts → lib/json.d.ts} +1 -1
- package/lib/json.js +55 -0
- package/lib/json.js.flow +50 -0
- package/lib/json.mjs +40 -0
- package/{number.d.ts → lib/numbers.d.ts} +2 -1
- package/lib/numbers.js +52 -0
- package/lib/numbers.js.flow +49 -0
- package/lib/numbers.mjs +42 -0
- package/{object.d.ts → lib/objects.d.ts} +11 -6
- package/lib/objects.js +240 -0
- package/lib/objects.js.flow +246 -0
- package/lib/objects.mjs +223 -0
- package/lib/strings.d.ts +13 -0
- package/lib/strings.js +101 -0
- package/lib/strings.js.flow +90 -0
- package/lib/strings.mjs +82 -0
- package/lib/unions.d.ts +78 -0
- package/lib/unions.js +161 -0
- package/lib/unions.js.flow +158 -0
- package/lib/unions.mjs +145 -0
- package/lib/utilities.d.ts +10 -0
- package/lib/utilities.js +94 -0
- package/lib/utilities.js.flow +84 -0
- package/lib/utilities.mjs +79 -0
- package/package.json +78 -23
- package/result.d.ts +16 -0
- package/result.js +34 -0
- package/result.js.flow +26 -0
- package/result.mjs +27 -0
- package/array.d.ts +0 -5
- package/array.js +0 -133
- package/array.js.flow +0 -106
- package/boolean.js +0 -42
- package/boolean.js.flow +0 -28
- package/constants.d.ts +0 -11
- package/constants.js +0 -67
- package/constants.js.flow +0 -45
- package/date.js +0 -42
- package/date.js.flow +0 -38
- package/describe.d.ts +0 -3
- package/describe.js +0 -22
- package/describe.js.flow +0 -17
- package/dispatch.d.ts +0 -8
- package/dispatch.js +0 -58
- package/dispatch.js.flow +0 -57
- package/either.d.ts +0 -61
- package/either.js +0 -85
- package/either.js.flow +0 -131
- package/fail.d.ts +0 -3
- 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/instanceOf.d.ts +0 -3
- package/instanceOf.js +0 -17
- package/instanceOf.js.flow +0 -21
- package/json.js +0 -33
- package/json.js.flow +0 -28
- package/lazy.d.ts +0 -3
- package/lazy.js +0 -18
- package/lazy.js.flow +0 -15
- package/mapping.d.ts +0 -4
- package/mapping.js +0 -113
- package/mapping.js.flow +0 -71
- package/number.js +0 -38
- package/number.js.flow +0 -35
- package/object.js +0 -254
- package/object.js.flow +0 -211
- package/optional.d.ts +0 -5
- package/optional.js +0 -52
- package/optional.js.flow +0 -42
- package/string.d.ts +0 -7
- package/string.js +0 -93
- package/string.js.flow +0 -81
- package/tuple.d.ts +0 -30
- package/tuple.js +0 -199
- package/tuple.js.flow +0 -221
- package/types.d.ts +0 -18
- package/types.js +0 -5
- package/types.js.flow +0 -26
- package/utils.d.ts +0 -13
- package/utils.js +0 -70
- package/utils.js.flow +0 -58
package/_utils.d.ts
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { Annotation } from './annotate';
|
|
2
|
+
|
|
3
|
+
export function asDate(value: unknown): Date | null;
|
|
4
|
+
export function isMultiline(s: string): boolean;
|
|
5
|
+
export function indent(s: string, prefix?: string): string;
|
|
6
|
+
export function summarize(
|
|
7
|
+
ann: Annotation,
|
|
8
|
+
keypath?: ReadonlyArray<number | string>,
|
|
9
|
+
): string[];
|
package/_utils.js
ADDED
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
exports.__esModule = true;
|
|
4
|
+
exports.INDENT = void 0;
|
|
5
|
+
exports.asDate = asDate;
|
|
6
|
+
exports.indent = indent;
|
|
7
|
+
exports.isMultiline = isMultiline;
|
|
8
|
+
exports.summarize = summarize;
|
|
9
|
+
// $FlowFixMe[unclear-type] - deliberate use of `any`
|
|
10
|
+
// Two spaces of indentation
|
|
11
|
+
var INDENT = ' ';
|
|
12
|
+
/**
|
|
13
|
+
* Is value is a valid Date instance, then return that. If not, then return
|
|
14
|
+
* null.
|
|
15
|
+
*/
|
|
16
|
+
|
|
17
|
+
exports.INDENT = INDENT;
|
|
18
|
+
|
|
19
|
+
function asDate(value) {
|
|
20
|
+
//
|
|
21
|
+
// `x instanceof Date` checks are unreliable across stack frames (that
|
|
22
|
+
// information might get lost by the JS runtime), so we'll have to reside
|
|
23
|
+
// to more runtime inspection checks.
|
|
24
|
+
//
|
|
25
|
+
// Taken from https://stackoverflow.com/a/44198641
|
|
26
|
+
//
|
|
27
|
+
return !!value && // $FlowFixMe[method-unbinding]
|
|
28
|
+
Object.prototype.toString.call(value) === '[object Date]' && !isNaN(value) ? value : null;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
function isMultiline(s) {
|
|
32
|
+
return s.indexOf('\n') >= 0;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
function indent(s, prefix) {
|
|
36
|
+
if (prefix === void 0) {
|
|
37
|
+
prefix = INDENT;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
if (isMultiline(s)) {
|
|
41
|
+
return s.split('\n').map(function (line) {
|
|
42
|
+
return prefix + line;
|
|
43
|
+
}).join('\n');
|
|
44
|
+
} else {
|
|
45
|
+
return prefix + s;
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
/**
|
|
49
|
+
* Walks the annotation tree and emits the annotation's key path within the
|
|
50
|
+
* object tree, and the message as a series of messages (array of strings).
|
|
51
|
+
*/
|
|
52
|
+
|
|
53
|
+
|
|
54
|
+
function summarize(ann, keypath) {
|
|
55
|
+
if (keypath === void 0) {
|
|
56
|
+
keypath = [];
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
var result = [];
|
|
60
|
+
|
|
61
|
+
if (ann.type === 'array') {
|
|
62
|
+
var items = ann.items;
|
|
63
|
+
var index = 0;
|
|
64
|
+
items.forEach(function (ann) {
|
|
65
|
+
summarize(ann, [].concat(keypath, [index++])).forEach(function (item) {
|
|
66
|
+
return (// Collect to results
|
|
67
|
+
result.push(item)
|
|
68
|
+
);
|
|
69
|
+
});
|
|
70
|
+
});
|
|
71
|
+
} else if (ann.type === 'object') {
|
|
72
|
+
var fields = ann.fields;
|
|
73
|
+
Object.keys(fields).forEach(function (key) {
|
|
74
|
+
var value = fields[key];
|
|
75
|
+
summarize(value, [].concat(keypath, [key])).forEach(function (item) {
|
|
76
|
+
return (// Collect to results
|
|
77
|
+
result.push(item)
|
|
78
|
+
);
|
|
79
|
+
});
|
|
80
|
+
});
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
var text = ann.text;
|
|
84
|
+
|
|
85
|
+
if (!text) {
|
|
86
|
+
return result;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
var prefix;
|
|
90
|
+
|
|
91
|
+
if (keypath.length === 0) {
|
|
92
|
+
prefix = '';
|
|
93
|
+
} else if (keypath.length === 1) {
|
|
94
|
+
prefix = typeof keypath[0] === 'number' ? "Value at index " + keypath[0] + ": " : "Value at key " + JSON.stringify(keypath[0]) + ": ";
|
|
95
|
+
} else {
|
|
96
|
+
prefix = "Value at keypath " + keypath.map(function (x) {
|
|
97
|
+
return x.toString();
|
|
98
|
+
}).join('.') + ": ";
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
return [].concat(result, [prefix + text]);
|
|
102
|
+
}
|
package/_utils.js.flow
ADDED
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
// @flow strict
|
|
2
|
+
|
|
3
|
+
import type { Annotation } from './annotate';
|
|
4
|
+
|
|
5
|
+
// $FlowFixMe[unclear-type] - deliberate use of `any`
|
|
6
|
+
export type _Any = any;
|
|
7
|
+
|
|
8
|
+
// Two spaces of indentation
|
|
9
|
+
export const INDENT = ' ';
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* Is value is a valid Date instance, then return that. If not, then return
|
|
13
|
+
* null.
|
|
14
|
+
*/
|
|
15
|
+
export function asDate(value: mixed): Date | null {
|
|
16
|
+
//
|
|
17
|
+
// `x instanceof Date` checks are unreliable across stack frames (that
|
|
18
|
+
// information might get lost by the JS runtime), so we'll have to reside
|
|
19
|
+
// to more runtime inspection checks.
|
|
20
|
+
//
|
|
21
|
+
// Taken from https://stackoverflow.com/a/44198641
|
|
22
|
+
//
|
|
23
|
+
return !!value &&
|
|
24
|
+
// $FlowFixMe[method-unbinding]
|
|
25
|
+
Object.prototype.toString.call(value) === '[object Date]' &&
|
|
26
|
+
!isNaN(value)
|
|
27
|
+
? ((value: _Any): Date)
|
|
28
|
+
: null;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
export function isMultiline(s: string): boolean {
|
|
32
|
+
return s.indexOf('\n') >= 0;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
export function indent(s: string, prefix: string = INDENT): string {
|
|
36
|
+
if (isMultiline(s)) {
|
|
37
|
+
return s
|
|
38
|
+
.split('\n')
|
|
39
|
+
.map((line) => prefix + line)
|
|
40
|
+
.join('\n');
|
|
41
|
+
} else {
|
|
42
|
+
return prefix + s;
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* Walks the annotation tree and emits the annotation's key path within the
|
|
48
|
+
* object tree, and the message as a series of messages (array of strings).
|
|
49
|
+
*/
|
|
50
|
+
export function summarize(
|
|
51
|
+
ann: Annotation,
|
|
52
|
+
keypath: $ReadOnlyArray<number | string> = [],
|
|
53
|
+
): Array<string> {
|
|
54
|
+
const result: Array<string> = [];
|
|
55
|
+
|
|
56
|
+
if (ann.type === 'array') {
|
|
57
|
+
const items = ann.items;
|
|
58
|
+
let index = 0;
|
|
59
|
+
items.forEach((ann) => {
|
|
60
|
+
summarize(ann, [...keypath, index++]).forEach((item) =>
|
|
61
|
+
// Collect to results
|
|
62
|
+
result.push(item),
|
|
63
|
+
);
|
|
64
|
+
});
|
|
65
|
+
} else if (ann.type === 'object') {
|
|
66
|
+
const fields = ann.fields;
|
|
67
|
+
Object.keys(fields).forEach((key) => {
|
|
68
|
+
const value = fields[key];
|
|
69
|
+
summarize(value, [...keypath, key]).forEach((item) =>
|
|
70
|
+
// Collect to results
|
|
71
|
+
result.push(item),
|
|
72
|
+
);
|
|
73
|
+
});
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
const text = ann.text;
|
|
77
|
+
if (!text) {
|
|
78
|
+
return result;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
let prefix: string;
|
|
82
|
+
if (keypath.length === 0) {
|
|
83
|
+
prefix = '';
|
|
84
|
+
} else if (keypath.length === 1) {
|
|
85
|
+
prefix =
|
|
86
|
+
typeof keypath[0] === 'number'
|
|
87
|
+
? `Value at index ${keypath[0]}: `
|
|
88
|
+
: `Value at key ${JSON.stringify(keypath[0])}: `;
|
|
89
|
+
} else {
|
|
90
|
+
prefix = `Value at keypath ${keypath.map((x) => x.toString()).join('.')}: `;
|
|
91
|
+
}
|
|
92
|
+
return [...result, prefix + text];
|
|
93
|
+
}
|
package/_utils.mjs
ADDED
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
// $FlowFixMe[unclear-type] - deliberate use of `any`
|
|
2
|
+
// Two spaces of indentation
|
|
3
|
+
export var INDENT = ' ';
|
|
4
|
+
/**
|
|
5
|
+
* Is value is a valid Date instance, then return that. If not, then return
|
|
6
|
+
* null.
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
export function asDate(value) {
|
|
10
|
+
//
|
|
11
|
+
// `x instanceof Date` checks are unreliable across stack frames (that
|
|
12
|
+
// information might get lost by the JS runtime), so we'll have to reside
|
|
13
|
+
// to more runtime inspection checks.
|
|
14
|
+
//
|
|
15
|
+
// Taken from https://stackoverflow.com/a/44198641
|
|
16
|
+
//
|
|
17
|
+
return !!value && // $FlowFixMe[method-unbinding]
|
|
18
|
+
Object.prototype.toString.call(value) === '[object Date]' && !isNaN(value) ? value : null;
|
|
19
|
+
}
|
|
20
|
+
export function isMultiline(s) {
|
|
21
|
+
return s.indexOf('\n') >= 0;
|
|
22
|
+
}
|
|
23
|
+
export function indent(s, prefix) {
|
|
24
|
+
if (prefix === void 0) {
|
|
25
|
+
prefix = INDENT;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
if (isMultiline(s)) {
|
|
29
|
+
return s.split('\n').map(function (line) {
|
|
30
|
+
return prefix + line;
|
|
31
|
+
}).join('\n');
|
|
32
|
+
} else {
|
|
33
|
+
return prefix + s;
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
/**
|
|
37
|
+
* Walks the annotation tree and emits the annotation's key path within the
|
|
38
|
+
* object tree, and the message as a series of messages (array of strings).
|
|
39
|
+
*/
|
|
40
|
+
|
|
41
|
+
export function summarize(ann, keypath) {
|
|
42
|
+
if (keypath === void 0) {
|
|
43
|
+
keypath = [];
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
var result = [];
|
|
47
|
+
|
|
48
|
+
if (ann.type === 'array') {
|
|
49
|
+
var items = ann.items;
|
|
50
|
+
var index = 0;
|
|
51
|
+
items.forEach(function (ann) {
|
|
52
|
+
summarize(ann, [].concat(keypath, [index++])).forEach(function (item) {
|
|
53
|
+
return (// Collect to results
|
|
54
|
+
result.push(item)
|
|
55
|
+
);
|
|
56
|
+
});
|
|
57
|
+
});
|
|
58
|
+
} else if (ann.type === 'object') {
|
|
59
|
+
var fields = ann.fields;
|
|
60
|
+
Object.keys(fields).forEach(function (key) {
|
|
61
|
+
var value = fields[key];
|
|
62
|
+
summarize(value, [].concat(keypath, [key])).forEach(function (item) {
|
|
63
|
+
return (// Collect to results
|
|
64
|
+
result.push(item)
|
|
65
|
+
);
|
|
66
|
+
});
|
|
67
|
+
});
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
var text = ann.text;
|
|
71
|
+
|
|
72
|
+
if (!text) {
|
|
73
|
+
return result;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
var prefix;
|
|
77
|
+
|
|
78
|
+
if (keypath.length === 0) {
|
|
79
|
+
prefix = '';
|
|
80
|
+
} else if (keypath.length === 1) {
|
|
81
|
+
prefix = typeof keypath[0] === 'number' ? "Value at index " + keypath[0] + ": " : "Value at key " + JSON.stringify(keypath[0]) + ": ";
|
|
82
|
+
} else {
|
|
83
|
+
prefix = "Value at keypath " + keypath.map(function (x) {
|
|
84
|
+
return x.toString();
|
|
85
|
+
}).join('.') + ": ";
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
return [].concat(result, [prefix + text]);
|
|
89
|
+
}
|
package/annotate.d.ts
ADDED
|
@@ -0,0 +1,62 @@
|
|
|
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
ADDED
|
@@ -0,0 +1,161 @@
|
|
|
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
|
+
|
|
17
|
+
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); }
|
|
18
|
+
|
|
19
|
+
var _register = new WeakSet();
|
|
20
|
+
|
|
21
|
+
function brand(ann) {
|
|
22
|
+
_register.add(ann);
|
|
23
|
+
|
|
24
|
+
return ann;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
function object(fields, text) {
|
|
28
|
+
return brand({
|
|
29
|
+
type: 'object',
|
|
30
|
+
fields: fields,
|
|
31
|
+
text: text
|
|
32
|
+
});
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
function array(items, text) {
|
|
36
|
+
return brand({
|
|
37
|
+
type: 'array',
|
|
38
|
+
items: items,
|
|
39
|
+
text: text
|
|
40
|
+
});
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
function func(text) {
|
|
44
|
+
return brand({
|
|
45
|
+
type: 'function',
|
|
46
|
+
text: text
|
|
47
|
+
});
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
function unknown(value, text) {
|
|
51
|
+
return brand({
|
|
52
|
+
type: 'unknown',
|
|
53
|
+
value: value,
|
|
54
|
+
text: text
|
|
55
|
+
});
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
function scalar(value, text) {
|
|
59
|
+
return brand({
|
|
60
|
+
type: 'scalar',
|
|
61
|
+
value: value,
|
|
62
|
+
text: text
|
|
63
|
+
});
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
function circularRef(text) {
|
|
67
|
+
return brand({
|
|
68
|
+
type: 'circular-ref',
|
|
69
|
+
text: text
|
|
70
|
+
});
|
|
71
|
+
}
|
|
72
|
+
/**
|
|
73
|
+
* Given an existing Annotation, set the annotation's text to a new value.
|
|
74
|
+
*/
|
|
75
|
+
|
|
76
|
+
|
|
77
|
+
function updateText(annotation, text) {
|
|
78
|
+
if (text !== undefined) {
|
|
79
|
+
return brand(_extends({}, annotation, {
|
|
80
|
+
text: text
|
|
81
|
+
}));
|
|
82
|
+
} else {
|
|
83
|
+
return annotation;
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
/**
|
|
87
|
+
* Given an existing ObjectAnnotation, merges new Annotations in there.
|
|
88
|
+
*/
|
|
89
|
+
|
|
90
|
+
|
|
91
|
+
function merge(objAnnotation, fields) {
|
|
92
|
+
var newFields = _extends({}, objAnnotation.fields, fields);
|
|
93
|
+
|
|
94
|
+
return object(newFields, objAnnotation.text);
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
function asAnnotation(thing) {
|
|
98
|
+
return typeof thing === 'object' && thing !== null && _register.has(thing) ? thing : undefined;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
function annotateArray(value, text, seen) {
|
|
102
|
+
seen.add(value);
|
|
103
|
+
var items = value.map(function (v) {
|
|
104
|
+
return annotate(v, undefined, seen);
|
|
105
|
+
});
|
|
106
|
+
return array(items, text);
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
function annotateObject(obj, text, seen) {
|
|
110
|
+
seen.add(obj);
|
|
111
|
+
var fields = {};
|
|
112
|
+
Object.keys(obj).forEach(function (key) {
|
|
113
|
+
var value = obj[key];
|
|
114
|
+
fields[key] = annotate(value, undefined, seen);
|
|
115
|
+
});
|
|
116
|
+
return object(fields, text);
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
function annotate(value, text, seen) {
|
|
120
|
+
if (value === null || value === undefined || typeof value === 'string' || typeof value === 'number' || typeof value === 'boolean' || typeof value === 'symbol' || typeof value.getMonth === 'function') {
|
|
121
|
+
return scalar(value, text);
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
var ann = asAnnotation(value);
|
|
125
|
+
|
|
126
|
+
if (ann) {
|
|
127
|
+
return updateText(ann, text);
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
if (Array.isArray(value)) {
|
|
131
|
+
// "Circular references" can only exist in objects or arrays
|
|
132
|
+
if (seen.has(value)) {
|
|
133
|
+
return circularRef(text);
|
|
134
|
+
} else {
|
|
135
|
+
return annotateArray(value, text, seen);
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
if (typeof value === 'object') {
|
|
140
|
+
// "Circular references" can only exist in objects or arrays
|
|
141
|
+
if (seen.has(value)) {
|
|
142
|
+
return circularRef(text);
|
|
143
|
+
} else {
|
|
144
|
+
return annotateObject(value, text, seen);
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
if (typeof value === 'function') {
|
|
149
|
+
return func(text);
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
return unknown(value, text);
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
function public_annotate(value, text) {
|
|
156
|
+
return annotate(value, text, new WeakSet());
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
function public_annotateObject(obj, text) {
|
|
160
|
+
return annotateObject(obj, text, new WeakSet());
|
|
161
|
+
}
|