decoders 2.0.0-beta3 → 2.0.0-beta7

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 (127) hide show
  1. package/CHANGELOG.md +4 -0
  2. package/NotSupportedTSVersion.d.ts +1 -0
  3. package/_guard.d.ts +7 -0
  4. package/_guard.js +2 -6
  5. package/_guard.js.flow +3 -3
  6. package/{_esm/_guard.js → _guard.mjs} +3 -3
  7. package/_types.d.ts +13 -0
  8. package/{_esm/_types.js → _types.mjs} +0 -0
  9. package/_utils.d.ts +10 -0
  10. package/{_esm/_utils.js → _utils.mjs} +0 -0
  11. package/annotate.d.ts +62 -0
  12. package/{_esm/annotate.js → annotate.mjs} +0 -0
  13. package/core/_helpers.d.ts +79 -0
  14. package/core/array.d.ts +5 -0
  15. package/core/array.js +5 -9
  16. package/core/array.js.flow +5 -5
  17. package/{_esm/core/array.js → core/array.mjs} +7 -7
  18. package/core/boolean.d.ts +5 -0
  19. package/core/boolean.js +4 -8
  20. package/core/boolean.js.flow +3 -5
  21. package/{_esm/core/boolean.js → core/boolean.mjs} +6 -6
  22. package/core/composition.d.ts +12 -0
  23. package/core/composition.js +6 -10
  24. package/core/composition.js.flow +6 -6
  25. package/{_esm/core/composition.js → core/composition.mjs} +7 -7
  26. package/core/constants.d.ts +11 -0
  27. package/core/constants.js +6 -10
  28. package/core/constants.js.flow +7 -9
  29. package/{_esm/core/constants.js → core/constants.mjs} +7 -7
  30. package/core/date.d.ts +4 -0
  31. package/core/date.js +3 -7
  32. package/core/date.js.flow +2 -4
  33. package/{_esm/core/date.js → core/date.mjs} +6 -6
  34. package/core/describe.d.ts +3 -0
  35. package/core/describe.js +2 -6
  36. package/core/describe.js.flow +2 -2
  37. package/{_esm/core/describe.js → core/describe.mjs} +3 -3
  38. package/core/dispatch.d.ts +8 -0
  39. package/core/dispatch.js +2 -6
  40. package/core/dispatch.js.flow +2 -2
  41. package/{_esm/core/dispatch.js → core/dispatch.mjs} +4 -4
  42. package/core/either.d.ts +61 -0
  43. package/core/either.js +7 -11
  44. package/core/either.js.flow +6 -6
  45. package/{_esm/core/either.js → core/either.mjs} +8 -8
  46. package/core/fail.d.ts +3 -0
  47. package/core/fail.js +2 -6
  48. package/core/fail.js.flow +2 -2
  49. package/{_esm/core/fail.js → core/fail.mjs} +3 -3
  50. package/core/instanceOf.d.ts +3 -0
  51. package/core/instanceOf.js +2 -6
  52. package/core/instanceOf.js.flow +3 -3
  53. package/core/instanceOf.mjs +8 -0
  54. package/core/json.d.ts +11 -0
  55. package/core/json.mjs +15 -0
  56. package/core/lazy.d.ts +3 -0
  57. package/{_esm/core/lazy.js → core/lazy.mjs} +0 -0
  58. package/core/mapping.d.ts +6 -0
  59. package/core/mapping.js +4 -8
  60. package/core/mapping.js.flow +3 -3
  61. package/{_esm/core/mapping.js → core/mapping.mjs} +7 -7
  62. package/core/number.d.ts +6 -0
  63. package/core/number.js +2 -6
  64. package/core/number.js.flow +3 -3
  65. package/{_esm/core/number.js → core/number.mjs} +4 -4
  66. package/core/object.d.ts +33 -0
  67. package/core/object.js +7 -11
  68. package/core/object.js.flow +7 -7
  69. package/{_esm/core/object.js → core/object.mjs} +9 -9
  70. package/core/optional.d.ts +5 -0
  71. package/core/optional.js +4 -8
  72. package/core/optional.js.flow +3 -3
  73. package/{_esm/core/optional.js → core/optional.mjs} +6 -6
  74. package/core/string.d.ts +7 -0
  75. package/core/string.js +5 -9
  76. package/core/string.js.flow +5 -7
  77. package/{_esm/core/string.js → core/string.mjs} +7 -7
  78. package/core/tuple.d.ts +30 -0
  79. package/core/tuple.js +10 -6
  80. package/core/tuple.js.flow +24 -30
  81. package/{_esm/core/tuple.js → core/tuple.mjs} +14 -10
  82. package/format.d.ts +4 -0
  83. package/{format/inline.js → format.js} +6 -1
  84. package/{_esm/format/inline.js.flow → format.js.flow} +6 -2
  85. package/{_esm/format/inline.js → format.mjs} +4 -1
  86. package/{_esm/index.js.flow → index.d.ts} +3 -23
  87. package/index.js.flow +1 -1
  88. package/{_esm/index.js → index.mjs} +19 -19
  89. package/package.json +15 -3
  90. package/result.d.ts +39 -0
  91. package/result.js +5 -75
  92. package/result.js.flow +5 -73
  93. package/{_esm/result.js → result.mjs} +4 -62
  94. package/_esm/_guard.js.flow +0 -20
  95. package/_esm/_types.js.flow +0 -20
  96. package/_esm/_utils.js.flow +0 -97
  97. package/_esm/annotate.js.flow +0 -218
  98. package/_esm/core/array.js.flow +0 -103
  99. package/_esm/core/boolean.js.flow +0 -29
  100. package/_esm/core/composition.js.flow +0 -43
  101. package/_esm/core/constants.js.flow +0 -46
  102. package/_esm/core/date.js.flow +0 -40
  103. package/_esm/core/describe.js.flow +0 -17
  104. package/_esm/core/dispatch.js.flow +0 -58
  105. package/_esm/core/either.js.flow +0 -151
  106. package/_esm/core/fail.js.flow +0 -12
  107. package/_esm/core/instanceOf.js +0 -8
  108. package/_esm/core/instanceOf.js.flow +0 -20
  109. package/_esm/core/json.js +0 -15
  110. package/_esm/core/json.js.flow +0 -28
  111. package/_esm/core/lazy.js.flow +0 -15
  112. package/_esm/core/mapping.js.flow +0 -54
  113. package/_esm/core/number.js.flow +0 -34
  114. package/_esm/core/object.js.flow +0 -203
  115. package/_esm/core/optional.js.flow +0 -41
  116. package/_esm/core/string.js.flow +0 -82
  117. package/_esm/core/tuple.js.flow +0 -220
  118. package/_esm/format/index.js +0 -2
  119. package/_esm/format/index.js.flow +0 -4
  120. package/_esm/format/short.js +0 -4
  121. package/_esm/format/short.js.flow +0 -8
  122. package/_esm/result.js.flow +0 -166
  123. package/format/index.js +0 -12
  124. package/format/index.js.flow +0 -4
  125. package/format/inline.js.flow +0 -122
  126. package/format/short.js +0 -10
  127. package/format/short.js.flow +0 -8
