decoders 1.26.0-test1 → 2.0.0-beta1

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.
Files changed (129) hide show
  1. package/CHANGELOG.md +23 -3
  2. package/cjs/_guard.js +26 -0
  3. package/cjs/_guard.js.flow +20 -0
  4. package/cjs/_types.js +1 -0
  5. package/cjs/_types.js.flow +20 -0
  6. package/cjs/_utils.js +108 -0
  7. package/cjs/_utils.js.flow +97 -0
  8. package/cjs/annotate.js +161 -0
  9. package/cjs/annotate.js.flow +218 -0
  10. package/cjs/format/index.js +12 -0
  11. package/cjs/format/index.js.flow +4 -0
  12. package/cjs/format/inline.js +146 -0
  13. package/cjs/format/inline.js.flow +122 -0
  14. package/cjs/format/short.js +10 -0
  15. package/cjs/format/short.js.flow +8 -0
  16. package/cjs/index.js +118 -395
  17. package/cjs/index.js.flow +31 -24
  18. package/cjs/result.js +172 -0
  19. package/cjs/result.js.flow +166 -0
  20. package/cjs/stdlib/array.js +108 -0
  21. package/cjs/{array.js.flow → stdlib/array.js.flow} +22 -25
  22. package/cjs/stdlib/boolean.js +44 -0
  23. package/cjs/{boolean.js.flow → stdlib/boolean.js.flow} +8 -7
  24. package/cjs/stdlib/composition.js +56 -0
  25. package/cjs/{utils.js.flow → stdlib/composition.js.flow} +7 -22
  26. package/cjs/stdlib/constants.js +69 -0
  27. package/cjs/{constants.js.flow → stdlib/constants.js.flow} +13 -12
  28. package/cjs/stdlib/date.js +46 -0
  29. package/cjs/{date.js.flow → stdlib/date.js.flow} +9 -7
  30. package/cjs/stdlib/describe.js +26 -0
  31. package/cjs/{describe.js.flow → stdlib/describe.js.flow} +4 -4
  32. package/cjs/stdlib/dispatch.js +62 -0
  33. package/cjs/{dispatch.js.flow → stdlib/dispatch.js.flow} +6 -5
  34. package/cjs/stdlib/either.js +117 -0
  35. package/cjs/{either.js.flow → stdlib/either.js.flow} +55 -35
  36. package/cjs/stdlib/fail.js +21 -0
  37. package/cjs/stdlib/fail.js.flow +12 -0
  38. package/cjs/stdlib/instanceOf.js +19 -0
  39. package/cjs/{instanceOf.js.flow → stdlib/instanceOf.js.flow} +7 -8
  40. package/cjs/{json.js → stdlib/json.js} +3 -5
  41. package/cjs/{json.js.flow → stdlib/json.js.flow} +4 -4
  42. package/cjs/{lazy.js → stdlib/lazy.js} +1 -3
  43. package/cjs/{lazy.js.flow → stdlib/lazy.js.flow} +1 -1
  44. package/cjs/stdlib/mapping.js +67 -0
  45. package/cjs/stdlib/mapping.js.flow +54 -0
  46. package/cjs/stdlib/number.js +40 -0
  47. package/cjs/{number.js.flow → stdlib/number.js.flow} +10 -11
  48. package/cjs/stdlib/object.js +194 -0
  49. package/cjs/{object.js.flow → stdlib/object.js.flow} +44 -52
  50. package/cjs/stdlib/optional.js +54 -0
  51. package/cjs/{optional.js.flow → stdlib/optional.js.flow} +6 -7
  52. package/cjs/stdlib/string.js +98 -0
  53. package/cjs/{string.js.flow → stdlib/string.js.flow} +13 -12
  54. package/cjs/stdlib/tuple.js +173 -0
  55. package/cjs/{tuple.js.flow → stdlib/tuple.js.flow} +62 -63
  56. package/es/_guard.js +15 -0
  57. package/es/_types.js +0 -0
  58. package/es/_utils.js +93 -0
  59. package/es/annotate.js +144 -0
  60. package/es/format/index.js +2 -0
  61. package/es/format/inline.js +137 -0
  62. package/es/format/short.js +4 -0
  63. package/es/index.js +36 -1211
  64. package/es/result.js +139 -0
  65. package/es/stdlib/array.js +91 -0
  66. package/es/stdlib/boolean.js +28 -0
  67. package/es/stdlib/composition.js +42 -0
  68. package/es/stdlib/constants.js +46 -0
  69. package/es/stdlib/date.js +28 -0
  70. package/{cjs → es/stdlib}/describe.js +5 -11
  71. package/{cjs → es/stdlib}/dispatch.js +8 -15
  72. package/es/stdlib/either.js +90 -0
  73. package/es/stdlib/fail.js +11 -0
  74. package/es/stdlib/instanceOf.js +8 -0
  75. package/es/stdlib/json.js +15 -0
  76. package/es/stdlib/lazy.js +11 -0
  77. package/es/stdlib/mapping.js +54 -0
  78. package/es/stdlib/number.js +25 -0
  79. package/es/stdlib/object.js +175 -0
  80. package/es/stdlib/optional.js +38 -0
  81. package/{cjs → es/stdlib}/string.js +18 -35
  82. package/es/stdlib/tuple.js +155 -0
  83. package/package.json +9 -5
  84. package/ts/_guard.d.ts +7 -0
  85. package/ts/{helpers.d.ts → _helpers.d.ts} +0 -0
  86. package/ts/{types.d.ts → _types.d.ts} +3 -5
  87. package/ts/{utils.d.ts → _utils.d.ts} +3 -3
  88. package/ts/annotate.d.ts +58 -0
  89. package/ts/array.d.ts +1 -1
  90. package/ts/boolean.d.ts +1 -1
  91. package/ts/constants.d.ts +1 -1
  92. package/ts/date.d.ts +1 -1
  93. package/ts/describe.d.ts +1 -1
  94. package/ts/dispatch.d.ts +3 -3
  95. package/ts/either.d.ts +8 -8
  96. package/ts/fail.d.ts +1 -1
  97. package/ts/index.d.ts +30 -26
  98. package/ts/inline.d.ts +3 -0
  99. package/ts/instanceOf.d.ts +1 -1
  100. package/ts/json.d.ts +1 -1
  101. package/ts/lazy.d.ts +1 -1
  102. package/ts/mapping.d.ts +1 -1
  103. package/ts/number.d.ts +1 -1
  104. package/ts/object.d.ts +6 -6
  105. package/ts/optional.d.ts +1 -1
  106. package/ts/result.d.ts +39 -0
  107. package/ts/short.d.ts +3 -0
  108. package/ts/string.d.ts +1 -1
  109. package/ts/tuple.d.ts +5 -5
  110. package/cjs/array.js +0 -133
  111. package/cjs/boolean.js +0 -42
  112. package/cjs/constants.js +0 -67
  113. package/cjs/date.js +0 -42
  114. package/cjs/either.js +0 -85
  115. package/cjs/fail.js +0 -19
  116. package/cjs/fail.js.flow +0 -13
  117. package/cjs/guard.js +0 -30
  118. package/cjs/guard.js.flow +0 -36
  119. package/cjs/instanceOf.js +0 -17
  120. package/cjs/mapping.js +0 -113
  121. package/cjs/mapping.js.flow +0 -71
  122. package/cjs/number.js +0 -38
  123. package/cjs/object.js +0 -254
  124. package/cjs/optional.js +0 -52
  125. package/cjs/tuple.js +0 -199
  126. package/cjs/types.js +0 -5
  127. package/cjs/types.js.flow +0 -26
  128. package/cjs/utils.js +0 -70
  129. package/ts/guard.d.ts +0 -7
