decoders 2.0.0-beta4 → 2.0.0-beta8

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 (132) hide show
  1. package/CHANGELOG.md +10 -0
  2. package/NotSupportedTSVersion.d.ts +1 -0
  3. package/README.md +728 -343
  4. package/_guard.d.ts +7 -0
  5. package/_guard.js +2 -6
  6. package/_guard.js.flow +3 -3
  7. package/{_esm/_guard.js → _guard.mjs} +3 -3
  8. package/_types.d.ts +13 -0
  9. package/{_esm/_types.js → _types.mjs} +0 -0
  10. package/_utils.d.ts +10 -0
  11. package/{_esm/_utils.js → _utils.mjs} +0 -0
  12. package/annotate.d.ts +62 -0
  13. package/{_esm/annotate.js → annotate.mjs} +0 -0
  14. package/core/_helpers.d.ts +79 -0
  15. package/core/array.d.ts +5 -0
  16. package/core/array.js +7 -11
  17. package/core/array.js.flow +6 -9
  18. package/{_esm/core/array.js → core/array.mjs} +9 -9
  19. package/core/boolean.d.ts +5 -0
  20. package/core/boolean.js +4 -8
  21. package/core/boolean.js.flow +3 -5
  22. package/{_esm/core/boolean.js → core/boolean.mjs} +6 -6
  23. package/core/composition.d.ts +14 -0
  24. package/core/composition.js +9 -11
  25. package/core/composition.js.flow +13 -8
  26. package/{_esm/core/composition.js → core/composition.mjs} +10 -8
  27. package/core/constants.d.ts +11 -0
  28. package/core/constants.js +6 -10
  29. package/core/constants.js.flow +7 -9
  30. package/{_esm/core/constants.js → core/constants.mjs} +7 -7
  31. package/core/date.d.ts +4 -0
  32. package/core/date.js +3 -7
  33. package/core/date.js.flow +2 -4
  34. package/{_esm/core/date.js → core/date.mjs} +6 -6
  35. package/core/describe.d.ts +3 -0
  36. package/core/describe.js +2 -6
  37. package/core/describe.js.flow +2 -2
  38. package/{_esm/core/describe.js → core/describe.mjs} +3 -3
  39. package/core/dispatch.d.ts +8 -0
  40. package/core/dispatch.js +9 -13
  41. package/core/dispatch.js.flow +8 -8
  42. package/{_esm/core/dispatch.js → core/dispatch.mjs} +10 -10
  43. package/core/either.d.ts +61 -0
  44. package/core/either.js +7 -11
  45. package/core/either.js.flow +6 -6
  46. package/{_esm/core/either.js → core/either.mjs} +8 -8
  47. package/core/fail.d.ts +3 -0
  48. package/core/fail.js +2 -6
  49. package/core/fail.js.flow +2 -2
  50. package/{_esm/core/fail.js → core/fail.mjs} +3 -3
  51. package/core/instanceOf.d.ts +3 -0
  52. package/core/instanceOf.js +2 -6
  53. package/core/instanceOf.js.flow +3 -3
  54. package/core/instanceOf.mjs +8 -0
  55. package/core/json.d.ts +11 -0
  56. package/core/json.mjs +15 -0
  57. package/core/lazy.d.ts +3 -0
  58. package/{_esm/core/lazy.js → core/lazy.mjs} +0 -0
  59. package/core/mapping.d.ts +6 -0
  60. package/core/mapping.js +23 -23
  61. package/core/mapping.js.flow +25 -17
  62. package/{_esm/core/mapping.js → core/mapping.mjs} +26 -22
  63. package/core/number.d.ts +6 -0
  64. package/core/number.js +9 -13
  65. package/core/number.js.flow +18 -12
  66. package/core/number.mjs +25 -0
  67. package/core/object.d.ts +33 -0
  68. package/core/object.js +7 -11
  69. package/core/object.js.flow +7 -7
  70. package/{_esm/core/object.js → core/object.mjs} +9 -9
  71. package/core/optional.d.ts +5 -0
  72. package/core/optional.js +4 -8
  73. package/core/optional.js.flow +3 -3
  74. package/{_esm/core/optional.js → core/optional.mjs} +6 -6
  75. package/core/string.d.ts +10 -0
  76. package/core/string.js +18 -48
  77. package/core/string.js.flow +16 -39
  78. package/core/string.mjs +49 -0
  79. package/core/tuple.d.ts +30 -0
  80. package/core/tuple.js +12 -8
  81. package/core/tuple.js.flow +25 -29
  82. package/{_esm/core/tuple.js → core/tuple.mjs} +16 -12
  83. package/format.d.ts +4 -0
  84. package/{format/inline.js → format.js} +6 -1
  85. package/{_esm/format/inline.js.flow → format.js.flow} +6 -2
  86. package/{_esm/format/inline.js → format.mjs} +4 -1
  87. package/index.d.ts +43 -0
  88. package/index.js +3 -2
  89. package/index.js.flow +2 -2
  90. package/{_esm/index.js → index.mjs} +19 -19
  91. package/package.json +15 -3
  92. package/result.d.ts +39 -0
  93. package/result.js +3 -78
  94. package/result.js.flow +4 -76
  95. package/{_esm/result.js → result.mjs} +3 -64
  96. package/_esm/_guard.js.flow +0 -20
  97. package/_esm/_types.js.flow +0 -20
  98. package/_esm/_utils.js.flow +0 -97
  99. package/_esm/annotate.js.flow +0 -218
  100. package/_esm/core/array.js.flow +0 -103
  101. package/_esm/core/boolean.js.flow +0 -29
  102. package/_esm/core/composition.js.flow +0 -43
  103. package/_esm/core/constants.js.flow +0 -46
  104. package/_esm/core/date.js.flow +0 -40
  105. package/_esm/core/describe.js.flow +0 -17
  106. package/_esm/core/dispatch.js.flow +0 -58
  107. package/_esm/core/either.js.flow +0 -151
  108. package/_esm/core/fail.js.flow +0 -12
  109. package/_esm/core/instanceOf.js +0 -8
  110. package/_esm/core/instanceOf.js.flow +0 -20
  111. package/_esm/core/json.js +0 -15
  112. package/_esm/core/json.js.flow +0 -28
  113. package/_esm/core/lazy.js.flow +0 -15
  114. package/_esm/core/mapping.js.flow +0 -54
  115. package/_esm/core/number.js +0 -25
  116. package/_esm/core/number.js.flow +0 -34
  117. package/_esm/core/object.js.flow +0 -203
  118. package/_esm/core/optional.js.flow +0 -41
  119. package/_esm/core/string.js +0 -76
  120. package/_esm/core/string.js.flow +0 -82
  121. package/_esm/core/tuple.js.flow +0 -215
  122. package/_esm/format/index.js +0 -2
  123. package/_esm/format/index.js.flow +0 -4
  124. package/_esm/format/short.js +0 -4
  125. package/_esm/format/short.js.flow +0 -8
  126. package/_esm/index.js.flow +0 -63
  127. package/_esm/result.js.flow +0 -170
  128. package/format/index.js +0 -12
  129. package/format/index.js.flow +0 -4
  130. package/format/inline.js.flow +0 -122
  131. package/format/short.js +0 -10
  132. package/format/short.js.flow +0 -8