@@ -1,166 +0,0 @@
1
- // @flow strict
2
-
3
- /**
4
- * Result <value> <error>
5
- * = Ok <value>
6
- * | Err <error>
7
- */
8
-
9
- type Ok<+T> = {| +type: 'ok', +value: T |};
10
- type Err<+E> = {| +type: 'err', +error: E |};
11
-
12
- export type Result<+T, +E> = Ok<T> | Err<E>;
13
-
14
- /**
15
- * Create a new Result instance representing a successful computation.
16
- */
17
- export function ok<T>(value: T): Ok<T> {
18
- return { type: 'ok', value };
19
- }
20
-
21
- /**
22
- * Create a new Result instance representing a failed computation.
23
- */
24
- export function err<E>(error: E): Err<E> {
25
- return { type: 'err', error };
26
- }
27
-
28
- export function toString(result: Result<mixed, mixed>): string {
29
- return result.type === 'ok'
30
- ? `Ok(${String(result.value)})`
31
- : `Err(${String(result.error)})`;
32
- }
33
-
34
- export function isOk(result: Result<mixed, mixed>): boolean {
35
- return result.type === 'ok';
36
- }
37
-
38
- export function isErr(result: Result<mixed, mixed>): boolean {
39
- return result.type === 'err';
40
- }
41
-
42
- export function withDefault<T>(result: Result<T, mixed>, defaultValue: T): T {
43
- return result.type === 'ok' ? result.value : defaultValue;
44
- }
45
-
46
- export function value<T>(result: Result<T, mixed>): void | T {
47
- return result.type === 'ok' ? result.value : undefined;
48
- }
49
-
50
- export function errValue<E>(result: Result<mixed, E>): void | E {
51
- return result.type === 'err' ? result.error : undefined;
52
- }
53
-
54
- /**
55
- * Unwrap the value from this Result instance if this is an "Ok" result.
56
- * Otherwise, will throw the "Err" error via a runtime exception.
57
- */
58
- export function unwrap<T>(result: Result<T, mixed>): T {
59
- if (result.type === 'ok') {
60
- return result.value;
61
- } else {
62
- throw result.error;
63
- }
64
- }
65
-
66
- export function expect<T>(result: Result<T, mixed>, message: string | Error): T {
67
- if (result.type === 'ok') {
68
- return result.value;
69
- } else {
70
- throw message instanceof Error ? message : new Error(message);
71
- }
72
- }
73
-
74
- export function dispatch<T, E, O>(
75
- result: Result<T, E>,
76
- okCallback: (value: T) => O,
77
- errCallback: (error: E) => O,
78
- ): O {
79
- return result.type === 'ok' ? okCallback(result.value) : errCallback(result.error);
80
- }
81
-
82
- /**
83
- * If the given result is OK, defers to the other result. Otherwise returns the
84
- * error result.
85
- *
86
- * It's like saying A && B, but on Result.
87
- *
88
- * Examples:
89
- *
90
- * Result.ok(42) && Result.ok('hi') // => Ok('hi')
91
- * Result.err('boo') && Result.ok('hi') // => Err('boo')
92
- * Result.ok(42) && Result.err('boo') // => Err('boo')
93
- * Result.err('boo') && Result.err('boo') // => Err('boo')
94
- *
95
- */
96
- // export function and<T, E, T2>(
97
- // result1: Result<T, E>,
98
- // result2: Result<T2, E>,
99
- // ): Result<T2, E> {
100
- // return result1.type === 'ok' ? result2 : result1;
101
- // }
102
-
103
- /**
104
- * If the given result is OK, return that result. Otherwise, defers to the
105
- * other result.
106
- *
107
- * It's like saying A || B, but on Result.
108
- *
109
- * Examples:
110
- *
111
- * Result.ok(42) || Result.ok('hi') // => Ok(42)
112
- * Result.err('boo') || Result.ok('hi') // => Ok('hi')
113
- * Result.ok(42) || Result.err('boo') // => Ok(42)
114
- * Result.err('bleh') || Result.err('boo') // => Err('boo')
115
- *
116
- */
117
- // export function or<T, E, E2>(
118
- // result1: Result<T, E>,
119
- // result2: Result<T, E2>,
120
- // ): Result<T, E2> {
121
- // return result1.type === 'ok' ? result1 : result2;
122
- // }
123
-
124
- /**
125
- * Like .and(), aka &&, but the second argument gets evaluated lazily only if
126
- * the first result is an Ok result. If so, it has access to the Ok value from
127
- * the first argument.
128
- */
129
- export function andThen<T, E, T2>(
130
- result1: Result<T, E>,
131
- lazyResult2: (value: T) => Result<T2, E>,
132
- ): Result<T2, E> {
133
- return result1.type === 'ok' ? lazyResult2(result1.value) : result1;
134
- }
135
-
136
- /**
137
- * Like .or(), aka ||, but the second argument gets evaluated lazily only if
138
- * the first result is an Err result. If so, it has access to the Err value
139
- * from the first argument.
140
- */
141
- export function orElse<T, E, E2>(
142
- result1: Result<T, E>,
143
- lazyResult2: (errValue: E) => Result<T, E2>,
144
- ): Result<T, E2> {
145
- return result1.type === 'ok' ? result1 : lazyResult2(result1.error);
146
- }
147
-
148
- /**
149
- * Transform an Ok result. Will not touch Err results.
150
- */
151
- export function map<T, E, T2>(
152
- result: Result<T, E>,
153
- mapper: (value: T) => T2,
154
- ): Result<T2, E> {
155
- return result.type === 'ok' ? ok(mapper(result.value)) : result;
156
- }
157
-
158
- /**
159
- * Transform an Err value. Will not touch Ok results.
160
- */
161
- export function mapError<T, E, E2>(
162
- result: Result<T, E>,
163
- mapper: (error: E) => E2,
164
- ): Result<T, E2> {
165
- return result.type === 'ok' ? result : err(mapper(result.error));
166
- }
package/format/index.js DELETED
@@ -1,12 +0,0 @@
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;
@@ -1,4 +0,0 @@
1
- // @flow strict
2
-
3
- export { formatInline } from './inline';
4
- export { formatShort } from './short';
@@ -1,122 +0,0 @@
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
- }
package/format/short.js DELETED
@@ -1,10 +0,0 @@
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
- }
@@ -1,8 +0,0 @@
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
- }