@@ -0,0 +1,12 @@
1
+ "use strict";
2
+
3
+ exports.__esModule = true;
4
+ exports.formatShort = exports.formatInline = void 0;
5
+
6
+ var _inline = require("./inline");
7
+
8
+ exports.formatInline = _inline.formatInline;
9
+
10
+ var _short = require("./short");
11
+
12
+ exports.formatShort = _short.formatShort;
@@ -0,0 +1,4 @@
1
+ // @flow strict
2
+
3
+ export { formatInline } from './inline';
4
+ export { formatShort } from './short';
@@ -0,0 +1,146 @@
1
+ "use strict";
2
+
3
+ exports.__esModule = true;
4
+ exports.formatInline = formatInline;
5
+ exports.serializeAnnotation = serializeAnnotation;
6
+ exports.serializeValue = serializeValue;
7
+
8
+ var _utils = require("../_utils");
9
+
10
+ function serializeString(s, width) {
11
+ if (width === void 0) {
12
+ width = 80;
13
+ }
14
+
15
+ // Full string
16
+ // Abbreviated to $maxlen i.e. "Vincent Driess..." [truncated]
17
+ var ser = JSON.stringify(s);
18
+
19
+ if (ser.length <= width) {
20
+ return ser;
21
+ } // Cut off a bit
22
+
23
+
24
+ var truncated = s.substring(0, width - 15) + '...';
25
+ ser = JSON.stringify(truncated) + ' [truncated]';
26
+ return ser;
27
+ }
28
+
29
+ function serializeArray(annotation, prefix) {
30
+ var items = annotation.items;
31
+
32
+ if (items.length === 0) {
33
+ return '[]';
34
+ }
35
+
36
+ var result = [];
37
+ items.forEach(function (item) {
38
+ var _serializeAnnotation = serializeAnnotation(item, prefix + _utils.INDENT),
39
+ ser = _serializeAnnotation[0],
40
+ ann = _serializeAnnotation[1];
41
+
42
+ result.push(prefix + _utils.INDENT + ser + ',');
43
+
44
+ if (ann !== undefined) {
45
+ result.push((0, _utils.indent)(ann, prefix + _utils.INDENT));
46
+ }
47
+ });
48
+ return ['['].concat(result, [prefix + ']']).join('\n');
49
+ }
50
+
51
+ function serializeObject(annotation, prefix) {
52
+ var fields = annotation.fields;
53
+ var fieldNames = Object.keys(fields);
54
+
55
+ if (fieldNames.length === 0) {
56
+ return '{}';
57
+ }
58
+
59
+ var result = [];
60
+ fieldNames.forEach(function (key) {
61
+ var valueAnnotation = fields[key];
62
+ var kser = serializeValue(key);
63
+ var valPrefix = prefix + _utils.INDENT + ' '.repeat(kser.length + 2);
64
+
65
+ var _serializeAnnotation2 = serializeAnnotation(valueAnnotation, prefix + _utils.INDENT),
66
+ vser = _serializeAnnotation2[0],
67
+ vann = _serializeAnnotation2[1];
68
+
69
+ result.push(prefix + _utils.INDENT + kser + ': ' + vser + ',');
70
+
71
+ if (vann !== undefined) {
72
+ result.push((0, _utils.indent)(vann, valPrefix));
73
+ }
74
+ });
75
+ return ['{'].concat(result, [prefix + '}']).join('\n');
76
+ }
77
+
78
+ function serializeValue(value) {
79
+ // istanbul ignore else
80
+ if (typeof value === 'string') {
81
+ return serializeString(value);
82
+ } else if (typeof value === 'number' || typeof value === 'boolean') {
83
+ return value.toString();
84
+ } else if (value === null) {
85
+ return 'null';
86
+ } else if (value === undefined) {
87
+ return 'undefined';
88
+ } else {
89
+ var valueAsDate = (0, _utils.asDate)(value);
90
+
91
+ if (valueAsDate !== null) {
92
+ return "new Date(" + JSON.stringify(valueAsDate.toISOString()) + ")";
93
+ } else if (value instanceof Date) {
94
+ // NOTE: Using `instanceof Date` is unreliable way of checking dates.
95
+ // If this case occurs (and it didn't pass the prior isDate())
96
+ // check, then this must be the case where it's an invalid date.
97
+ return '(Invalid Date)';
98
+ } else {
99
+ return '(unserializable)';
100
+ }
101
+ }
102
+ }
103
+
104
+ function serializeAnnotation(ann, prefix) {
105
+ if (prefix === void 0) {
106
+ prefix = '';
107
+ }
108
+
109
+ // The serialized data (the input object echoed back)
110
+ var serialized;
111
+
112
+ if (ann.type === 'array') {
113
+ serialized = serializeArray(ann, prefix);
114
+ } else if (ann.type === 'object') {
115
+ serialized = serializeObject(ann, prefix);
116
+ } else if (ann.type === 'function') {
117
+ serialized = '<function>';
118
+ } else if (ann.type === 'circular-ref') {
119
+ serialized = '<circular ref>';
120
+ } else if (ann.type === 'unknown') {
121
+ serialized = '???';
122
+ } else {
123
+ serialized = serializeValue(ann.value);
124
+ }
125
+
126
+ var text = ann.text;
127
+
128
+ if (text !== undefined) {
129
+ var sep = '^'.repeat((0, _utils.isMultiline)(serialized) ? 1 : serialized.length);
130
+ return [serialized, [sep, text].join((0, _utils.isMultiline)(text) ? '\n' : ' ')];
131
+ } else {
132
+ return [serialized, undefined];
133
+ }
134
+ }
135
+
136
+ function formatInline(ann) {
137
+ var _serializeAnnotation3 = serializeAnnotation(ann),
138
+ serialized = _serializeAnnotation3[0],
139
+ annotation = _serializeAnnotation3[1];
140
+
141
+ if (annotation !== undefined) {
142
+ return serialized + '\n' + annotation;
143
+ } else {
144
+ return serialized;
145
+ }
146
+ }
@@ -0,0 +1,122 @@
1
+ // @flow strict
2
+
3
+ import { asDate, indent, INDENT, isMultiline } from '../_utils';
4
+ import type { Annotation, ArrayAnnotation, ObjectAnnotation } from '../annotate';
5
+
6
+ function serializeString(s: string, width: number = 80): string {
7
+ // Full string
8
+ // Abbreviated to $maxlen i.e. "Vincent Driess..." [truncated]
9
+ let ser = JSON.stringify(s);
10
+ if (ser.length <= width) {
11
+ return ser;
12
+ }
13
+
14
+ // Cut off a bit
15
+ const truncated = s.substring(0, width - 15) + '...';
16
+ ser = JSON.stringify(truncated) + ' [truncated]';
17
+ return ser;
18
+ }
19
+
20
+ function serializeArray(annotation: ArrayAnnotation, prefix: string): string {
21
+ const { items } = annotation;
22
+ if (items.length === 0) {
23
+ return '[]';
24
+ }
25
+
26
+ const result = [];
27
+ items.forEach((item) => {
28
+ const [ser, ann] = serializeAnnotation(item, prefix + INDENT);
29
+ result.push(prefix + INDENT + ser + ',');
30
+ if (ann !== undefined) {
31
+ result.push(indent(ann, prefix + INDENT));
32
+ }
33
+ });
34
+ return ['[', ...result, prefix + ']'].join('\n');
35
+ }
36
+
37
+ function serializeObject(annotation: ObjectAnnotation, prefix: string): string {
38
+ const { fields } = annotation;
39
+
40
+ const fieldNames = Object.keys(fields);
41
+ if (fieldNames.length === 0) {
42
+ return '{}';
43
+ }
44
+
45
+ const result = [];
46
+ fieldNames.forEach((key) => {
47
+ const valueAnnotation = fields[key];
48
+ const kser = serializeValue(key);
49
+
50
+ const valPrefix = prefix + INDENT + ' '.repeat(kser.length + 2);
51
+ const [vser, vann] = serializeAnnotation(valueAnnotation, prefix + INDENT);
52
+
53
+ result.push(prefix + INDENT + kser + ': ' + vser + ',');
54
+ if (vann !== undefined) {
55
+ result.push(indent(vann, valPrefix));
56
+ }
57
+ });
58
+ return ['{', ...result, prefix + '}'].join('\n');
59
+ }
60
+
61
+ export function serializeValue(value: mixed): string {
62
+ // istanbul ignore else
63
+ if (typeof value === 'string') {
64
+ return serializeString(value);
65
+ } else if (typeof value === 'number' || typeof value === 'boolean') {
66
+ return value.toString();
67
+ } else if (value === null) {
68
+ return 'null';
69
+ } else if (value === undefined) {
70
+ return 'undefined';
71
+ } else {
72
+ const valueAsDate = asDate(value);
73
+ if (valueAsDate !== null) {
74
+ return `new Date(${JSON.stringify(valueAsDate.toISOString())})`;
75
+ } else if (value instanceof Date) {
76
+ // NOTE: Using `instanceof Date` is unreliable way of checking dates.
77
+ // If this case occurs (and it didn't pass the prior isDate())
78
+ // check, then this must be the case where it's an invalid date.
79
+ return '(Invalid Date)';
80
+ } else {
81
+ return '(unserializable)';
82
+ }
83
+ }
84
+ }
85
+
86
+ export function serializeAnnotation(
87
+ ann: Annotation,
88
+ prefix: string = '',
89
+ ): [string, string | void] {
90
+ // The serialized data (the input object echoed back)
91
+ let serialized;
92
+ if (ann.type === 'array') {
93
+ serialized = serializeArray(ann, prefix);
94
+ } else if (ann.type === 'object') {
95
+ serialized = serializeObject(ann, prefix);
96
+ } else if (ann.type === 'function') {
97
+ serialized = '<function>';
98
+ } else if (ann.type === 'circular-ref') {
99
+ serialized = '<circular ref>';
100
+ } else if (ann.type === 'unknown') {
101
+ serialized = '???';
102
+ } else {
103
+ serialized = serializeValue(ann.value);
104
+ }
105
+
106
+ const text = ann.text;
107
+ if (text !== undefined) {
108
+ const sep = '^'.repeat(isMultiline(serialized) ? 1 : serialized.length);
109
+ return [serialized, [sep, text].join(isMultiline(text) ? '\n' : ' ')];
110
+ } else {
111
+ return [serialized, undefined];
112
+ }
113
+ }
114
+
115
+ export function formatInline(ann: Annotation): string {
116
+ const [serialized, annotation] = serializeAnnotation(ann);
117
+ if (annotation !== undefined) {
118
+ return serialized + '\n' + annotation;
119
+ } else {
120
+ return serialized;
121
+ }
122
+ }
@@ -0,0 +1,10 @@
1
+ "use strict";
2
+
3
+ exports.__esModule = true;
4
+ exports.formatShort = formatShort;
5
+
6
+ var _utils = require("../_utils");
7
+
8
+ function formatShort(ann) {
9
+ return (0, _utils.summarize)(ann, []).join('\n');
10
+ }
@@ -0,0 +1,8 @@
1
+ // @flow strict
2
+
3
+ import { summarize as _summarize } from '../_utils';
4
+ import type { Annotation } from '../annotate';
5
+
6
+ export function formatShort(ann: Annotation): string {
7
+ return _summarize(ann, []).join('\n');
8
+ }