decoders 2.0.0-beta6 → 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 (123) hide show
  1. package/NotSupportedTSVersion.d.ts +1 -0
  2. package/_guard.d.ts +7 -0
  3. package/_guard.js +2 -6
  4. package/_guard.js.flow +3 -3
  5. package/{_esm/_guard.js → _guard.mjs} +3 -3
  6. package/_types.d.ts +13 -0
  7. package/{_esm/_types.js → _types.mjs} +0 -0
  8. package/_utils.d.ts +10 -0
  9. package/{_esm/_utils.js → _utils.mjs} +0 -0
  10. package/annotate.d.ts +62 -0
  11. package/{_esm/annotate.js → annotate.mjs} +0 -0
  12. package/core/_helpers.d.ts +79 -0
  13. package/core/array.d.ts +5 -0
  14. package/core/array.js +5 -9
  15. package/core/array.js.flow +5 -5
  16. package/{_esm/core/array.js → core/array.mjs} +7 -7
  17. package/core/boolean.d.ts +5 -0
  18. package/core/boolean.js +4 -8
  19. package/core/boolean.js.flow +3 -5
  20. package/{_esm/core/boolean.js → core/boolean.mjs} +6 -6
  21. package/core/composition.d.ts +12 -0
  22. package/core/composition.js +6 -10
  23. package/core/composition.js.flow +6 -6
  24. package/{_esm/core/composition.js → core/composition.mjs} +7 -7
  25. package/core/constants.d.ts +11 -0
  26. package/core/constants.js +6 -10
  27. package/core/constants.js.flow +7 -9
  28. package/{_esm/core/constants.js → core/constants.mjs} +7 -7
  29. package/core/date.d.ts +4 -0
  30. package/core/date.js +3 -7
  31. package/core/date.js.flow +2 -4
  32. package/{_esm/core/date.js → core/date.mjs} +6 -6
  33. package/core/describe.d.ts +3 -0
  34. package/core/describe.js +2 -6
  35. package/core/describe.js.flow +2 -2
  36. package/{_esm/core/describe.js → core/describe.mjs} +3 -3
  37. package/core/dispatch.d.ts +8 -0
  38. package/core/dispatch.js +2 -6
  39. package/core/dispatch.js.flow +2 -2
  40. package/{_esm/core/dispatch.js → core/dispatch.mjs} +4 -4
  41. package/core/either.d.ts +61 -0
  42. package/core/either.js +7 -11
  43. package/core/either.js.flow +6 -6
  44. package/{_esm/core/either.js → core/either.mjs} +8 -8
  45. package/core/fail.d.ts +3 -0
  46. package/core/fail.js +2 -6
  47. package/core/fail.js.flow +2 -2
  48. package/{_esm/core/fail.js → core/fail.mjs} +3 -3
  49. package/core/instanceOf.d.ts +3 -0
  50. package/core/instanceOf.js +2 -6
  51. package/core/instanceOf.js.flow +3 -3
  52. package/core/instanceOf.mjs +8 -0
  53. package/core/json.d.ts +11 -0
  54. package/core/json.mjs +15 -0
  55. package/core/lazy.d.ts +3 -0
  56. package/{_esm/core/lazy.js → core/lazy.mjs} +0 -0
  57. package/core/mapping.d.ts +6 -0
  58. package/core/mapping.js +4 -8
  59. package/core/mapping.js.flow +3 -3
  60. package/{_esm/core/mapping.js → core/mapping.mjs} +7 -7
  61. package/core/number.d.ts +6 -0
  62. package/core/number.js +2 -6
  63. package/core/number.js.flow +3 -3
  64. package/{_esm/core/number.js → core/number.mjs} +4 -4
  65. package/core/object.d.ts +33 -0
  66. package/core/object.js +7 -11
  67. package/core/object.js.flow +7 -7
  68. package/{_esm/core/object.js → core/object.mjs} +9 -9
  69. package/core/optional.d.ts +5 -0
  70. package/core/optional.js +4 -8
  71. package/core/optional.js.flow +3 -3
  72. package/{_esm/core/optional.js → core/optional.mjs} +6 -6
  73. package/core/string.d.ts +7 -0
  74. package/core/string.js +5 -9
  75. package/core/string.js.flow +5 -7
  76. package/{_esm/core/string.js → core/string.mjs} +7 -7
  77. package/core/tuple.d.ts +30 -0
  78. package/{_esm/core/tuple.js → core/tuple.mjs} +4 -4
  79. package/format.d.ts +4 -0
  80. package/{format/inline.js → format.js} +6 -1
  81. package/{_esm/format/inline.js.flow → format.js.flow} +6 -2
  82. package/{_esm/format/inline.js → format.mjs} +4 -1
  83. package/{_esm/index.js.flow → index.d.ts} +3 -23
  84. package/{_esm/index.js → index.mjs} +19 -19
  85. package/package.json +15 -3
  86. package/result.d.ts +39 -0
  87. package/result.js +0 -62
  88. package/result.js.flow +0 -60
  89. package/{_esm/result.js → result.mjs} +0 -54
  90. package/_esm/_guard.js.flow +0 -20
  91. package/_esm/_types.js.flow +0 -20
  92. package/_esm/_utils.js.flow +0 -97
  93. package/_esm/annotate.js.flow +0 -218
  94. package/_esm/core/array.js.flow +0 -103
  95. package/_esm/core/boolean.js.flow +0 -29
  96. package/_esm/core/composition.js.flow +0 -43
  97. package/_esm/core/constants.js.flow +0 -46
  98. package/_esm/core/date.js.flow +0 -40
  99. package/_esm/core/describe.js.flow +0 -17
  100. package/_esm/core/dispatch.js.flow +0 -58
  101. package/_esm/core/either.js.flow +0 -151
  102. package/_esm/core/fail.js.flow +0 -12
  103. package/_esm/core/instanceOf.js +0 -8
  104. package/_esm/core/instanceOf.js.flow +0 -20
  105. package/_esm/core/json.js +0 -15
  106. package/_esm/core/json.js.flow +0 -28
  107. package/_esm/core/lazy.js.flow +0 -15
  108. package/_esm/core/mapping.js.flow +0 -54
  109. package/_esm/core/number.js.flow +0 -34
  110. package/_esm/core/object.js.flow +0 -203
  111. package/_esm/core/optional.js.flow +0 -41
  112. package/_esm/core/string.js.flow +0 -82
  113. package/_esm/core/tuple.js.flow +0 -214
  114. package/_esm/format/index.js +0 -2
  115. package/_esm/format/index.js.flow +0 -4
  116. package/_esm/format/short.js +0 -4
  117. package/_esm/format/short.js.flow +0 -8
  118. package/_esm/result.js.flow +0 -158
  119. package/format/index.js +0 -12
  120. package/format/index.js.flow +0 -4
  121. package/format/inline.js.flow +0 -122
  122. package/format/short.js +0 -10
  123. package/format/short.js.flow +0 -8