@@ -1,63 +0,0 @@
1
- // @flow strict
2
-
3
- /**
4
- * Elm-like JSON decoders, for use with Flow.
5
- * See http://elmplayground.com/decoding-json-in-elm-1 for an introduction.
6
- *
7
- * Why? All JSON responses coming from our API endpoints are just that: free-form
8
- * JSON data. To Flow, the only type classification possilbe is "any" -- effectively
9
- * turning off all type checks for anything related to JSON. To the receiving end
10
- * (our frontend), the structure of that data is completely opaque to any type
11
- * checkers since JSON values can be anything: an object, an array, null, a string,
12
- * a bool, etc. Our type system is not a runtime type system, so we need a way of
13
- * "converting" an any-type JSON value into a type that we want to work with in our
14
- * frontend code base.
15
- *
16
- * Elm's solution to this problem is to define composable decoders: functions that
17
- * take anything and either fail with an error, or guarantee to return the expected
18
- * type. In our case, it's fine to fail with a runtime error.
19
- *
20
- */
21
- export type {
22
- Decoder,
23
- DecodeResult,
24
- DecoderType,
25
- Guard,
26
- GuardType,
27
- Predicate,
28
- Scalar,
29
- } from './_types';
30
- export type { Result } from './result';
31
- export type { JSONValue, JSONObject, JSONArray } from './core/json';
32
-
33
- export { guard } from './_guard';
34
-
35
- export { compose, map, predicate } from './core/composition';
36
-
37
- export { array, nonEmptyArray, poja } from './core/array';
38
- export { boolean, numericBoolean, truthy } from './core/boolean';
39
- export { constant, hardcoded, mixed, null_, undefined_, unknown } from './core/constants';
40
- export { date, iso8601 } from './core/date';
41
- export { describe } from './core/describe';
42
- export { dispatch } from './core/dispatch';
43
- export {
44
- either,
45
- either3,
46
- either4,
47
- either5,
48
- either6,
49
- either7,
50
- either8,
51
- either9,
52
- oneOf,
53
- } from './core/either';
54
- export { fail } from './core/fail';
55
- export { instanceOf } from './core/instanceOf';
56
- export { json, jsonObject, jsonArray } from './core/json';
57
- export { lazy } from './core/lazy';
58
- export { mapping, dict } from './core/mapping';
59
- export { integer, number, positiveInteger, positiveNumber } from './core/number';
60
- export { exact, inexact, object, pojo } from './core/object';
61
- export { maybe, nullable, optional } from './core/optional';
62
- export { email, nonEmptyString, regex, string, url } from './core/string';
63
- export { tuple1, tuple2, tuple3, tuple4, tuple5, tuple6 } from './core/tuple';
@@ -1,170 +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 okValue<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
- export function okOrErrValue<T, E>(result: Result<T, E>): T | E {
55
- return result.type === 'ok' ? result.value : result.error;
56
- }
57
-
58
- /**
59
- * Unwrap the value from this Result instance if this is an "Ok" result.
60
- * Otherwise, will throw the "Err" error via a runtime exception.
61
- */
62
- export function unwrap<T>(result: Result<T, mixed>): T {
63
- if (result.type === 'ok') {
64
- return result.value;
65
- } else {
66
- throw result.error;
67
- }
68
- }
69
-
70
- export function expect<T>(result: Result<T, mixed>, message: string | Error): T {
71
- if (result.type === 'ok') {
72
- return result.value;
73
- } else {
74
- throw message instanceof Error ? message : new Error(message);
75
- }
76
- }
77
-
78
- export function dispatch<T, E, O>(
79
- result: Result<T, E>,
80
- okCallback: (value: T) => O,
81
- errCallback: (error: E) => O,
82
- ): O {
83
- return result.type === 'ok' ? okCallback(result.value) : errCallback(result.error);
84
- }
85
-
86
- /**
87
- * If the given result is OK, defers to the other result. Otherwise returns the
88
- * error result.
89
- *
90
- * It's like saying A && B, but on Result.
91
- *
92
- * Examples:
93
- *
94
- * Result.ok(42) && Result.ok('hi') // => Ok('hi')
95
- * Result.err('boo') && Result.ok('hi') // => Err('boo')
96
- * Result.ok(42) && Result.err('boo') // => Err('boo')
97
- * Result.err('boo') && Result.err('boo') // => Err('boo')
98
- *
99
- */
100
- // export function and<T, E, T2>(
101
- // result1: Result<T, E>,
102
- // result2: Result<T2, E>,
103
- // ): Result<T2, E> {
104
- // return result1.type === 'ok' ? result2 : result1;
105
- // }
106
-
107
- /**
108
- * If the given result is OK, return that result. Otherwise, defers to the
109
- * other result.
110
- *
111
- * It's like saying A || B, but on Result.
112
- *
113
- * Examples:
114
- *
115
- * Result.ok(42) || Result.ok('hi') // => Ok(42)
116
- * Result.err('boo') || Result.ok('hi') // => Ok('hi')
117
- * Result.ok(42) || Result.err('boo') // => Ok(42)
118
- * Result.err('bleh') || Result.err('boo') // => Err('boo')
119
- *
120
- */
121
- // export function or<T, E, E2>(
122
- // result1: Result<T, E>,
123
- // result2: Result<T, E2>,
124
- // ): Result<T, E2> {
125
- // return result1.type === 'ok' ? result1 : result2;
126
- // }
127
-
128
- /**
129
- * Like .and(), aka &&, but the second argument gets evaluated lazily only if
130
- * the first result is an Ok result. If so, it has access to the Ok value from
131
- * the first argument.
132
- */
133
- export function andThen<T, E, T2>(
134
- result1: Result<T, E>,
135
- lazyResult2: (value: T) => Result<T2, E>,
136
- ): Result<T2, E> {
137
- return result1.type === 'ok' ? lazyResult2(result1.value) : result1;
138
- }
139
-
140
- /**
141
- * Like .or(), aka ||, but the second argument gets evaluated lazily only if
142
- * the first result is an Err result. If so, it has access to the Err value
143
- * from the first argument.
144
- */
145
- export function orElse<T, E, E2>(
146
- result1: Result<T, E>,
147
- lazyResult2: (errValue: E) => Result<T, E2>,
148
- ): Result<T, E2> {
149
- return result1.type === 'ok' ? result1 : lazyResult2(result1.error);
150
- }
151
-
152
- /**
153
- * Transform an Ok result. Will not touch Err results.
154
- */
155
- export function mapOk<T, E, T2>(
156
- result: Result<T, E>,
157
- mapper: (value: T) => T2,
158
- ): Result<T2, E> {
159
- return result.type === 'ok' ? ok(mapper(result.value)) : result;
160
- }
161
-
162
- /**
163
- * Transform an Err value. Will not touch Ok results.
164
- */
165
- export function mapError<T, E, E2>(
166
- result: Result<T, E>,
167
- mapper: (error: E) => E2,
168
- ): Result<T, E2> {
169
- return result.type === 'ok' ? result : err(mapper(result.error));
170
- }
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
- }