decoders 2.0.0 → 2.0.2

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/_utils.js CHANGED
@@ -1,102 +1,91 @@
1
- "use strict";
1
+ 'use strict'
2
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
- */
3
+ exports.__esModule = true
4
+ exports.INDENT = void 0
5
+ exports.asDate = asDate
6
+ exports.indent = indent
7
+ exports.isMultiline = isMultiline
8
+ exports.subtract = subtract
9
+ exports.summarize = summarize
10
+ var INDENT = ' '
16
11
 
17
- exports.INDENT = INDENT;
12
+ exports.INDENT = INDENT
13
+
14
+ function subtract(xs, ys) {
15
+ var result = new Set()
16
+ xs.forEach(function (x) {
17
+ if (!ys.has(x)) {
18
+ result.add(x)
19
+ }
20
+ })
21
+ return result
22
+ }
18
23
 
19
24
  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;
25
+ return !!value && Object.prototype.toString.call(value) === '[object Date]' && !isNaN(value) ? value : null
29
26
  }
30
27
 
31
28
  function isMultiline(s) {
32
- return s.indexOf('\n') >= 0;
29
+ return s.indexOf('\n') >= 0
33
30
  }
34
31
 
35
32
  function indent(s, prefix) {
36
33
  if (prefix === void 0) {
37
- prefix = INDENT;
34
+ prefix = INDENT
38
35
  }
39
36
 
40
37
  if (isMultiline(s)) {
41
- return s.split('\n').map(function (line) {
42
- return prefix + line;
43
- }).join('\n');
38
+ return s
39
+ .split('\n')
40
+ .map(function (line) {
41
+ return '' + prefix + line
42
+ })
43
+ .join('\n')
44
44
  } else {
45
- return prefix + s;
45
+ return '' + prefix + s
46
46
  }
47
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
48
 
54
49
  function summarize(ann, keypath) {
55
50
  if (keypath === void 0) {
56
- keypath = [];
51
+ keypath = []
57
52
  }
58
53
 
59
- var result = [];
54
+ var result = []
60
55
 
61
56
  if (ann.type === 'array') {
62
- var items = ann.items;
63
- var index = 0;
57
+ var items = ann.items
58
+ var index = 0
64
59
  items.forEach(function (ann) {
65
60
  summarize(ann, [].concat(keypath, [index++])).forEach(function (item) {
66
- return (// Collect to results
67
- result.push(item)
68
- );
69
- });
70
- });
61
+ return result.push(item)
62
+ })
63
+ })
71
64
  } else if (ann.type === 'object') {
72
- var fields = ann.fields;
65
+ var fields = ann.fields
73
66
  Object.keys(fields).forEach(function (key) {
74
- var value = fields[key];
67
+ var value = fields[key]
75
68
  summarize(value, [].concat(keypath, [key])).forEach(function (item) {
76
- return (// Collect to results
77
- result.push(item)
78
- );
79
- });
80
- });
69
+ return result.push(item)
70
+ })
71
+ })
81
72
  }
82
73
 
83
- var text = ann.text;
74
+ var text = ann.text
84
75
 
85
76
  if (!text) {
86
- return result;
77
+ return result
87
78
  }
88
79
 
89
- var prefix;
80
+ var prefix
90
81
 
91
82
  if (keypath.length === 0) {
92
- prefix = '';
83
+ prefix = ''
93
84
  } else if (keypath.length === 1) {
94
- prefix = typeof keypath[0] === 'number' ? "Value at index " + keypath[0] + ": " : "Value at key " + JSON.stringify(keypath[0]) + ": ";
85
+ prefix = typeof keypath[0] === 'number' ? 'Value at index ' + keypath[0] + ': ' : 'Value at key ' + JSON.stringify(keypath[0]) + ': '
95
86
  } else {
96
- prefix = "Value at keypath " + keypath.map(function (x) {
97
- return x.toString();
98
- }).join('.') + ": ";
87
+ prefix = 'Value at keypath ' + keypath.map(String).join('.') + ': '
99
88
  }
100
89
 
101
- return [].concat(result, [prefix + text]);
102
- }
90
+ return [].concat(result, ['' + prefix + text])
91
+ }
package/_utils.js.flow CHANGED
@@ -1,6 +1,7 @@
1
1
  // @flow strict
2
2
 
3
3
  import type { Annotation } from './annotate';
4
+ import type { Scalar } from './Decoder';
4
5
 
5
6
  // $FlowFixMe[unclear-type] - deliberate use of `any`
6
7
  export type _Any = any;
@@ -8,6 +9,19 @@ export type _Any = any;
8
9
  // Two spaces of indentation
9
10
  export const INDENT = ' ';