@@ -1,6 +1,6 @@
1
- import * as Result from '../result';
2
- import { annotate } from '../annotate';
3
- import { compose, predicate } from './composition';
1
+ import { annotate } from '../annotate.mjs';
2
+ import { compose, predicate } from './composition.mjs';
3
+ import { err, ok } from '../result.mjs';
4
4
 
5
5
  /** Match groups in this regex:
6
6
  * \1 - the scheme
@@ -17,7 +17,7 @@ var DEFAULT_SCHEMES = ['https'];
17
17
  */
18
18
 
19
19
  export var string = function string(blob) {
20
- return typeof blob === 'string' ? Result.ok(blob) : Result.err(annotate(blob, 'Must be string'));
20
+ return typeof blob === 'string' ? ok(blob) : err(annotate(blob, 'Must be string'));
21
21
  };
22
22
  /**
23
23
  * Decoder that only returns Ok for non-empty string inputs. Err otherwise.
@@ -62,14 +62,14 @@ export var url = function url(schemes) {
62
62
  var matches = value.match(url_re);
63
63
 
64
64
  if (!matches) {
65
- return Result.err(annotate(value, 'Must be URL'));
65
+ return err(annotate(value, 'Must be URL'));
66
66
  } else {
67
67
  var scheme = matches[1];
68
68
 
69
69
  if (schemes.length === 0 || schemes.includes(scheme.toLowerCase())) {
70
- return Result.ok(value);
70
+ return ok(value);
71
71
  } else {
72
- return Result.err(annotate(value, "URL scheme must be any of: " + schemes.join(', ')));
72
+ return err(annotate(value, "URL scheme must be any of: " + schemes.join(', ')));
73
73
  }
74
74
  }
75
75
  });
@@ -0,0 +1,30 @@
1
+ import { Decoder } from '../_types';
2
+
3
+ export function tuple1<T1>(d1: Decoder<T1>): Decoder<[T1]>;
4
+ export function tuple2<T1, T2>(d1: Decoder<T1>, d2: Decoder<T2>): Decoder<[T1, T2]>;
5
+ export function tuple3<T1, T2, T3>(
6
+ d1: Decoder<T1>,
7
+ d2: Decoder<T2>,
8
+ d3: Decoder<T3>,
9
+ ): Decoder<[T1, T2, T3]>;
10
+ export function tuple4<T1, T2, T3, T4>(
11
+ d1: Decoder<T1>,
12
+ d2: Decoder<T2>,
13
+ d3: Decoder<T3>,
14
+ d4: Decoder<T4>,
15
+ ): Decoder<[T1, T2, T3, T4]>;
16
+ export function tuple5<T1, T2, T3, T4, T5>(
17
+ d1: Decoder<T1>,
18
+ d2: Decoder<T2>,
19
+ d3: Decoder<T3>,
20
+ d4: Decoder<T4>,
21
+ d5: Decoder<T5>,
22
+ ): Decoder<[T1, T2, T3, T4, T5]>;
23
+ export function tuple6<T1, T2, T3, T4, T5, T6>(
24
+ d1: Decoder<T1>,
25
+ d2: Decoder<T2>,
26
+ d3: Decoder<T3>,
27
+ d4: Decoder<T4>,
28
+ d5: Decoder<T5>,
29
+ d6: Decoder<T6>,
30
+ ): Decoder<[T1, T2, T3, T4, T5, T6]>;
@@ -1,7 +1,7 @@
1
- import { annotate } from '../annotate';
2
- import { compose, predicate } from './composition';
3
- import { err, ok, unwrap } from '../result';
4
- import { poja } from './array';
1
+ import { annotate } from '../annotate.mjs';
2
+ import { compose, predicate } from './composition.mjs';
3
+ import { err, ok, unwrap } from '../result.mjs';
4
+ import { poja } from './array.mjs';
5
5
 
6
6
  function okOrErr(result) {
7
7
  return result.type === 'ok' ? result.value : result.error;
package/format.d.ts ADDED
@@ -0,0 +1,4 @@
1
+ import { Annotation } from './annotate';
2
+
3
+ export function formatInline(ann: Annotation): string;
4
+ export function formatShort(ann: Annotation): string;
@@ -2,10 +2,11 @@
2
2
 
3
3
  exports.__esModule = true;
4
4
  exports.formatInline = formatInline;
5
+ exports.formatShort = formatShort;
5
6
  exports.serializeAnnotation = serializeAnnotation;
6
7
  exports.serializeValue = serializeValue;
7
8
 
8
- var _utils = require("../_utils");
9
+ var _utils = require("./_utils");
9
10
 
10
11
  function serializeString(s, width) {
11
12
  if (width === void 0) {
@@ -143,4 +144,8 @@ function formatInline(ann) {
143
144
  } else {
144
145
  return serialized;
145
146
  }
147
+ }
148
+
149
+ function formatShort(ann) {
150
+ return (0, _utils.summarize)(ann, []).join('\n');
146
151
  }
@@ -1,7 +1,7 @@
1
1
  // @flow strict
2
2
 
3
- import { asDate, indent, INDENT, isMultiline } from '../_utils';
4
- import type { Annotation, ArrayAnnotation, ObjectAnnotation } from '../annotate';
3
+ import { summarize as _summarize, asDate, INDENT, indent, isMultiline } from './_utils';
4
+ import type { Annotation, ArrayAnnotation, ObjectAnnotation } from './annotate';
5
5
 
6
6
  function serializeString(s: string, width: number = 80): string {
7
7
  // Full string
@@ -120,3 +120,7 @@ export function formatInline(ann: Annotation): string {
120
120
  return serialized;
121
121
  }
122
122
  }
123
+
124
+ export function formatShort(ann: Annotation): string {
125
+ return _summarize(ann, []).join('\n');
126
+ }
@@ -1,4 +1,4 @@
1
- import { asDate, indent, INDENT, isMultiline } from '../_utils';
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) {
@@ -134,4 +134,7 @@ export function formatInline(ann) {
134
134
  } else {
135
135
  return serialized;
136
136
  }
137
+ }
138
+ export function formatShort(ann) {
139
+ return _summarize(ann, []).join('\n');
137
140
  }
@@ -1,24 +1,4 @@
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 {
1
+ export {
22
2
  Decoder,
23
3
  DecodeResult,
24
4
  DecoderType,
@@ -27,8 +7,8 @@ export type {
27
7
  Predicate,
28
8
  Scalar,
29
9
  } from './_types';
30
- export type { Result } from './result';
31
- export type { JSONValue, JSONObject, JSONArray } from './core/json';
10
+ export { Result } from './result';
11
+ export { JSONValue, JSONObject, JSONArray } from './core/json';
32
12
 
33
13
  export { guard } from './_guard';
34
14
 
@@ -16,22 +16,22 @@
16
16
  * type. In our case, it's fine to fail with a runtime error.
17
17
  *
18
18
  */
