decoders 1.25.4 → 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 (132) hide show
  1. package/CHANGELOG.md +30 -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 +120 -0
  17. package/{index.js.flow → 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/{array.js.flow → cjs/stdlib/array.js.flow} +22 -25
  22. package/cjs/stdlib/boolean.js +44 -0
  23. package/{boolean.js.flow → cjs/stdlib/boolean.js.flow} +8 -7
  24. package/cjs/stdlib/composition.js +56 -0
  25. package/{utils.js.flow → cjs/stdlib/composition.js.flow} +7 -22
  26. package/cjs/stdlib/constants.js +69 -0
  27. package/{constants.js.flow → cjs/stdlib/constants.js.flow} +13 -12
  28. package/cjs/stdlib/date.js +46 -0
  29. package/{date.js.flow → cjs/stdlib/date.js.flow} +9 -7
  30. package/cjs/stdlib/describe.js +26 -0
  31. package/{describe.js.flow → cjs/stdlib/describe.js.flow} +4 -4
  32. package/cjs/stdlib/dispatch.js +62 -0
  33. package/{dispatch.js.flow → cjs/stdlib/dispatch.js.flow} +6 -5
  34. package/cjs/stdlib/either.js +117 -0
  35. package/{either.js.flow → cjs/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/{instanceOf.js.flow → cjs/stdlib/instanceOf.js.flow} +7 -8
  40. package/{json.js → cjs/stdlib/json.js} +3 -5
  41. package/{json.js.flow → cjs/stdlib/json.js.flow} +4 -4
  42. package/{lazy.js → cjs/stdlib/lazy.js} +1 -3
  43. package/{lazy.js.flow → cjs/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/{number.js.flow → cjs/stdlib/number.js.flow} +10 -11
  48. package/cjs/stdlib/object.js +194 -0
  49. package/{object.js.flow → cjs/stdlib/object.js.flow} +44 -52
  50. package/cjs/stdlib/optional.js +54 -0
  51. package/{optional.js.flow → cjs/stdlib/optional.js.flow} +6 -7
  52. package/cjs/stdlib/string.js +98 -0
  53. package/{string.js.flow → cjs/stdlib/string.js.flow} +13 -12
  54. package/cjs/stdlib/tuple.js +173 -0
  55. package/{tuple.js.flow → cjs/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 +37 -0
  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/{describe.js → es/stdlib/describe.js} +5 -11
  71. package/{dispatch.js → 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/{string.js → es/stdlib/string.js} +18 -35
  82. package/es/stdlib/tuple.js +155 -0
  83. package/package.json +12 -7
  84. package/ts/_guard.d.ts +7 -0
  85. package/ts/_helpers.d.ts +79 -0
  86. package/{types.d.ts → ts/_types.d.ts} +3 -5
  87. package/{utils.d.ts → ts/_utils.d.ts} +3 -3
  88. package/ts/annotate.d.ts +58 -0
  89. package/{array.d.ts → ts/array.d.ts} +1 -1
  90. package/{boolean.d.ts → ts/boolean.d.ts} +1 -1
  91. package/{constants.d.ts → ts/constants.d.ts} +1 -1
  92. package/{date.d.ts → ts/date.d.ts} +1 -1
  93. package/{describe.d.ts → ts/describe.d.ts} +1 -1
  94. package/{dispatch.d.ts → ts/dispatch.d.ts} +3 -3
  95. package/{either.d.ts → ts/either.d.ts} +8 -8
  96. package/{fail.d.ts → ts/fail.d.ts} +1 -1
  97. package/ts/index.d.ts +42 -0
  98. package/ts/inline.d.ts +3 -0
  99. package/{instanceOf.d.ts → ts/instanceOf.d.ts} +1 -1
  100. package/{json.d.ts → ts/json.d.ts} +1 -1
  101. package/{lazy.d.ts → ts/lazy.d.ts} +1 -1
  102. package/{mapping.d.ts → ts/mapping.d.ts} +1 -1
  103. package/{number.d.ts → ts/number.d.ts} +1 -1
  104. package/{object.d.ts → ts/object.d.ts} +8 -8
  105. package/{optional.d.ts → ts/optional.d.ts} +1 -1
  106. package/ts/result.d.ts +39 -0
  107. package/ts/short.d.ts +3 -0
  108. package/{string.d.ts → ts/string.d.ts} +1 -1
  109. package/{tuple.d.ts → ts/tuple.d.ts} +5 -5
  110. package/array.js +0 -133
  111. package/boolean.js +0 -42
  112. package/constants.js +0 -67
  113. package/date.js +0 -42
  114. package/either.js +0 -85
  115. package/fail.js +0 -19
  116. package/fail.js.flow +0 -13
  117. package/guard.d.ts +0 -7
  118. package/guard.js +0 -30
  119. package/guard.js.flow +0 -36
  120. package/helpers.d.ts +0 -62
  121. package/index.d.ts +0 -38
  122. package/index.js +0 -397
  123. package/instanceOf.js +0 -17
  124. package/mapping.js +0 -113
  125. package/mapping.js.flow +0 -71
  126. package/number.js +0 -38
  127. package/object.js +0 -254
  128. package/optional.js +0 -52
  129. package/tuple.js +0 -199
  130. package/types.js +0 -5
  131. package/types.js.flow +0 -26
  132. package/utils.js +0 -70
@@ -0,0 +1,166 @@
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
+ }
@@ -0,0 +1,108 @@
1
+ "use strict";
2
+
3
+ exports.__esModule = true;
4
+ exports.array = array;
5
+ exports.nonEmptyArray = nonEmptyArray;
6
+ exports.poja = void 0;
7
+
8
+ var Result = _interopRequireWildcard(require("../result"));
9
+
10
+ var _annotate = require("../annotate");
11
+
12
+ var _composition = require("./composition");
13
+
14
+ function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
15
+
16
+ function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { "default": obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj["default"] = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
17
+
18
+ /**
19
+ * Like a "Plain Old JavaScript Object", but for arrays: "Plain Old JavaScript
20
+ * Array" ^_^
21
+ */
22
+ var poja = function poja(blob) {
23
+ if (!Array.isArray(blob)) {
24
+ return Result.err((0, _annotate.annotate)(blob, 'Must be an array'));
25
+ }
26
+
27
+ return Result.ok( // NOTE: Since Flow 0.98, Array.isArray() returns $ReadOnlyArray<mixed>
28
+ // instead of Array<mixed>. For rationale, see
29
+ // https://github.com/facebook/flow/issues/7684. In this case, we
30
+ // don't want to output read-only types because it's up to the user of
31
+ // decoders to determine what they want to do with the decoded output.
32
+ // If they want to write items into the array, that's fine!
33
+ // The fastest way to turn a read-only array into a normal array in
34
+ // Javascript is to use .slice() on it, see this benchmark:
35
+ // http://jsben.ch/lO6C5
36
+ blob.slice());
37
+ };
38
+ /**
39
+ * Given an array of Result instances, loop over them all and return:
40
+ * - An [index, err] tuple, indicating the (index of the) first Err instance
41
+ * encountered; or
42
+ * - a new Ok with an array of all unwrapped Ok'ed values
43
+ */
44
+
45
+
46
+ exports.poja = poja;
47
+
48
+ function all(items, blobs) {
49
+ var results = [];
50
+
51
+ for (var index = 0; index < items.length; ++index) {
52
+ var result = items[index];
53
+
54
+ if (result.type === 'ok') {
55
+ results.push(result.value);
56
+ } else {
57
+ var ann = result.error; // Rewrite the annotation to include the index information, and inject it into the original blob
58
+
59
+ var clone = [].concat(blobs);
60
+ clone.splice(index, 1, (0, _annotate.annotate)(ann, ann.text ? ann.text + " (at index " + index + ")" : "index " + index)); // const errValue = [];
61
+ // if (index > 0) {
62
+ // errValue.push('...'); // TODO: make special mark, not string!
63
+ // }
64
+ // errValue.push(
65
+ // );
66
+ // if (index < iterable.length - 1) {
67
+ // errValue.push('...'); // TODO: make special mark, not string!
68
+ // }
69
+
70
+ return Result.err((0, _annotate.annotate)(clone));
71
+ }
72
+ }
73
+
74
+ return Result.ok(results);
75
+ }
76
+ /**
77
+ * Given a T, builds a decoder that assumes an array input and returns an
78
+ * Array<T>.
79
+ */
80
+
81
+
82
+ function members(decoder) {
83
+ return function (blobs) {
84
+ var results = blobs.map(decoder);
85
+ var result = all(results, blobs);
86
+ return result;
87
+ };
88
+ }
89
+ /**
90
+ * Builds a Decoder that returns Ok for values of `Array<T>`, given a Decoder
91
+ * for `T`. Err otherwise.
92
+ */
93
+
94
+
95
+ function array(decoder) {
96
+ return (0, _composition.compose)(poja, members(decoder));
97
+ }
98
+ /**
99
+ * Builds a Decoder that returns Ok for values of `Array<T>`, but will reject
100
+ * empty arrays.
101
+ */
102
+
103
+
104
+ function nonEmptyArray(decoder) {
105
+ return (0, _composition.compose)(array(decoder), (0, _composition.predicate)(function (arr) {
106
+ return arr.length > 0;
107
+ }, 'Must be non-empty array'));
108
+ }
@@ -1,10 +1,9 @@
1
1
  // @flow strict
2
2
 
3
- import { annotate } from 'debrief';
4
- import { Err, Ok } from 'lemons/Result';
5
-
6
- import type { DecodeResult, Decoder } from './types';
7
- import { compose, predicate } from './utils';
3
+ import * as Result from '../result';
4
+ import { annotate } from '../annotate';
5
+ import { compose, predicate } from './composition';
6
+ import type { Decoder, DecodeResult } from '../_types';
8
7
 
9
8
  /**
10
9
  * Like a "Plain Old JavaScript Object", but for arrays: "Plain Old JavaScript
@@ -12,9 +11,9 @@ import { compose, predicate } from './utils';
12
11
  */
13
12
  export const poja: Decoder<Array<mixed>> = (blob: mixed) => {
14
13
  if (!Array.isArray(blob)) {
15
- return Err(annotate(blob, 'Must be an array'));
14
+ return Result.err(annotate(blob, 'Must be an array'));
16
15
  }
17
- return Ok(
16
+ return Result.ok(
18
17
  // NOTE: Since Flow 0.98, Array.isArray() returns $ReadOnlyArray<mixed>
19
18
  // instead of Array<mixed>. For rationale, see
20
19
  // https://github.com/facebook/flow/issues/7684. In this case, we
@@ -24,27 +23,28 @@ export const poja: Decoder<Array<mixed>> = (blob: mixed) => {
24
23
  // The fastest way to turn a read-only array into a normal array in
25
24
  // Javascript is to use .slice() on it, see this benchmark:
26
25
  // http://jsben.ch/lO6C5
27
- blob.slice()
26
+ blob.slice(),
28
27
  );
29
28
  };
30
29
 
31
30
  /**
32
- * Given an iterable of Result instances, exhaust them all and return:
31
+ * Given an array of Result instances, loop over them all and return:
33
32
  * - An [index, err] tuple, indicating the (index of the) first Err instance
34
33
  * encountered; or
35
34
  * - a new Ok with an array of all unwrapped Ok'ed values
36
35
  */
37
36
  function all<T>(
38
- iterable: $ReadOnlyArray<DecodeResult<T>>,
39
- blobs: $ReadOnlyArray<mixed>
37
+ items: $ReadOnlyArray<DecodeResult<T>>,
38
+ blobs: $ReadOnlyArray<mixed>,
40
39
  ): DecodeResult<Array<T>> {
41
40
  const results: Array<T> = [];
42
- let index = 0;
43
- for (const result of iterable) {
44
- try {
45
- const value = result.unwrap();
46
- results.push(value);
47
- } catch (ann) {
41
+ for (let index = 0; index < items.length; ++index) {
42
+ const result = items[index];
43
+ if (result.type === 'ok') {
44
+ results.push(result.value);
45
+ } else {
46
+ const ann = result.error;
47
+
48
48
  // Rewrite the annotation to include the index information, and inject it into the original blob
49
49
  const clone = [...blobs];
50
50
  clone.splice(
@@ -52,10 +52,8 @@ function all<T>(
52
52
  1,
53
53
  annotate(
54
54
  ann,
55
- ann.annotation !== undefined
56
- ? `${ann.annotation} (at index ${index})`
57
- : `index ${index}`
58
- )
55
+ ann.text ? `${ann.text} (at index ${index})` : `index ${index}`,
56
+ ),
59
57
  );
60
58
 
61
59
  // const errValue = [];
@@ -67,11 +65,10 @@ function all<T>(
67
65
  // if (index < iterable.length - 1) {
68
66
  // errValue.push('...'); // TODO: make special mark, not string!
69
67
  // }
70
- return Err(annotate(clone));
68
+ return Result.err(annotate(clone));
71
69
  }
72
- index++;
73
70
  }
74
- return Ok(results);
71
+ return Result.ok(results);
75
72
  }
76
73
 
77
74
  /**
@@ -101,6 +98,6 @@ export function array<T>(decoder: Decoder<T>): Decoder<Array<T>> {
101
98
  export function nonEmptyArray<T>(decoder: Decoder<T>): Decoder<Array<T>> {
102
99
  return compose(
103
100
  array(decoder),
104
- predicate((arr) => arr.length > 0, 'Must be non-empty array')
101
+ predicate((arr) => arr.length > 0, 'Must be non-empty array'),
105
102
  );
106
103
  }
@@ -0,0 +1,44 @@
1
+ "use strict";
2
+
3
+ exports.__esModule = true;
4
+ exports.truthy = exports.numericBoolean = exports["boolean"] = void 0;
5
+
6
+ var Result = _interopRequireWildcard(require("../result"));
7
+
8
+ var _annotate = require("../annotate");
9
+
10
+ var _composition = require("./composition");
11
+
12
+ var _number = require("./number");
13
+
14
+ function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
15
+
16
+ function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { "default": obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj["default"] = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
17
+
18
+ /**
19
+ * Decoder that only returns Ok for boolean inputs. Err otherwise.
20
+ */
21
+ var _boolean = function _boolean(blob) {
22
+ return typeof blob === 'boolean' ? Result.ok(blob) : Result.err((0, _annotate.annotate)(blob, 'Must be boolean'));
23
+ };
24
+ /**
25
+ * Decoder that returns true for all truthy values, and false otherwise. Never fails.
26
+ */
27
+
28
+
29
+ exports["boolean"] = _boolean;
30
+
31
+ var truthy = function truthy(blob) {
32
+ return Result.ok(!!blob);
33
+ };
34
+ /**
35
+ * Decoder that only returns Ok for numeric input values representing booleans.
36
+ * Returns their boolean representation. Err otherwise.
37
+ */
38
+
39
+
40
+ exports.truthy = truthy;
41
+ var numericBoolean = (0, _composition.map)(_number.number, function (n) {
42
+ return !!n;
43
+ });
44
+ exports.numericBoolean = numericBoolean;
@@ -1,24 +1,25 @@
1
1
  // @flow strict
2
2
 
3
- import { annotate } from 'debrief';
4
- import { Err, Ok } from 'lemons/Result';
5
-
3
+ import * as Result from '../result';
4
+ import { annotate } from '../annotate';
5
+ import { map } from './composition';
6
6
  import { number } from './number';
7
- import type { Decoder } from './types';
8
- import { map } from './utils';
7
+ import type { Decoder } from '../_types';
9
8
 
10
9
  /**
11
10
  * Decoder that only returns Ok for boolean inputs. Err otherwise.
12
11
  */
13
12
  export const boolean: Decoder<boolean> = (blob: mixed) => {
14
- return typeof blob === 'boolean' ? Ok(blob) : Err(annotate(blob, 'Must be boolean'));
13
+ return typeof blob === 'boolean'
14
+ ? Result.ok(blob)
15
+ : Result.err(annotate(blob, 'Must be boolean'));
15
16
  };
16
17
 
17
18
  /**
18
19
  * Decoder that returns true for all truthy values, and false otherwise. Never fails.
19
20
  */
20
21
  export const truthy: Decoder<boolean> = (blob: mixed) => {
21
- return Ok(!!blob);
22
+ return Result.ok(!!blob);
22
23
  };
23
24
 
24
25
  /**
@@ -0,0 +1,56 @@
1
+ "use strict";
2
+
3
+ exports.__esModule = true;
4
+ exports.compose = compose;
5
+ exports.map = map;
6
+ exports.predicate = predicate;
7
+
8
+ var Result = _interopRequireWildcard(require("../result"));
9
+
10
+ var _annotate = require("../annotate");
11
+
12
+ function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
13
+
14
+ function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { "default": obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj["default"] = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
15
+
16
+ /**
17
+ * Given a decoder T and a mapping function from T's to V's, returns a decoder
18
+ * for V's. This is useful to change the original input data.
19
+ */
20
+ function map(decoder, mapper) {
21
+ return compose(decoder, function (x) {
22
+ try {
23
+ return Result.ok(mapper(x));
24
+ } catch (e) {
25
+ return Result.err((0, _annotate.annotate)(x, e instanceof Error ? e.message : String(e)));
26
+ }
27
+ });
28
+ }
29
+ /**
30
+ * Compose two decoders by passing the result of the first into the second.
31
+ * The second decoder may assume as its input type the output type of the first
32
+ * decoder (so it's not necessary to accept the typical "mixed"). This is
33
+ * useful for "narrowing down" the checks. For example, if you want to write
34
+ * a decoder for positive numbers, you can compose it from an existing decoder
35
+ * for any number, and a decoder that, assuming a number, checks if it's
36
+ * positive. Very often combined with the predicate() helper as the second
37
+ * argument.
38
+ */
39
+
40
+
41
+ function compose(decoder, next) {
42
+ return function (blob) {
43
+ return Result.andThen(decoder(blob), next);
44
+ };
45
+ }
46
+ /**
47
+ * Factory function returning a Decoder<T>, given a predicate function that
48
+ * accepts/rejects the input of type T.
49
+ */
50
+
51
+
52
+ function predicate(predicate, msg) {
53
+ return function (value) {
54
+ return predicate(value) ? Result.ok(value) : Result.err((0, _annotate.annotate)(value, msg));
55
+ };
56
+ }
@@ -1,23 +1,8 @@
1
1
  // @flow strict
2
2
 
3
- import { annotate } from 'debrief';
4
- import { Err, Ok } from 'lemons/Result';
5
-
6
- import type { Decoder } from './types';
7
-
8
- /**
9
- * `x instanceof Date` checks are unreliable across stack frames (that information
10
- * might get lost by the JS runtime), so we'll have to reside to more runtime
11
- * inspection checks.
12
- *
13
- * Taken from https://stackoverflow.com/a/44198641
14
- */
15
- export const isDate = (value: mixed): boolean %checks =>
16
- value !== undefined &&
17
- value !== null &&
18
- // $FlowFixMe[method-unbinding]
19
- Object.prototype.toString.call(value) === '[object Date]' &&
20
- !isNaN(value);
3
+ import * as Result from '../result';
4
+ import { annotate } from '../annotate';
5
+ import type { Decoder } from '../_types';
21
6
 
22
7
  /**
23
8
  * Given a decoder T and a mapping function from T's to V's, returns a decoder
@@ -26,9 +11,9 @@ export const isDate = (value: mixed): boolean %checks =>
26
11
  export function map<T, V>(decoder: Decoder<T>, mapper: (T) => V): Decoder<V> {
27
12
  return compose(decoder, (x) => {
28
13
  try {
29
- return Ok(mapper(x));
14
+ return Result.ok(mapper(x));
30
15
  } catch (e) {
31
- return Err(annotate(x, e instanceof Error ? e.message : String(e)));
16
+ return Result.err(annotate(x, e instanceof Error ? e.message : String(e)));
32
17
  }
33
18
  });
34
19
  }
@@ -44,7 +29,7 @@ export function map<T, V>(decoder: Decoder<T>, mapper: (T) => V): Decoder<V> {
44
29
  * argument.
45
30
  */
46
31
  export function compose<T, V>(decoder: Decoder<T>, next: Decoder<V, T>): Decoder<V> {
47
- return (blob: mixed) => decoder(blob).andThen(next);
32
+ return (blob: mixed) => Result.andThen(decoder(blob), next);
48
33
  }
49
34
 
50
35
  /**
@@ -53,6 +38,6 @@ export function compose<T, V>(decoder: Decoder<T>, next: Decoder<V, T>): Decoder
53
38
  */
54
39
  export function predicate<T>(predicate: (T) => boolean, msg: string): Decoder<T, T> {
55
40
  return (value: T) => {
56
- return predicate(value) ? Ok(value) : Err(annotate(value, msg));
41
+ return predicate(value) ? Result.ok(value) : Result.err(annotate(value, msg));
57
42
  };
58
43
  }
@@ -0,0 +1,69 @@
1
+ "use strict";
2
+
3
+ exports.__esModule = true;
4
+ exports.constant = constant;
5
+ exports.hardcoded = hardcoded;
6
+ exports.unknown = exports.undefined_ = exports.null_ = exports.mixed = void 0;
7
+
8
+ var Result = _interopRequireWildcard(require("../result"));
9
+
10
+ var _annotate = require("../annotate");
11
+
12
+ function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
13
+
14
+ function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { "default": obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj["default"] = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
15
+
16
+ /**
17
+ * Decoder that only returns Ok for `null` inputs. Err otherwise.
18
+ */
19
+ var null_ = function null_(blob) {
20
+ return blob === null ? Result.ok(blob) : Result.err((0, _annotate.annotate)(blob, 'Must be null'));
21
+ };
22
+ /**
23
+ * Decoder that only returns Ok for `undefined` inputs. Err otherwise.
24
+ */
25
+
26
+
27
+ exports.null_ = null_;
28
+
29
+ var undefined_ = function undefined_(blob) {
30
+ return blob === undefined ? Result.ok(blob) : Result.err((0, _annotate.annotate)(blob, 'Must be undefined'));
31
+ };
32
+ /**
33
+ * Decoder that only returns Ok for the given value constant. Err otherwise.
34
+ */
35
+
36
+
37
+ exports.undefined_ = undefined_;
38
+
39
+ function constant(value) {
40
+ return function (blob) {
41
+ return blob === value ? Result.ok(value) : Result.err((0, _annotate.annotate)(blob, "Must be constant " + String(value)));
42
+ };
43
+ }
44
+ /**
45
+ * Decoder that always returns Ok for the given hardcoded value, no matter what the input.
46
+ */
47
+
48
+
49
+ function hardcoded(value) {
50
+ return function () {
51
+ return Result.ok(value);
52
+ };
53
+ }
54
+ /**
55
+ * Decoder that always returns Ok for the given hardcoded value, no matter what the input.
56
+ */
57
+
58
+
59
+ var unknown = function unknown(blob) {
60
+ return Result.ok(blob);
61
+ };
62
+ /**
63
+ * Alias of unknown.
64
+ */
65
+
66
+
67
+ exports.unknown = unknown;
68
+ var mixed = unknown;
69
+ exports.mixed = mixed;