10
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
+
11
25
  /**
12
26
  * Is value is a valid Date instance, then return that. If not, then return
13
27
  * null.
@@ -36,10 +50,10 @@ export function indent(s: string, prefix: string = INDENT): string {
36
50
  if (isMultiline(s)) {
37
51
  return s
38
52
  .split('\n')
39
- .map((line) => prefix + line)
53
+ .map((line) => `${prefix}${line}`)
40
54
  .join('\n');
41
55
  } else {
42
- return prefix + s;
56
+ return `${prefix}${s}`;
43
57
  }
44
58
  }
45
59
 
@@ -87,7 +101,7 @@ export function summarize(
87
101
  ? `Value at index ${keypath[0]}: `
88
102
  : `Value at key ${JSON.stringify(keypath[0])}: `;
89
103
  } else {
90
- prefix = `Value at keypath ${keypath.map((x) => x.toString()).join('.')}: `;
104
+ prefix = `Value at keypath ${keypath.map(String).join('.')}: `;
91
105
  }
92
- return [...result, prefix + text];
106
+ return [...result, `${prefix}${text}`];
93
107
  }
package/_utils.mjs CHANGED
@@ -1,89 +1,78 @@
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
- */
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
+ }
8
12
 
9
13
  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;
14
+ return !!value && Object.prototype.toString.call(value) === '[object Date]' && !isNaN(value) ? value : null
19
15
  }
20
16
  export function isMultiline(s) {
21
- return s.indexOf('\n') >= 0;
17
+ return s.indexOf('\n') >= 0
22
18
  }
23
19
  export function indent(s, prefix) {
24
20
  if (prefix === void 0) {
25
- prefix = INDENT;
21
+ prefix = INDENT
26
22
  }
27
23
 
28
24
  if (isMultiline(s)) {
29
- return s.split('\n').map(function (line) {
30
- return prefix + line;
31
- }).join('\n');
25
+ return s
26
+ .split('\n')
27
+ .map(function (line) {
28
+ return '' + prefix + line
29
+ })
30
+ .join('\n')
32
31
  } else {
33
- return prefix + s;
32
+ return '' + prefix + s
34
33
  }
35
34
  }
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
35
 
41
36
  export function summarize(ann, keypath) {
42
37
  if (keypath === void 0) {
43
- keypath = [];
38
+ keypath = []
44
39
  }
45
40
 
46
- var result = [];
41
+ var result = []
47
42
 
48
43
  if (ann.type === 'array') {
49
- var items = ann.items;
50
- var index = 0;
44
+ var items = ann.items
45
+ var index = 0
51
46
  items.forEach(function (ann) {
52
47
  summarize(ann, [].concat(keypath, [index++])).forEach(function (item) {
53
- return (// Collect to results
54
- result.push(item)
55
- );
56
- });
57
- });
48
+ return result.push(item)
49
+ })
50
+ })
58
51
  } else if (ann.type === 'object') {
59
- var fields = ann.fields;
52
+ var fields = ann.fields
60
53
  Object.keys(fields).forEach(function (key) {
61
- var value = fields[key];
54
+ var value = fields[key]
62
55
  summarize(value, [].concat(keypath, [key])).forEach(function (item) {
63
- return (// Collect to results
64
- result.push(item)
65
- );
66
- });
67
- });
56
+ return result.push(item)
57
+ })
58
+ })
68
59
  }
69
60
 
70
- var text = ann.text;
61
+ var text = ann.text
71
62
 
72
63
  if (!text) {
73
- return result;
64
+ return result
74
65
  }
75
66
 
76
- var prefix;
67
+ var prefix
77
68
 
78
69
  if (keypath.length === 0) {
79
- prefix = '';
70
+ prefix = ''
80
71
  } else if (keypath.length === 1) {
81
- prefix = typeof keypath[0] === 'number' ? "Value at index " + keypath[0] + ": " : "Value at key " + JSON.stringify(keypath[0]) + ": ";
72
+ prefix = typeof keypath[0] === 'number' ? 'Value at index ' + keypath[0] + ': ' : 'Value at key ' + JSON.stringify(keypath[0]) + ': '
82
73
  } else {
83
- prefix = "Value at keypath " + keypath.map(function (x) {
84
- return x.toString();
85
- }).join('.') + ": ";
74
+ prefix = 'Value at keypath ' + keypath.map(String).join('.') + ': '
86
75
  }
87
76
 
88
- return [].concat(result, [prefix + text]);
89
- }
77
+ return [].concat(result, ['' + prefix + text])
78
+ }
package/annotate.js CHANGED
@@ -1,161 +1,168 @@
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();
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() {
18
+ _extends = Object.assign
19
+ ? Object.assign.bind()
20
+ : function (target) {
21
+ for (var i = 1; i < arguments.length; i++) {
22
+ var source = arguments[i]
23
+ for (var key in source) {
24
+ if (Object.prototype.hasOwnProperty.call(source, key)) {
25
+ target[key] = source[key]
26
+ }
27
+ }
28
+ }
29
+ return target
30
+ }
31
+ return _extends.apply(this, arguments)
32
+ }
33
+
34
+ var _register = new WeakSet()
20
35
 
21
36
  function brand(ann) {
22
- _register.add(ann);
37
+ _register.add(ann)
23
38
 
24
- return ann;
39
+ return ann
25
40
  }
26
41
 