19
- export { guard } from './_guard';
20
- export { compose, map, predicate } from './core/composition';
21
- export { array, nonEmptyArray, poja } from './core/array';
22
- export { boolean, numericBoolean, truthy } from './core/boolean';
23
- export { constant, hardcoded, mixed, null_, undefined_, unknown } from './core/constants';
24
- export { date, iso8601 } from './core/date';
25
- export { describe } from './core/describe';
26
- export { dispatch } from './core/dispatch';
27
- export { either, either3, either4, either5, either6, either7, either8, either9, oneOf } from './core/either';
28
- export { fail } from './core/fail';
29
- export { instanceOf } from './core/instanceOf';
30
- export { json, jsonObject, jsonArray } from './core/json';
31
- export { lazy } from './core/lazy';
32
- export { mapping, dict } from './core/mapping';
33
- export { integer, number, positiveInteger, positiveNumber } from './core/number';
34
- export { exact, inexact, object, pojo } from './core/object';
35
- export { maybe, nullable, optional } from './core/optional';
36
- export { email, nonEmptyString, regex, string, url } from './core/string';
37
- export { tuple1, tuple2, tuple3, tuple4, tuple5, tuple6 } from './core/tuple';
19
+ export { guard } from './_guard.mjs';
20
+ export { compose, map, predicate } from './core/composition.mjs';
21
+ export { array, nonEmptyArray, poja } from './core/array.mjs';
22
+ export { boolean, numericBoolean, truthy } from './core/boolean.mjs';
23
+ export { constant, hardcoded, mixed, null_, undefined_, unknown } from './core/constants.mjs';
24
+ export { date, iso8601 } from './core/date.mjs';
25
+ export { describe } from './core/describe.mjs';
26
+ export { dispatch } from './core/dispatch.mjs';
27
+ export { either, either3, either4, either5, either6, either7, either8, either9, oneOf } from './core/either.mjs';
28
+ export { fail } from './core/fail.mjs';
29
+ export { instanceOf } from './core/instanceOf.mjs';
30
+ export { json, jsonObject, jsonArray } from './core/json.mjs';
31
+ export { lazy } from './core/lazy.mjs';
32
+ export { mapping, dict } from './core/mapping.mjs';
33
+ export { integer, number, positiveInteger, positiveNumber } from './core/number.mjs';
34
+ export { exact, inexact, object, pojo } from './core/object.mjs';
35
+ export { maybe, nullable, optional } from './core/optional.mjs';
36
+ export { email, nonEmptyString, regex, string, url } from './core/string.mjs';
37
+ export { tuple1, tuple2, tuple3, tuple4, tuple5, tuple6 } from './core/tuple.mjs';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "decoders",
3
- "version": "2.0.0-beta6",
3
+ "version": "2.0.0-beta7",
4
4
  "description": "Elegant and battle-tested validation library for type-safe input data (for TypeScript and Flow)",
