decoders 2.0.0 → 2.0.1
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 +11 -0
- package/Decoder.js +41 -148
- package/Decoder.js.flow +1 -1
- package/Decoder.mjs +38 -145
- package/README.md +1 -1
- package/_utils.js +50 -61
- package/_utils.js.flow +18 -4
- package/_utils.mjs +41 -52
- package/annotate.js +79 -72
- package/annotate.mjs +62 -58
- package/format.js +64 -74
- package/format.js.flow +11 -11
- package/format.mjs +58 -68
- package/index.js +66 -66
- package/index.mjs +11 -11
- package/lib/arrays.js +47 -89
- package/lib/arrays.mjs +38 -78
- package/lib/basics.js +59 -109
- package/lib/basics.mjs +49 -94
- package/lib/booleans.js +14 -24
- package/lib/booleans.mjs +9 -19
- package/lib/dates.js +18 -34
- package/lib/dates.mjs +12 -27
- package/lib/json.d.ts +3 -3
- package/lib/json.js +20 -42
- package/lib/json.js.flow +2 -2
- package/lib/json.mjs +13 -35
- package/lib/numbers.js +19 -39
- package/lib/numbers.mjs +11 -31
- package/lib/objects.d.ts +11 -10
- package/lib/objects.js +84 -163
- package/lib/objects.js.flow +4 -12
- package/lib/objects.mjs +74 -150
- package/lib/strings.js +41 -84
- package/lib/strings.mjs +25 -67
- package/lib/unions.d.ts +1 -1
- package/lib/unions.js +52 -116
- package/lib/unions.js.flow +1 -1
- package/lib/unions.mjs +46 -109
- package/lib/utilities.d.ts +7 -1
- package/lib/utilities.js +23 -50
- package/lib/utilities.mjs +15 -38
- package/package.json +1 -1
- package/result.js +9 -22
- package/result.mjs +5 -17
package/annotate.mjs
CHANGED
|
@@ -1,144 +1,148 @@
|
|
|
1
|
-
function _extends() {
|
|
1
|
+
function _extends() {
|
|
2
|
+
_extends = Object.assign
|
|
3
|
+
? Object.assign.bind()
|
|
4
|
+
: function (target) {
|
|
5
|
+
for (var i = 1; i < arguments.length; i++) {
|
|
6
|
+
var source = arguments[i]
|
|
7
|
+
for (var key in source) {
|
|
8
|
+
if (Object.prototype.hasOwnProperty.call(source, key)) {
|
|
9
|
+
target[key] = source[key]
|
|
10
|
+
}
|
|
11
|
+
}
|
|
12
|
+
}
|
|
13
|
+
return target
|
|
14
|
+
}
|
|
15
|
+
return _extends.apply(this, arguments)
|
|
16
|
+
}
|
|
2
17
|
|
|
3
|
-
var _register = new WeakSet()
|
|
18
|
+
var _register = new WeakSet()
|
|
4
19
|
|
|
5
20
|
function brand(ann) {
|
|
6
|
-
_register.add(ann)
|
|
21
|
+
_register.add(ann)
|
|
7
22
|
|
|
8
|
-
return ann
|
|
23
|
+
return ann
|
|
9
24
|
}
|
|
10
25
|
|
|
11
26
|
export function object(fields, text) {
|
|
12
27
|
return brand({
|
|
13
28
|
type: 'object',
|
|
14
29
|
fields: fields,
|
|
15
|
-
text: text
|
|
16
|
-
})
|
|
30
|
+
text: text,
|
|
31
|
+
})
|
|
17
32
|
}
|
|
18
33
|
export function array(items, text) {
|
|
19
34
|
return brand({
|
|
20
35
|
type: 'array',
|
|
21
36
|
items: items,
|
|
22
|
-
text: text
|
|
23
|
-
})
|
|
37
|
+
text: text,
|
|
38
|
+
})
|
|
24
39
|
}
|
|
25
40
|
export function func(text) {
|
|
26
41
|
return brand({
|
|
27
42
|
type: 'function',
|
|
28
|
-
text: text
|
|
29
|
-
})
|
|
43
|
+
text: text,
|
|
44
|
+
})
|
|
30
45
|
}
|
|
31
46
|
export function unknown(value, text) {
|
|
32
47
|
return brand({
|
|
33
48
|
type: 'unknown',
|
|
34
49
|
value: value,
|
|
35
|
-
text: text
|
|
36
|
-
})
|
|
50
|
+
text: text,
|
|
51
|
+
})
|
|
37
52
|
}
|
|
38
53
|
export function scalar(value, text) {
|
|
39
54
|
return brand({
|
|
40
55
|
type: 'scalar',
|
|
41
56
|
value: value,
|
|
42
|
-
text: text
|
|
43
|
-
})
|
|
57
|
+
text: text,
|
|
58
|
+
})
|
|
44
59
|
}
|
|
45
60
|
export function circularRef(text) {
|
|
46
61
|
return brand({
|
|
47
62
|
type: 'circular-ref',
|
|
48
|
-
text: text
|
|
49
|
-
})
|
|
63
|
+
text: text,
|
|
64
|
+
})
|
|
50
65
|
}
|
|
51
|
-
/**
|
|
52
|
-
* Given an existing Annotation, set the annotation's text to a new value.
|
|
53
|
-
*/
|
|
54
66
|
|
|
55
67
|
export function updateText(annotation, text) {
|
|
56
68
|
if (text !== undefined) {
|
|
57
|
-
return brand(
|
|
58
|
-
|
|
59
|
-
|
|
69
|
+
return brand(
|
|
70
|
+
_extends({}, annotation, {
|
|
71
|
+
text: text,
|
|
72
|
+
})
|
|
73
|
+
)
|
|
60
74
|
} else {
|
|
61
|
-
return annotation
|
|
75
|
+
return annotation
|
|
62
76
|
}
|
|
63
77
|
}
|
|
64
|
-
/**
|
|
65
|
-
* Given an existing ObjectAnnotation, merges new Annotations in there.
|
|
66
|
-
*/
|
|
67
78
|
|
|
68
79
|
export function merge(objAnnotation, fields) {
|
|
69
|
-
var newFields = _extends({}, objAnnotation.fields, fields)
|
|
80
|
+
var newFields = _extends({}, objAnnotation.fields, fields)
|
|
70
81
|
|
|
71
|
-
return object(newFields, objAnnotation.text)
|
|
82
|
+
return object(newFields, objAnnotation.text)
|
|
72
83
|
}
|
|
73
84
|
export function asAnnotation(thing) {
|
|
74
|
-
return typeof thing === 'object' && thing !== null && _register.has(thing) ? thing : undefined
|
|
85
|
+
return typeof thing === 'object' && thing !== null && _register.has(thing) ? thing : undefined
|
|
75
86
|
}
|
|
76
87
|
|
|
77
88
|
function annotateArray(value, text, seen) {
|
|
78
|
-
seen.add(value)
|
|
89
|
+
seen.add(value)
|
|
79
90
|
var items = value.map(function (v) {
|
|
80
|
-
return annotate(v, undefined, seen)
|
|
81
|
-
})
|
|
82
|
-
return array(items, text)
|
|
91
|
+
return annotate(v, undefined, seen)
|
|
92
|
+
})
|
|
93
|
+
return array(items, text)
|
|
83
94
|
}
|
|
84
95
|
|
|
85
96
|
function annotateObject(obj, text, seen) {
|
|
86
|
-
seen.add(obj)
|
|
87
|
-
var fields = {}
|
|
97
|
+
seen.add(obj)
|
|
98
|
+
var fields = {}
|
|
88
99
|
Object.keys(obj).forEach(function (key) {
|
|
89
|
-
var value = obj[key]
|
|
90
|
-
fields[key] = annotate(value, undefined, seen)
|
|
91
|
-
})
|
|
92
|
-
return object(fields, text)
|
|
100
|
+
var value = obj[key]
|
|
101
|
+
fields[key] = annotate(value, undefined, seen)
|
|
102
|
+
})
|
|
103
|
+
return object(fields, text)
|
|
93
104
|
}
|
|
94
105
|
|
|
95
106
|
function annotate(value, text, seen) {
|
|
96
107
|
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)
|
|
108
|
+
return scalar(value, text)
|
|
98
109
|
}
|
|
99
110
|
|
|
100
|
-
var ann = asAnnotation(value)
|
|
111
|
+
var ann = asAnnotation(value)
|
|
101
112
|
|
|
102
113
|
if (ann) {
|
|
103
|
-
return updateText(ann, text)
|
|
114
|
+
return updateText(ann, text)
|
|
104
115
|
}
|
|
105
116
|
|
|
106
117
|
if (Array.isArray(value)) {
|
|
107
|
-
// "Circular references" can only exist in objects or arrays
|
|
108
118
|
if (seen.has(value)) {
|
|
109
|
-
return circularRef(text)
|
|
119
|
+
return circularRef(text)
|
|
110
120
|
} else {
|
|
111
|
-
return annotateArray(value, text, seen)
|
|
121
|
+
return annotateArray(value, text, seen)
|
|
112
122
|
}
|
|
113
123
|
}
|
|
114
124
|
|
|
115
125
|
if (typeof value === 'object') {
|
|
116
|
-
// "Circular references" can only exist in objects or arrays
|
|
117
126
|
if (seen.has(value)) {
|
|
118
|
-
return circularRef(text)
|
|
127
|
+
return circularRef(text)
|
|
119
128
|
} else {
|
|
120
|
-
return annotateObject(value, text, seen)
|
|
129
|
+
return annotateObject(value, text, seen)
|
|
121
130
|
}
|
|
122
131
|
}
|
|
123
132
|
|
|
124
133
|
if (typeof value === 'function') {
|
|
125
|
-
return func(text)
|
|
134
|
+
return func(text)
|
|
126
135
|
}
|
|
127
136
|
|
|
128
|
-
return unknown(value, text)
|
|
137
|
+
return unknown(value, text)
|
|
129
138
|
}
|
|
130
139
|
|
|
131
140
|
function public_annotate(value, text) {
|
|
132
|
-
return annotate(value, text, new WeakSet())
|
|
141
|
+
return annotate(value, text, new WeakSet())
|
|
133
142
|
}
|
|
134
143
|
|
|
135
144
|
function public_annotateObject(obj, text) {
|
|
136
|
-
return annotateObject(obj, text, new WeakSet())
|
|
145
|
+
return annotateObject(obj, text, new WeakSet())
|
|
137
146
|
}
|
|
138
147
|
|
|
139
|
-
export {
|
|
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 };
|
|
148
|
+
export { public_annotate as annotate, public_annotateObject as annotateObject, annotate as __private_annotate }
|
package/format.js
CHANGED
|
@@ -1,151 +1,141 @@
|
|
|
1
|
-
|
|
1
|
+
'use strict'
|
|
2
2
|
|
|
3
|
-
exports.__esModule = true
|
|
4
|
-
exports.formatInline = formatInline
|
|
5
|
-
exports.formatShort = formatShort
|
|
6
|
-
exports.serializeAnnotation = serializeAnnotation
|
|
7
|
-
exports.serializeValue = serializeValue
|
|
3
|
+
exports.__esModule = true
|
|
4
|
+
exports.formatInline = formatInline
|
|
5
|
+
exports.formatShort = formatShort
|
|
6
|
+
exports.serializeAnnotation = serializeAnnotation
|
|
7
|
+
exports.serializeValue = serializeValue
|
|
8
8
|
|
|
9
|
-
var _utils = require(
|
|
9
|
+
var _utils = require('./_utils')
|
|
10
10
|
|
|
11
11
|
function serializeString(s, width) {
|
|
12
12
|
if (width === void 0) {
|
|
13
|
-
width = 80
|
|
13
|
+
width = 80
|
|
14
14
|
}
|
|
15
|
-
|
|
16
|
-
// Full string
|
|
17
|
-
// Abbreviated to $maxlen i.e. "Vincent Driess..." [truncated]
|
|
18
|
-
var ser = JSON.stringify(s);
|
|
15
|
+
var ser = JSON.stringify(s)
|
|
19
16
|
|
|
20
17
|
if (ser.length <= width) {
|
|
21
|
-
return ser
|
|
22
|
-
}
|
|
23
|
-
|
|
18
|
+
return ser
|
|
19
|
+
}
|
|
24
20
|
|
|
25
|
-
var truncated = s.substring(0, width - 15) + '...'
|
|
26
|
-
ser = JSON.stringify(truncated) + ' [truncated]'
|
|
27
|
-
return ser
|
|
21
|
+
var truncated = s.substring(0, width - 15) + '...'
|
|
22
|
+
ser = JSON.stringify(truncated) + ' [truncated]'
|
|
23
|
+
return ser
|
|
28
24
|
}
|
|
29
25
|
|
|
30
26
|
function serializeArray(annotation, prefix) {
|
|
31
|
-
var items = annotation.items
|
|
27
|
+
var items = annotation.items
|
|
32
28
|
|
|
33
29
|
if (items.length === 0) {
|
|
34
|
-
return '[]'
|
|
30
|
+
return '[]'
|
|
35
31
|
}
|
|
36
32
|
|
|
37
|
-
var result = []
|
|
33
|
+
var result = []
|
|
38
34
|
items.forEach(function (item) {
|
|
39
|
-
var _serializeAnnotation = serializeAnnotation(item, prefix + _utils.INDENT),
|
|
40
|
-
|
|
41
|
-
|
|
35
|
+
var _serializeAnnotation = serializeAnnotation(item, '' + prefix + _utils.INDENT),
|
|
36
|
+
ser = _serializeAnnotation[0],
|
|
37
|
+
ann = _serializeAnnotation[1]
|
|
42
38
|
|
|
43
|
-
result.push(prefix + _utils.INDENT + ser + ',')
|
|
39
|
+
result.push('' + prefix + _utils.INDENT + ser + ',')
|
|
44
40
|
|
|
45
41
|
if (ann !== undefined) {
|
|
46
|
-
result.push((0, _utils.indent)(ann, prefix + _utils.INDENT))
|
|
42
|
+
result.push((0, _utils.indent)(ann, '' + prefix + _utils.INDENT))
|
|
47
43
|
}
|
|
48
|
-
})
|
|
49
|
-
return ['['].concat(result, [prefix + ']']).join('\n')
|
|
44
|
+
})
|
|
45
|
+
return ['['].concat(result, [prefix + ']']).join('\n')
|
|
50
46
|
}
|
|
51
47
|
|
|
52
48
|
function serializeObject(annotation, prefix) {
|
|
53
|
-
var fields = annotation.fields
|
|
54
|
-
var fieldNames = Object.keys(fields)
|
|
49
|
+
var fields = annotation.fields
|
|
50
|
+
var fieldNames = Object.keys(fields)
|
|
55
51
|
|
|
56
52
|
if (fieldNames.length === 0) {
|
|
57
|
-
return '{}'
|
|
53
|
+
return '{}'
|
|
58
54
|
}
|
|
59
55
|
|
|
60
|
-
var result = []
|
|
56
|
+
var result = []
|
|
61
57
|
fieldNames.forEach(function (key) {
|
|
62
|
-
var valueAnnotation = fields[key]
|
|
63
|
-
var kser = serializeValue(key)
|
|
64
|
-
var valPrefix = prefix + _utils.INDENT + ' '.repeat(kser.length + 2)
|
|
58
|
+
var valueAnnotation = fields[key]
|
|
59
|
+
var kser = serializeValue(key)
|
|
60
|
+
var valPrefix = '' + prefix + _utils.INDENT + ' '.repeat(kser.length + 2)
|
|
65
61
|
|
|
66
|
-
var _serializeAnnotation2 = serializeAnnotation(valueAnnotation, prefix + _utils.INDENT),
|
|
67
|
-
|
|
68
|
-
|
|
62
|
+
var _serializeAnnotation2 = serializeAnnotation(valueAnnotation, '' + prefix + _utils.INDENT),
|
|
63
|
+
vser = _serializeAnnotation2[0],
|
|
64
|
+
vann = _serializeAnnotation2[1]
|
|
69
65
|
|
|
70
|
-
result.push(prefix + _utils.INDENT + kser + ': ' + vser + ',')
|
|
66
|
+
result.push('' + prefix + _utils.INDENT + kser + ': ' + vser + ',')
|
|
71
67
|
|
|
72
68
|
if (vann !== undefined) {
|
|
73
|
-
result.push((0, _utils.indent)(vann, valPrefix))
|
|
69
|
+
result.push((0, _utils.indent)(vann, valPrefix))
|
|
74
70
|
}
|
|
75
|
-
})
|
|
76
|
-
return ['{'].concat(result, [prefix + '}']).join('\n')
|
|
71
|
+
})
|
|
72
|
+
return ['{'].concat(result, [prefix + '}']).join('\n')
|
|
77
73
|
}
|
|
78
74
|
|
|
79
75
|
function serializeValue(value) {
|
|
80
|
-
// istanbul ignore else
|
|
81
76
|
if (typeof value === 'string') {
|
|
82
|
-
return serializeString(value)
|
|
77
|
+
return serializeString(value)
|
|
83
78
|
} else if (typeof value === 'number' || typeof value === 'boolean') {
|
|
84
|
-
return value.toString()
|
|
79
|
+
return value.toString()
|
|
85
80
|
} else if (value === null) {
|
|
86
|
-
return 'null'
|
|
81
|
+
return 'null'
|
|
87
82
|
} else if (value === undefined) {
|
|
88
|
-
return 'undefined'
|
|
83
|
+
return 'undefined'
|
|
89
84
|
} else {
|
|
90
|
-
var valueAsDate = (0, _utils.asDate)(value)
|
|
85
|
+
var valueAsDate = (0, _utils.asDate)(value)
|
|
91
86
|
|
|
92
87
|
if (valueAsDate !== null) {
|
|
93
|
-
return
|
|
88
|
+
return 'new Date(' + JSON.stringify(valueAsDate.toISOString()) + ')'
|
|
94
89
|
} else if (value instanceof Date) {
|
|
95
|
-
|
|
96
|
-
// If this case occurs (and it didn't pass the prior asDate())
|
|
97
|
-
// check, then this must be the case where it's an invalid date.
|
|
98
|
-
return '(Invalid Date)';
|
|
90
|
+
return '(Invalid Date)'
|
|
99
91
|
} else {
|
|
100
|
-
return '(unserializable)'
|
|
92
|
+
return '(unserializable)'
|
|
101
93
|
}
|
|
102
94
|
}
|
|
103
95
|
}
|
|
104
96
|
|
|
105
97
|
function serializeAnnotation(ann, prefix) {
|
|
106
98
|
if (prefix === void 0) {
|
|
107
|
-
prefix = ''
|
|
99
|
+
prefix = ''
|
|
108
100
|
}
|
|
109
|
-
|
|
110
|
-
// The serialized data (the input object echoed back)
|
|
111
|
-
var serialized;
|
|
101
|
+
var serialized
|
|
112
102
|
|
|
113
103
|
if (ann.type === 'array') {
|
|
114
|
-
serialized = serializeArray(ann, prefix)
|
|
104
|
+
serialized = serializeArray(ann, prefix)
|
|
115
105
|
} else if (ann.type === 'object') {
|
|
116
|
-
serialized = serializeObject(ann, prefix)
|
|
106
|
+
serialized = serializeObject(ann, prefix)
|
|
117
107
|
} else if (ann.type === 'function') {
|
|
118
|
-
serialized = '<function>'
|
|
108
|
+
serialized = '<function>'
|
|
119
109
|
} else if (ann.type === 'circular-ref') {
|
|
120
|
-
serialized = '<circular ref>'
|
|
110
|
+
serialized = '<circular ref>'
|
|
121
111
|
} else if (ann.type === 'unknown') {
|
|
122
|
-
serialized = '???'
|
|
112
|
+
serialized = '???'
|
|
123
113
|
} else {
|
|
124
|
-
serialized = serializeValue(ann.value)
|
|
114
|
+
serialized = serializeValue(ann.value)
|
|
125
115
|
}
|
|
126
116
|
|
|
127
|
-
var text = ann.text
|
|
117
|
+
var text = ann.text
|
|
128
118
|
|
|
129
119
|
if (text !== undefined) {
|
|
130
|
-
var sep = '^'.repeat((0, _utils.isMultiline)(serialized) ? 1 : serialized.length)
|
|
131
|
-
return [serialized, [sep, text].join((0, _utils.isMultiline)(text) ? '\n' : ' ')]
|
|
120
|
+
var sep = '^'.repeat((0, _utils.isMultiline)(serialized) ? 1 : serialized.length)
|
|
121
|
+
return [serialized, [sep, text].join((0, _utils.isMultiline)(text) ? '\n' : ' ')]
|
|
132
122
|
} else {
|
|
133
|
-
return [serialized, undefined]
|
|
123
|
+
return [serialized, undefined]
|
|
134
124
|
}
|
|
135
125
|
}
|
|
136
126
|
|
|
137
127
|
function formatInline(ann) {
|
|
138
128
|
var _serializeAnnotation3 = serializeAnnotation(ann),
|
|
139
|
-
|
|
140
|
-
|
|
129
|
+
serialized = _serializeAnnotation3[0],
|
|
130
|
+
annotation = _serializeAnnotation3[1]
|
|
141
131
|
|
|
142
132
|
if (annotation !== undefined) {
|
|
143
|
-
return serialized + '\n' + annotation
|
|
133
|
+
return serialized + '\n' + annotation
|
|
144
134
|
} else {
|
|
145
|
-
return serialized
|
|
135
|
+
return serialized
|
|
146
136
|
}
|
|
147
137
|
}
|
|
148
138
|
|
|
149
139
|
function formatShort(ann) {
|
|
150
|
-
return (0, _utils.summarize)(ann, []).join('\n')
|
|
151
|
-
}
|
|
140
|
+
return (0, _utils.summarize)(ann, []).join('\n')
|
|
141
|
+
}
|
package/format.js.flow
CHANGED
|
@@ -14,8 +14,8 @@ function serializeString(s: string, width: number = 80): string {
|
|
|
14
14
|
}
|
|
15
15
|
|
|
16
16
|
// Cut off a bit
|
|
17
|
-
const truncated = s.substring(0, width - 15)
|
|
18
|
-
ser = JSON.stringify(truncated)
|
|
17
|
+
const truncated = `${s.substring(0, width - 15)}...`;
|
|
18
|
+
ser = `${JSON.stringify(truncated)} [truncated]`;
|
|
19
19
|
return ser;
|
|
20
20
|
}
|
|
21
21
|
|
|
@@ -27,13 +27,13 @@ function serializeArray(annotation: ArrayAnnotation, prefix: string): string {
|
|
|
27
27
|
|
|
28
28
|
const result = [];
|
|
29
29
|
items.forEach((item) => {
|
|
30
|
-
const [ser, ann] = serializeAnnotation(item, prefix
|
|
31
|
-
result.push(prefix
|
|
30
|
+
const [ser, ann] = serializeAnnotation(item, `${prefix}${INDENT}`);
|
|
31
|
+
result.push(`${prefix}${INDENT}${ser}${','}`);
|
|
32
32
|
if (ann !== undefined) {
|
|
33
|
-
result.push(indent(ann, prefix
|
|
33
|
+
result.push(indent(ann, `${prefix}${INDENT}`));
|
|
34
34
|
}
|
|
35
35
|
});
|
|
36
|
-
return ['[', ...result, prefix
|
|
36
|
+
return ['[', ...result, `${prefix}]`].join('\n');
|
|
37
37
|
}
|
|
38
38
|
|
|
39
39
|
function serializeObject(annotation: ObjectAnnotation, prefix: string): string {
|
|
@@ -49,15 +49,15 @@ function serializeObject(annotation: ObjectAnnotation, prefix: string): string {
|
|
|
49
49
|
const valueAnnotation = fields[key];
|
|
50
50
|
const kser = serializeValue(key);
|
|
51
51
|
|
|
52
|
-
const valPrefix = prefix
|
|
53
|
-
const [vser, vann] = serializeAnnotation(valueAnnotation, prefix
|
|
52
|
+
const valPrefix = `${prefix}${INDENT}${' '.repeat(kser.length + 2)}`;
|
|
53
|
+
const [vser, vann] = serializeAnnotation(valueAnnotation, `${prefix}${INDENT}`);
|
|
54
54
|
|
|
55
|
-
result.push(prefix
|
|
55
|
+
result.push(`${prefix}${INDENT}${kser}: ${vser},`);
|
|
56
56
|
if (vann !== undefined) {
|
|
57
57
|
result.push(indent(vann, valPrefix));
|
|
58
58
|
}
|
|
59
59
|
});
|
|
60
|
-
return ['{', ...result, prefix
|
|
60
|
+
return ['{', ...result, `${prefix}}`].join('\n');
|
|
61
61
|
}
|
|
62
62
|
|
|
63
63
|
export function serializeValue(value: mixed): string {
|
|
@@ -117,7 +117,7 @@ export function serializeAnnotation(
|
|
|
117
117
|
export function formatInline(ann: Annotation): string {
|
|
118
118
|
const [serialized, annotation] = serializeAnnotation(ann);
|
|
119
119
|
if (annotation !== undefined) {
|
|
120
|
-
return serialized
|
|
120
|
+
return `${serialized}\n${annotation}`;
|
|
121
121
|
} else {
|
|
122
122
|
return serialized;
|
|
123
123
|
}
|
package/format.mjs
CHANGED
|
@@ -1,140 +1,130 @@
|
|
|
1
|
-
import { summarize as _summarize, asDate, INDENT, indent, isMultiline } from './_utils.mjs'
|
|
1
|
+
import { summarize as _summarize, asDate, INDENT, indent, isMultiline } from './_utils.mjs'
|
|
2
2
|
|
|
3
3
|
function serializeString(s, width) {
|
|
4
4
|
if (width === void 0) {
|
|
5
|
-
width = 80
|
|
5
|
+
width = 80
|
|
6
6
|
}
|
|
7
|
-
|
|
8
|
-
// Full string
|
|
9
|
-
// Abbreviated to $maxlen i.e. "Vincent Driess..." [truncated]
|
|
10
|
-
var ser = JSON.stringify(s);
|
|
7
|
+
var ser = JSON.stringify(s)
|
|
11
8
|
|
|
12
9
|
if (ser.length <= width) {
|
|
13
|
-
return ser
|
|
14
|
-
}
|
|
15
|
-
|
|
10
|
+
return ser
|
|
11
|
+
}
|
|
16
12
|
|
|
17
|
-
var truncated = s.substring(0, width - 15) + '...'
|
|
18
|
-
ser = JSON.stringify(truncated) + ' [truncated]'
|
|
19
|
-
return ser
|
|
13
|
+
var truncated = s.substring(0, width - 15) + '...'
|
|
14
|
+
ser = JSON.stringify(truncated) + ' [truncated]'
|
|
15
|
+
return ser
|
|
20
16
|
}
|
|
21
17
|
|
|
22
18
|
function serializeArray(annotation, prefix) {
|
|
23
|
-
var items = annotation.items
|
|
19
|
+
var items = annotation.items
|
|
24
20
|
|
|
25
21
|
if (items.length === 0) {
|
|
26
|
-
return '[]'
|
|
22
|
+
return '[]'
|
|
27
23
|
}
|
|
28
24
|
|
|
29
|
-
var result = []
|
|
25
|
+
var result = []
|
|
30
26
|
items.forEach(function (item) {
|
|
31
|
-
var _serializeAnnotation = serializeAnnotation(item, prefix + INDENT),
|
|
32
|
-
|
|
33
|
-
|
|
27
|
+
var _serializeAnnotation = serializeAnnotation(item, '' + prefix + INDENT),
|
|
28
|
+
ser = _serializeAnnotation[0],
|
|
29
|
+
ann = _serializeAnnotation[1]
|
|
34
30
|
|
|
35
|
-
result.push(prefix + INDENT + ser + ',')
|
|
31
|
+
result.push('' + prefix + INDENT + ser + ',')
|
|
36
32
|
|
|
37
33
|
if (ann !== undefined) {
|
|
38
|
-
result.push(indent(ann, prefix + INDENT))
|
|
34
|
+
result.push(indent(ann, '' + prefix + INDENT))
|
|
39
35
|
}
|
|
40
|
-
})
|
|
41
|
-
return ['['].concat(result, [prefix + ']']).join('\n')
|
|
36
|
+
})
|
|
37
|
+
return ['['].concat(result, [prefix + ']']).join('\n')
|
|
42
38
|
}
|
|
43
39
|
|
|
44
40
|
function serializeObject(annotation, prefix) {
|
|
45
|
-
var fields = annotation.fields
|
|
46
|
-
var fieldNames = Object.keys(fields)
|
|
41
|
+
var fields = annotation.fields
|
|
42
|
+
var fieldNames = Object.keys(fields)
|
|
47
43
|
|
|
48
44
|
if (fieldNames.length === 0) {
|
|
49
|
-
return '{}'
|
|
45
|
+
return '{}'
|
|
50
46
|
}
|
|
51
47
|
|
|
52
|
-
var result = []
|
|
48
|
+
var result = []
|
|
53
49
|
fieldNames.forEach(function (key) {
|
|
54
|
-
var valueAnnotation = fields[key]
|
|
55
|
-
var kser = serializeValue(key)
|
|
56
|
-
var valPrefix = prefix + INDENT + ' '.repeat(kser.length + 2)
|
|
50
|
+
var valueAnnotation = fields[key]
|
|
51
|
+
var kser = serializeValue(key)
|
|
52
|
+
var valPrefix = '' + prefix + INDENT + ' '.repeat(kser.length + 2)
|
|
57
53
|
|
|
58
|
-
var _serializeAnnotation2 = serializeAnnotation(valueAnnotation, prefix + INDENT),
|
|
59
|
-
|
|
60
|
-
|
|
54
|
+
var _serializeAnnotation2 = serializeAnnotation(valueAnnotation, '' + prefix + INDENT),
|
|
55
|
+
vser = _serializeAnnotation2[0],
|
|
56
|
+
vann = _serializeAnnotation2[1]
|
|
61
57
|
|
|
62
|
-
result.push(prefix + INDENT + kser + ': ' + vser + ',')
|
|
58
|
+
result.push('' + prefix + INDENT + kser + ': ' + vser + ',')
|
|
63
59
|
|
|
64
60
|
if (vann !== undefined) {
|
|
65
|
-
result.push(indent(vann, valPrefix))
|
|
61
|
+
result.push(indent(vann, valPrefix))
|
|
66
62
|
}
|
|
67
|
-
})
|
|
68
|
-
return ['{'].concat(result, [prefix + '}']).join('\n')
|
|
63
|
+
})
|
|
64
|
+
return ['{'].concat(result, [prefix + '}']).join('\n')
|
|
69
65
|
}
|
|
70
66
|
|
|
71
67
|
export function serializeValue(value) {
|
|
72
|
-
// istanbul ignore else
|
|
73
68
|
if (typeof value === 'string') {
|
|
74
|
-
return serializeString(value)
|
|
69
|
+
return serializeString(value)
|
|
75
70
|
} else if (typeof value === 'number' || typeof value === 'boolean') {
|
|
76
|
-
return value.toString()
|
|
71
|
+
return value.toString()
|
|
77
72
|
} else if (value === null) {
|
|
78
|
-
return 'null'
|
|
73
|
+
return 'null'
|
|
79
74
|
} else if (value === undefined) {
|
|
80
|
-
return 'undefined'
|
|
75
|
+
return 'undefined'
|
|
81
76
|
} else {
|
|
82
|
-
var valueAsDate = asDate(value)
|
|
77
|
+
var valueAsDate = asDate(value)
|
|
83
78
|
|
|
84
79
|
if (valueAsDate !== null) {
|
|
85
|
-
return
|
|
80
|
+
return 'new Date(' + JSON.stringify(valueAsDate.toISOString()) + ')'
|
|
86
81
|
} else if (value instanceof Date) {
|
|
87
|
-
|
|
88
|
-
// If this case occurs (and it didn't pass the prior asDate())
|
|
89
|
-
// check, then this must be the case where it's an invalid date.
|
|
90
|
-
return '(Invalid Date)';
|
|
82
|
+
return '(Invalid Date)'
|
|
91
83
|
} else {
|
|
92
|
-
return '(unserializable)'
|
|
84
|
+
return '(unserializable)'
|
|
93
85
|
}
|
|
94
86
|
}
|
|
95
87
|
}
|
|
96
88
|
export function serializeAnnotation(ann, prefix) {
|
|
97
89
|
if (prefix === void 0) {
|
|
98
|
-
prefix = ''
|
|
90
|
+
prefix = ''
|
|
99
91
|
}
|
|
100
|
-
|
|
101
|
-
// The serialized data (the input object echoed back)
|
|
102
|
-
var serialized;
|
|
92
|
+
var serialized
|
|
103
93
|
|
|
104
94
|
if (ann.type === 'array') {
|
|
105
|
-
serialized = serializeArray(ann, prefix)
|
|
95
|
+
serialized = serializeArray(ann, prefix)
|
|
106
96
|
} else if (ann.type === 'object') {
|
|
107
|
-
serialized = serializeObject(ann, prefix)
|
|
97
|
+
serialized = serializeObject(ann, prefix)
|
|
108
98
|
} else if (ann.type === 'function') {
|
|
109
|
-
serialized = '<function>'
|
|
99
|
+
serialized = '<function>'
|
|
110
100
|
} else if (ann.type === 'circular-ref') {
|
|
111
|
-
serialized = '<circular ref>'
|
|
101
|
+
serialized = '<circular ref>'
|
|
112
102
|
} else if (ann.type === 'unknown') {
|
|
113
|
-
serialized = '???'
|
|
103
|
+
serialized = '???'
|
|
114
104
|
} else {
|
|
115
|
-
serialized = serializeValue(ann.value)
|
|
105
|
+
serialized = serializeValue(ann.value)
|
|
116
106
|
}
|
|
117
107
|
|
|
118
|
-
var text = ann.text
|
|
108
|
+
var text = ann.text
|
|
119
109
|
|
|
120
110
|
if (text !== undefined) {
|
|
121
|
-
var sep = '^'.repeat(isMultiline(serialized) ? 1 : serialized.length)
|
|
122
|
-
return [serialized, [sep, text].join(isMultiline(text) ? '\n' : ' ')]
|
|
111
|
+
var sep = '^'.repeat(isMultiline(serialized) ? 1 : serialized.length)
|
|
112
|
+
return [serialized, [sep, text].join(isMultiline(text) ? '\n' : ' ')]
|
|
123
113
|
} else {
|
|
124
|
-
return [serialized, undefined]
|
|
114
|
+
return [serialized, undefined]
|
|
125
115
|
}
|
|
126
116
|
}
|
|
127
117
|
export function formatInline(ann) {
|
|
128
118
|
var _serializeAnnotation3 = serializeAnnotation(ann),
|
|
129
|
-
|
|
130
|
-
|
|
119
|
+
serialized = _serializeAnnotation3[0],
|
|
120
|
+
annotation = _serializeAnnotation3[1]
|
|
131
121
|
|
|
132
122
|
if (annotation !== undefined) {
|
|
133
|
-
return serialized + '\n' + annotation
|
|
123
|
+
return serialized + '\n' + annotation
|
|
134
124
|
} else {
|
|
135
|
-
return serialized
|
|
125
|
+
return serialized
|
|
136
126
|
}
|
|
137
127
|
}
|
|
138
128
|
export function formatShort(ann) {
|
|
139
|
-
return _summarize(ann, []).join('\n')
|
|
140
|
-
}
|
|
129
|
+
return _summarize(ann, []).join('\n')
|
|
130
|
+
}
|