27
42
  function object(fields, text) {
28
43
  return brand({
29
44
  type: 'object',
30
45
  fields: fields,
31
- text: text
32
- });
46
+ text: text,
47
+ })
33
48
  }
34
49
 
35
50
  function array(items, text) {
36
51
  return brand({
37
52
  type: 'array',
38
53
  items: items,
39
- text: text
40
- });
54
+ text: text,
55
+ })
41
56
  }
42
57
 
43
58
  function func(text) {
44
59
  return brand({
45
60
  type: 'function',
46
- text: text
47
- });
61
+ text: text,
62
+ })
48
63
  }
49
64
 
50
65
  function unknown(value, text) {
51
66
  return brand({
52
67
  type: 'unknown',
53
68
  value: value,
54
- text: text
55
- });
69
+ text: text,
70
+ })
56
71
  }
57
72
 
58
73
  function scalar(value, text) {
59
74
  return brand({
60
75
  type: 'scalar',
61
76
  value: value,
62
- text: text
63
- });
77
+ text: text,
78
+ })
64
79
  }
65
80
 
66
81
  function circularRef(text) {
67
82
  return brand({
68
83
  type: 'circular-ref',
69
- text: text
70
- });
84
+ text: text,
85
+ })
71
86
  }
72
- /**
73
- * Given an existing Annotation, set the annotation's text to a new value.
74
- */
75
-
76
87
 
77
88
  function updateText(annotation, text) {
78
89
  if (text !== undefined) {
79
- return brand(_extends({}, annotation, {
80
- text: text
81
- }));
90
+ return brand(
91
+ _extends({}, annotation, {
92
+ text: text,
93
+ })
94
+ )
82
95
  } else {
83
- return annotation;
96
+ return annotation
84
97
  }
85
98
  }
86
- /**
87
- * Given an existing ObjectAnnotation, merges new Annotations in there.
88
- */
89
-
90
99
 
91
100
  function merge(objAnnotation, fields) {
92
- var newFields = _extends({}, objAnnotation.fields, fields);
101
+ var newFields = _extends({}, objAnnotation.fields, fields)
93
102
 
94
- return object(newFields, objAnnotation.text);
103
+ return object(newFields, objAnnotation.text)
95
104
  }
96
105
 
97
106
  function asAnnotation(thing) {
98
- return typeof thing === 'object' && thing !== null && _register.has(thing) ? thing : undefined;
107
+ return typeof thing === 'object' && thing !== null && _register.has(thing) ? thing : undefined
99
108
  }
100
109
 
101
110
  function annotateArray(value, text, seen) {
102
- seen.add(value);
111
+ seen.add(value)
103
112
  var items = value.map(function (v) {
104
- return annotate(v, undefined, seen);
105
- });
106
- return array(items, text);
113
+ return annotate(v, undefined, seen)
114
+ })
115
+ return array(items, text)
107
116
  }
108
117
 
109
118
  function annotateObject(obj, text, seen) {
110
- seen.add(obj);
111
- var fields = {};
119
+ seen.add(obj)
120
+ var fields = {}
112
121
  Object.keys(obj).forEach(function (key) {
113
- var value = obj[key];
114
- fields[key] = annotate(value, undefined, seen);
115
- });
116
- return object(fields, text);
122
+ var value = obj[key]
123
+ fields[key] = annotate(value, undefined, seen)
124
+ })
125
+ return object(fields, text)
117
126
  }
118
127
 
119
128
  function annotate(value, text, seen) {
120
129
  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);
130
+ return scalar(value, text)
122
131
  }
123
132
 
124
- var ann = asAnnotation(value);
133
+ var ann = asAnnotation(value)
125
134
 
126
135
  if (ann) {
127
- return updateText(ann, text);
136
+ return updateText(ann, text)
128
137
  }
129
138
 
130
139
  if (Array.isArray(value)) {
131
- // "Circular references" can only exist in objects or arrays
132
140
  if (seen.has(value)) {
133
- return circularRef(text);
141
+ return circularRef(text)
134
142
  } else {
135
- return annotateArray(value, text, seen);
143
+ return annotateArray(value, text, seen)
136
144
  }
137
145
  }
138
146
 
139
147
  if (typeof value === 'object') {
140
- // "Circular references" can only exist in objects or arrays
141
148
  if (seen.has(value)) {
142
- return circularRef(text);
149
+ return circularRef(text)
143
150
  } else {
144
- return annotateObject(value, text, seen);
151
+ return annotateObject(value, text, seen)
145
152
  }
146
153
  }
147
154
 
148
155
  if (typeof value === 'function') {
149
- return func(text);
156
+ return func(text)
150
157
  }
151
158
 
152
- return unknown(value, text);
159
+ return unknown(value, text)
153
160
  }
154
161
 
155
162
  function public_annotate(value, text) {
156
- return annotate(value, text, new WeakSet());
163
+ return annotate(value, text, new WeakSet())
157
164
  }
158
165
 
159
166
  function public_annotateObject(obj, text) {
160
- return annotateObject(obj, text, new WeakSet());
161
- }
167
+ return annotateObject(obj, text, new WeakSet())
168
+ }