5
5
  "license": "MIT",
6
6
  "repository": {
@@ -13,7 +13,7 @@
13
13
  "url": "https://github.com/nvie/decoders/issues"
14
14
  },
15
15
  "main": "./index.js",
16
- "module": "./_esm/index.js",
16
+ "module": "./index.mjs",
17
17
  "keywords": [
18
18
  "decoder",
19
19
  "decoders",
@@ -40,6 +40,18 @@
40
40
  "map",
41
41
  "predicate"
42
42
  ],
43
+ "*": {
44
+ "*": [
45
+ "NotSupportedTSVersion.d.ts"
46
+ ]
47
+ },
43
48
  "githubUrl": "https://github.com/nvie/decoders",
44
- "sideEffects": false
49
+ "sideEffects": false,
50
+ "typesVersions": {
51
+ ">=4.1.0": {
52
+ "*": [
53
+ "*"
54
+ ]
55
+ }
56
+ }
45
57
  }
package/result.d.ts ADDED
@@ -0,0 +1,39 @@
1
+ export interface Ok<T> {
2
+ type: 'ok';
3
+ value: T;
4
+ error: undefined;
5
+ }
6
+
7
+ export interface Err<E> {
8
+ type: 'err';
9
+ value: undefined;
10
+ error: E;
11
+ }
12
+
13
+ export type Result<T, E> = Ok<T> | Err<E>;
14
+
15
+ export function ok<T>(value: T): Ok<T>;
16
+ export function err<E>(error: E): Err<E>;
17
+ export function unwrap<T>(result: Result<T, unknown>): T;
18
+ export function expect<T>(result: Result<T, unknown>, message: string | Error): T;
19
+ export function dispatch<T, E, O>(
20
+ result: Result<T, E>,
21
+ okCallback: (value: T) => O,
22
+ errCallback: (error: E) => O,
23
+ ): O;
24
+ export function andThen<T, E, T2>(
25
+ result1: Result<T, E>,
26
+ lazyResult2: (value: T) => Result<T2, E>,
27
+ ): Result<T2, E>;
28
+ export function orElse<T, E, E2>(
29
+ result1: Result<T, E>,
30
+ lazyResult2: (errValue: E) => Result<T, E2>,
31
+ ): Result<T, E2>;
32
+ export function mapOk<T, E, T2>(
33
+ result: Result<T, E>,
34
+ mapper: (value: T) => T2,
35
+ ): Result<T2, E>;
36
+ export function mapError<T, E, E2>(
37
+ result: Result<T, E>,
38
+ mapper: (error: E) => E2,
39
+ ): Result<T, E2>;
package/result.js CHANGED
@@ -5,15 +5,11 @@ exports.andThen = andThen;
5
5
  exports.dispatch = dispatch;
6
6
  exports.err = err;
7
7
  exports.expect = expect;
8
- exports.isErr = isErr;
9
- exports.isOk = isOk;
10
8
  exports.mapError = mapError;
11
9
  exports.mapOk = mapOk;
12
10
  exports.ok = ok;
13
11
  exports.orElse = orElse;
14
- exports.toString = toString;
15
12
  exports.unwrap = unwrap;
16
- exports.withDefault = withDefault;
17
13
 
18
14
  /**
19
15
  * Result <value> <error>
@@ -43,22 +39,6 @@ function err(error) {
43
39
  error: error
44
40
  };
45
41
  }
46
-
47
- function toString(result) {
48
- return result.type === 'ok' ? "Ok(" + String(result.value) + ")" : "Err(" + String(result.error) + ")";
49
- }
50
-
51
- function isOk(result) {
52
- return result.type === 'ok';
53
- }
54
-
55
- function isErr(result) {
56
- return result.type === 'err';
57
- }
58
-
59
- function withDefault(result, defaultValue) {
60
- return result.type === 'ok' ? result.value : defaultValue;
61
- }
62
42
  /**
63
43
  * Unwrap the value from this Result instance if this is an "Ok" result.
64
44
  * Otherwise, will throw the "Err" error via a runtime exception.
@@ -84,48 +64,6 @@ function expect(result, message) {
84
64
  function dispatch(result, okCallback, errCallback) {
85
65
  return result.type === 'ok' ? okCallback(result.value) : errCallback(result.error);
86
66
  }
87
- /**
88
- * If the given result is OK, defers to the other result. Otherwise returns the
89
- * error result.
90
- *
91
- * It's like saying A && B, but on Result.
92
- *
93
- * Examples:
94
- *
95
- * Result.ok(42) && Result.ok('hi') // => Ok('hi')
96
- * Result.err('boo') && Result.ok('hi') // => Err('boo')
97
- * Result.ok(42) && Result.err('boo') // => Err('boo')
98
- * Result.err('boo') && Result.err('boo') // => Err('boo')
99
- *
100
- */
101
- // export function and<T, E, T2>(
102
- // result1: Result<T, E>,
103
- // result2: Result<T2, E>,
104
- // ): Result<T2, E> {
105
- // return result1.type === 'ok' ? result2 : result1;
106
- // }
107
-
108
- /**
109
- * If the given result is OK, return that result. Otherwise, defers to the
110
- * other result.
111
- *
112
- * It's like saying A || B, but on Result.
113
- *
114
- * Examples:
115
- *
116
- * Result.ok(42) || Result.ok('hi') // => Ok(42)
117
- * Result.err('boo') || Result.ok('hi') // => Ok('hi')
118
- * Result.ok(42) || Result.err('boo') // => Ok(42)
119
- * Result.err('bleh') || Result.err('boo') // => Err('boo')
120
- *
121
- */
122
- // export function or<T, E, E2>(
123
- // result1: Result<T, E>,
124
- // result2: Result<T, E2>,
125
- // ): Result<T, E2> {
126
- // return result1.type === 'ok' ? result1 : result2;
127
- // }
128
-
129
67
  /**
130
68
  * Like .and(), aka &&, but the second argument gets evaluated lazily only if
131
69
  * the first result is an Ok result. If so, it has access to the Ok value from
package/result.js.flow CHANGED
@@ -25,24 +25,6 @@ export function err<E>(error: E): Err<E> {
25
25
  return { type: 'err', value: undefined, error };
26
26
  }
27
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
28
  /**
47
29
  * Unwrap the value from this Result instance if this is an "Ok" result.
48
30
  * Otherwise, will throw the "Err" error via a runtime exception.
@@ -71,48 +53,6 @@ export function dispatch<T, E, O>(
71
53
  return result.type === 'ok' ? okCallback(result.value) : errCallback(result.error);
72
54
  }
73
55
 
74
- /**
75
- * If the given result is OK, defers to the other result. Otherwise returns the
76
- * error result.
77
- *
78
- * It's like saying A && B, but on Result.
79
- *
80
- * Examples:
81
- *
82
- * Result.ok(42) && Result.ok('hi') // => Ok('hi')
83
- * Result.err('boo') && Result.ok('hi') // => Err('boo')
84
- * Result.ok(42) && Result.err('boo') // => Err('boo')
85
- * Result.err('boo') && Result.err('boo') // => Err('boo')
86
- *
87
- */
88
- // export function and<T, E, T2>(
89
- // result1: Result<T, E>,
90
- // result2: Result<T2, E>,
91
- // ): Result<T2, E> {
92
- // return result1.type === 'ok' ? result2 : result1;
93
- // }
94
-
95
- /**
96
- * If the given result is OK, return that result. Otherwise, defers to the
97
- * other result.
98
- *
99
- * It's like saying A || B, but on Result.
100
- *
101
- * Examples:
102
- *
103
- * Result.ok(42) || Result.ok('hi') // => Ok(42)
104
- * Result.err('boo') || Result.ok('hi') // => Ok('hi')
105
- * Result.ok(42) || Result.err('boo') // => Ok(42)
106
- * Result.err('bleh') || Result.err('boo') // => Err('boo')
107
- *
108
- */
109
- // export function or<T, E, E2>(
110
- // result1: Result<T, E>,
111
- // result2: Result<T, E2>,
112
- // ): Result<T, E2> {
113
- // return result1.type === 'ok' ? result1 : result2;
114
- // }
115
-
116
56
  /**
117
57
  * Like .and(), aka &&, but the second argument gets evaluated lazily only if
118
58
  * the first result is an Ok result. If so, it has access to the Ok value from
@@ -25,18 +25,6 @@ export function err(error) {
25
25
  error: error
26
26
  };
27
27
  }
28
- export function toString(result) {
29
- return result.type === 'ok' ? "Ok(" + String(result.value) + ")" : "Err(" + String(result.error) + ")";
30
- }
31
- export function isOk(result) {
32
- return result.type === 'ok';
33
- }
34
- export function isErr(result) {
35
- return result.type === 'err';
36
- }
37
- export function withDefault(result, defaultValue) {
38
- return result.type === 'ok' ? result.value : defaultValue;
39
- }
40
28
  /**
41
29
  * Unwrap the value from this Result instance if this is an "Ok" result.
42
30
  * Otherwise, will throw the "Err" error via a runtime exception.
@@ -59,48 +47,6 @@ export function expect(result, message) {
59
47
  export function dispatch(result, okCallback, errCallback) {
60
48
  return result.type === 'ok' ? okCallback(result.value) : errCallback(result.error);
61
49
  }
62
- /**
63
- * If the given result is OK, defers to the other result. Otherwise returns the
64
- * error result.
65
- *
66
- * It's like saying A && B, but on Result.
67
- *
68
- * Examples:
69
- *
70
- * Result.ok(42) && Result.ok('hi') // => Ok('hi')
71
- * Result.err('boo') && Result.ok('hi') // => Err('boo')
72
- * Result.ok(42) && Result.err('boo') // => Err('boo')
73
- * Result.err('boo') && Result.err('boo') // => Err('boo')
74
- *
75
- */
76
- // export function and<T, E, T2>(
77
- // result1: Result<T, E>,
78
- // result2: Result<T2, E>,
79
- // ): Result<T2, E> {
80
- // return result1.type === 'ok' ? result2 : result1;
81
- // }
82
-
83
- /**
84
- * If the given result is OK, return that result. Otherwise, defers to the
85
- * other result.
86
- *
87
- * It's like saying A || B, but on Result.
88
- *
89
- * Examples:
90
- *
91
- * Result.ok(42) || Result.ok('hi') // => Ok(42)
92
- * Result.err('boo') || Result.ok('hi') // => Ok('hi')
93
- * Result.ok(42) || Result.err('boo') // => Ok(42)
94
- * Result.err('bleh') || Result.err('boo') // => Err('boo')
95
- *
96
- */
97
- // export function or<T, E, E2>(
98
- // result1: Result<T, E>,
99
- // result2: Result<T, E2>,
100
- // ): Result<T, E2> {
101
- // return result1.type === 'ok' ? result1 : result2;
102
- // }
103
-
104
50
  /**
105
51
  * Like .and(), aka &&, but the second argument gets evaluated lazily only if
106
52
  * the first result is an Ok result. If so, it has access to the Ok value from
@@ -1,20 +0,0 @@
1
- // @flow strict
2
-
3
- import * as Result from './result';
4
- import { formatInline } from './format';
5
- import type { Annotation } from './annotate';
6
- import type { Decoder, Guard } from './_types';
7
-
8
- export function guard<T>(
9
- decoder: Decoder<T>,
10
- formatter: (Annotation) => string = formatInline,
11
- ): Guard<T> {
12
- return (blob: mixed) =>
13
- Result.unwrap(
14
- Result.mapError(decoder(blob), (annotation) => {
15
- const err = new Error('\n' + formatter(annotation));
16
- err.name = 'Decoding error';
17
- return err;
18
- }),
19
- );
20
- }
@@ -1,20 +0,0 @@
1
- // @flow strict
2
-
3
- import type { Annotation } from './annotate';
4
- import type { Result } from './result';
5
-
6
- export type Scalar = string | number | boolean | symbol | void | null;
7
-
8
- export type Predicate<T> = (T) => boolean;
9
- export type DecodeResult<T> = Result<T, Annotation>;
10
-
11
- export type Decoder<T, F = mixed> = (F) => DecodeResult<T>;
12
- export type Guard<T> = (mixed) => T;
13
-
14
- /**
15
- * A "type function" which informs Flow about how a type will be modified at runtime.
16
- * Read this as "given a Guard of type T, I can produce a value of type T". This
17
- * definition helps construct $ObjMap types.
18
- */
19
- export type DecoderType = <T>(Decoder<T>) => T;
20
- export type GuardType = <T>(Guard<T>) => T;
@@ -1,97 +0,0 @@
1
- // @flow strict
2
-
3
- import type { Annotation } from './annotate';
4
-
5
- // $FlowFixMe[unclear-type] - deliberate casting
6
- type cast = any;
7
-
8
- // Two spaces of indentation
9
- export const INDENT = ' ';
10
-
11
- /**
12
- * `x instanceof Date` checks are unreliable across stack frames (that information
13
- * might get lost by the JS runtime), so we'll have to reside to more runtime
14
- * inspection checks.
15
- *
16
- * Taken from https://stackoverflow.com/a/44198641
17
- */
18
- export function isDate(value: mixed): boolean {
19
- return (
20
- !!value &&
21
- // $FlowFixMe[method-unbinding]
22
- Object.prototype.toString.call(value) === '[object Date]' &&
23
- !isNaN(value)
24
- );
25
- }
26
-
27
- /**
28
- * Is value is a valid Date instance, then return that. If not, then return
29
- * null.
30
- */
31
- export function asDate(value: mixed): Date | null {
32
- return isDate(value) ? ((value: cast): Date) : null;
33
- }
34
-
35
- export function isMultiline(s: string): boolean {
36
- return s.indexOf('\n') >= 0;
37
- }
38
-
39
- export function indent(s: string, prefix: string = INDENT): string {
40
- if (isMultiline(s)) {
41
- return s
42
- .split('\n')
43
- .map((line) => prefix + line)
44
- .join('\n');
45
- } else {
46
- return prefix + s;
47
- }
48
- }
49
-
50
- /**
51
- * Walks the annotation tree and emits the annotation's key path within the
52
- * object tree, and the message as a series of messages (array of strings).
53
- */
54
- export function summarize(
55
- ann: Annotation,
56
- keypath: $ReadOnlyArray<number | string> = [],
57
- ): Array<string> {
58
- const result: Array<string> = [];
59
-
60
- if (ann.type === 'array') {
61
- const items = ann.items;
62
- let index = 0;
63
- items.forEach((ann) => {
64
- summarize(ann, [...keypath, index++]).forEach((item) =>
65
- // Collect to results
66
- result.push(item),
67
- );
68
- });
69
- } else if (ann.type === 'object') {
70
- const fields = ann.fields;
71
- Object.keys(fields).forEach((key) => {
72
- const value = fields[key];
73
- summarize(value, [...keypath, key]).forEach((item) =>
74
- // Collect to results
75
- result.push(item),
76
- );
77
- });
78
- }
79
-
80
- const text = ann.text;
81
- if (!text) {
82
- return result;
83
- }
84
-
85
- let prefix: string;
86
- if (keypath.length === 0) {
87
- prefix = '';
88
- } else if (keypath.length === 1) {
89
- prefix =
90
- typeof keypath[0] === 'number'
91
- ? `Value at index ${keypath[0]}: `
92
- : `Value at key ${JSON.stringify(keypath[0])}: `;
93
- } else {
94
- prefix = `Value at keypath ${keypath.map((x) => x.toString()).join('.')}: `;
95
- }
96
- return [...result, prefix + text];
97
- }