decoders 1.25.5 → 2.0.0-beta2

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 (151) hide show
  1. package/CHANGELOG.md +23 -3
  2. package/_esm/_guard.js +15 -0
  3. package/_esm/_guard.js.flow +20 -0
  4. package/_esm/_types.js +0 -0
  5. package/_esm/_types.js.flow +20 -0
  6. package/_esm/_utils.js +93 -0
  7. package/_esm/_utils.js.flow +97 -0
  8. package/_esm/annotate.js +144 -0
  9. package/_esm/annotate.js.flow +218 -0
  10. package/_esm/core/array.js +91 -0
  11. package/{array.js.flow → _esm/core/array.js.flow} +22 -25
  12. package/_esm/core/boolean.js +28 -0
  13. package/{boolean.js.flow → _esm/core/boolean.js.flow} +8 -7
  14. package/_esm/core/composition.js +42 -0
  15. package/{utils.js.flow → _esm/core/composition.js.flow} +7 -22
  16. package/_esm/core/constants.js +46 -0
  17. package/{constants.js.flow → _esm/core/constants.js.flow} +13 -12
  18. package/_esm/core/date.js +28 -0
  19. package/{date.js.flow → _esm/core/date.js.flow} +9 -7
  20. package/{describe.js → _esm/core/describe.js} +5 -11
  21. package/{describe.js.flow → _esm/core/describe.js.flow} +4 -4
  22. package/{dispatch.js → _esm/core/dispatch.js} +8 -15
  23. package/{dispatch.js.flow → _esm/core/dispatch.js.flow} +6 -5
  24. package/_esm/core/either.js +90 -0
  25. package/{either.js.flow → _esm/core/either.js.flow} +55 -35
  26. package/_esm/core/fail.js +11 -0
  27. package/_esm/core/fail.js.flow +12 -0
  28. package/_esm/core/instanceOf.js +8 -0
  29. package/{instanceOf.js.flow → _esm/core/instanceOf.js.flow} +7 -8
  30. package/_esm/core/json.js +15 -0
  31. package/{json.js.flow → _esm/core/json.js.flow} +4 -4
  32. package/_esm/core/lazy.js +11 -0
  33. package/{lazy.js.flow → _esm/core/lazy.js.flow} +1 -1
  34. package/_esm/core/mapping.js +54 -0
  35. package/_esm/core/mapping.js.flow +54 -0
  36. package/_esm/core/number.js +25 -0
  37. package/{number.js.flow → _esm/core/number.js.flow} +10 -11
  38. package/_esm/core/object.js +175 -0
  39. package/{object.js.flow → _esm/core/object.js.flow} +44 -52
  40. package/_esm/core/optional.js +38 -0
  41. package/{optional.js.flow → _esm/core/optional.js.flow} +6 -7
  42. package/{string.js → _esm/core/string.js} +18 -35
  43. package/{string.js.flow → _esm/core/string.js.flow} +13 -12
  44. package/_esm/core/tuple.js +155 -0
  45. package/{tuple.js.flow → _esm/core/tuple.js.flow} +62 -63
  46. package/_esm/format/index.js +2 -0
  47. package/_esm/format/index.js.flow +4 -0
  48. package/_esm/format/inline.js +137 -0
  49. package/_esm/format/inline.js.flow +122 -0
  50. package/_esm/format/short.js +4 -0
  51. package/_esm/format/short.js.flow +8 -0
  52. package/_esm/index.js +37 -0
  53. package/_esm/index.js.flow +56 -0
  54. package/_esm/result.js +139 -0
  55. package/_esm/result.js.flow +166 -0
  56. package/_guard.js +26 -0
  57. package/_guard.js.flow +20 -0
  58. package/_types.js +1 -0
  59. package/_types.js.flow +20 -0
  60. package/_utils.js +108 -0
  61. package/_utils.js.flow +97 -0
  62. package/annotate.js +161 -0
  63. package/annotate.js.flow +218 -0
  64. package/core/array.js +108 -0
  65. package/core/array.js.flow +103 -0
  66. package/core/boolean.js +44 -0
  67. package/core/boolean.js.flow +29 -0
  68. package/core/composition.js +56 -0
  69. package/core/composition.js.flow +43 -0
  70. package/core/constants.js +69 -0
  71. package/core/constants.js.flow +46 -0
  72. package/core/date.js +46 -0
  73. package/core/date.js.flow +40 -0
  74. package/core/describe.js +26 -0
  75. package/core/describe.js.flow +17 -0
  76. package/core/dispatch.js +62 -0
  77. package/core/dispatch.js.flow +58 -0
  78. package/core/either.js +117 -0
  79. package/core/either.js.flow +151 -0
  80. package/core/fail.js +21 -0
  81. package/core/fail.js.flow +12 -0
  82. package/core/instanceOf.js +19 -0
  83. package/core/instanceOf.js.flow +20 -0
  84. package/{json.js → core/json.js} +3 -5
  85. package/core/json.js.flow +28 -0
  86. package/{lazy.js → core/lazy.js} +1 -3
  87. package/core/lazy.js.flow +15 -0
  88. package/core/mapping.js +67 -0
  89. package/core/mapping.js.flow +54 -0
  90. package/core/number.js +40 -0
  91. package/core/number.js.flow +34 -0
  92. package/core/object.js +194 -0
  93. package/core/object.js.flow +203 -0
  94. package/core/optional.js +54 -0
  95. package/core/optional.js.flow +41 -0
  96. package/core/string.js +98 -0
  97. package/core/string.js.flow +82 -0
  98. package/core/tuple.js +173 -0
  99. package/core/tuple.js.flow +220 -0
  100. package/format/index.js +12 -0
  101. package/format/index.js.flow +4 -0
  102. package/format/inline.js +146 -0
  103. package/format/inline.js.flow +122 -0
  104. package/format/short.js +10 -0
  105. package/format/short.js.flow +8 -0
  106. package/index.js +118 -395
  107. package/index.js.flow +24 -24
  108. package/package.json +2 -6
  109. package/result.js +172 -0
  110. package/result.js.flow +166 -0
  111. package/array.d.ts +0 -5
  112. package/array.js +0 -133
  113. package/boolean.d.ts +0 -5
  114. package/boolean.js +0 -42
  115. package/constants.d.ts +0 -11
  116. package/constants.js +0 -67
  117. package/date.d.ts +0 -4
  118. package/date.js +0 -42
  119. package/describe.d.ts +0 -3
  120. package/dispatch.d.ts +0 -8
  121. package/either.d.ts +0 -61
  122. package/either.js +0 -85
  123. package/fail.d.ts +0 -3
  124. package/fail.js +0 -19
  125. package/fail.js.flow +0 -13
  126. package/guard.d.ts +0 -7
  127. package/guard.js +0 -30
  128. package/guard.js.flow +0 -36
  129. package/helpers.d.ts +0 -79
  130. package/index.d.ts +0 -38
  131. package/instanceOf.d.ts +0 -3
  132. package/instanceOf.js +0 -17
  133. package/json.d.ts +0 -11
  134. package/lazy.d.ts +0 -3
  135. package/mapping.d.ts +0 -4
  136. package/mapping.js +0 -113
  137. package/mapping.js.flow +0 -71
  138. package/number.d.ts +0 -6
  139. package/number.js +0 -38
  140. package/object.d.ts +0 -33
  141. package/object.js +0 -254
  142. package/optional.d.ts +0 -5
  143. package/optional.js +0 -52
  144. package/string.d.ts +0 -7
  145. package/tuple.d.ts +0 -30
  146. package/tuple.js +0 -199
  147. package/types.d.ts +0 -18
  148. package/types.js +0 -5
  149. package/types.js.flow +0 -26
  150. package/utils.d.ts +0 -13
  151. package/utils.js +0 -70
@@ -0,0 +1,91 @@
1
+ import * as Result from '../result';
2
+ import { annotate } from '../annotate';
3
+ import { compose, predicate } from './composition';
4
+
5
+ /**
6
+ * Like a "Plain Old JavaScript Object", but for arrays: "Plain Old JavaScript
7
+ * Array" ^_^
8
+ */
9
+ export var poja = function poja(blob) {
10
+ if (!Array.isArray(blob)) {
11
+ return Result.err(annotate(blob, 'Must be an array'));
12
+ }
13
+
14
+ return Result.ok( // NOTE: Since Flow 0.98, Array.isArray() returns $ReadOnlyArray<mixed>
15
+ // instead of Array<mixed>. For rationale, see
16
+ // https://github.com/facebook/flow/issues/7684. In this case, we
17
+ // don't want to output read-only types because it's up to the user of
18
+ // decoders to determine what they want to do with the decoded output.
19
+ // If they want to write items into the array, that's fine!
20
+ // The fastest way to turn a read-only array into a normal array in
21
+ // Javascript is to use .slice() on it, see this benchmark:
22
+ // http://jsben.ch/lO6C5
23
+ blob.slice());
24
+ };
25
+ /**
26
+ * Given an array of Result instances, loop over them all and return:
27
+ * - An [index, err] tuple, indicating the (index of the) first Err instance
28
+ * encountered; or
29
+ * - a new Ok with an array of all unwrapped Ok'ed values
30
+ */
31
+
32
+ function all(items, blobs) {
33
+ var results = [];
34
+
35
+ for (var index = 0; index < items.length; ++index) {
36
+ var result = items[index];
37
+
38
+ if (result.type === 'ok') {
39
+ results.push(result.value);
40
+ } else {
41
+ var ann = result.error; // Rewrite the annotation to include the index information, and inject it into the original blob
42
+
43
+ var clone = [].concat(blobs);
44
+ clone.splice(index, 1, annotate(ann, ann.text ? ann.text + " (at index " + index + ")" : "index " + index)); // const errValue = [];
45
+ // if (index > 0) {
46
+ // errValue.push('...'); // TODO: make special mark, not string!
47
+ // }
48
+ // errValue.push(
49
+ // );
50
+ // if (index < iterable.length - 1) {
51
+ // errValue.push('...'); // TODO: make special mark, not string!
52
+ // }
53
+
54
+ return Result.err(annotate(clone));
55
+ }
56
+ }
57
+
58
+ return Result.ok(results);
59
+ }
60
+ /**
61
+ * Given a T, builds a decoder that assumes an array input and returns an
62
+ * Array<T>.
63
+ */
64
+
65
+
66
+ function members(decoder) {
67
+ return function (blobs) {
68
+ var results = blobs.map(decoder);
69
+ var result = all(results, blobs);
70
+ return result;
71
+ };
72
+ }
73
+ /**
74
+ * Builds a Decoder that returns Ok for values of `Array<T>`, given a Decoder
75
+ * for `T`. Err otherwise.
76
+ */
77
+
78
+
79
+ export function array(decoder) {
80
+ return compose(poja, members(decoder));
81
+ }
82
+ /**
83
+ * Builds a Decoder that returns Ok for values of `Array<T>`, but will reject
84
+ * empty arrays.
85
+ */
86
+
87
+ export function nonEmptyArray(decoder) {
88
+ return compose(array(decoder), predicate(function (arr) {
89
+ return arr.length > 0;
90
+ }, 'Must be non-empty array'));
91
+ }
@@ -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,28 @@
1
+ import * as Result from '../result';
2
+ import { annotate } from '../annotate';
3
+ import { map } from './composition';
4
+ import { number } from './number';
5
+
6
+ /**
7
+ * Decoder that only returns Ok for boolean inputs. Err otherwise.
8
+ */
9
+ var _boolean = function _boolean(blob) {
10
+ return typeof blob === 'boolean' ? Result.ok(blob) : Result.err(annotate(blob, 'Must be boolean'));
11
+ };
12
+ /**
13
+ * Decoder that returns true for all truthy values, and false otherwise. Never fails.
14
+ */
15
+
16
+
17
+ export { _boolean as boolean };
18
+ export var truthy = function truthy(blob) {
19
+ return Result.ok(!!blob);
20
+ };
21
+ /**
22
+ * Decoder that only returns Ok for numeric input values representing booleans.
23
+ * Returns their boolean representation. Err otherwise.
24
+ */
25
+
26
+ export var numericBoolean = map(number, function (n) {
27
+ return !!n;
28
+ });
@@ -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,42 @@
1
+ import * as Result from '../result';
2
+ import { annotate } from '../annotate';
3
+
4
+ /**
5
+ * Given a decoder T and a mapping function from T's to V's, returns a decoder
6
+ * for V's. This is useful to change the original input data.
7
+ */
8
+ export function map(decoder, mapper) {
9
+ return compose(decoder, function (x) {
10
+ try {
11
+ return Result.ok(mapper(x));
12
+ } catch (e) {
13
+ return Result.err(annotate(x, e instanceof Error ? e.message : String(e)));
14
+ }
15
+ });
16
+ }
17
+ /**
18
+ * Compose two decoders by passing the result of the first into the second.
19
+ * The second decoder may assume as its input type the output type of the first
20
+ * decoder (so it's not necessary to accept the typical "mixed"). This is
21
+ * useful for "narrowing down" the checks. For example, if you want to write
22
+ * a decoder for positive numbers, you can compose it from an existing decoder
23
+ * for any number, and a decoder that, assuming a number, checks if it's
24
+ * positive. Very often combined with the predicate() helper as the second
25
+ * argument.
26
+ */
27
+
28
+ export function compose(decoder, next) {
29
+ return function (blob) {
30
+ return Result.andThen(decoder(blob), next);
31
+ };
32
+ }
33
+ /**
34
+ * Factory function returning a Decoder<T>, given a predicate function that
35
+ * accepts/rejects the input of type T.
36
+ */
37
+
38
+ export function predicate(predicate, msg) {
39
+ return function (value) {
40
+ return predicate(value) ? Result.ok(value) : Result.err(annotate(value, msg));
41
+ };
42
+ }
@@ -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,46 @@
1
+ import * as Result from '../result';
2
+ import { annotate } from '../annotate';
3
+
4
+ /**
5
+ * Decoder that only returns Ok for `null` inputs. Err otherwise.
6
+ */
7
+ export var null_ = function null_(blob) {
8
+ return blob === null ? Result.ok(blob) : Result.err(annotate(blob, 'Must be null'));
9
+ };
10
+ /**
11
+ * Decoder that only returns Ok for `undefined` inputs. Err otherwise.
12
+ */
13
+
14
+ export var undefined_ = function undefined_(blob) {
15
+ return blob === undefined ? Result.ok(blob) : Result.err(annotate(blob, 'Must be undefined'));
16
+ };
17
+ /**
18
+ * Decoder that only returns Ok for the given value constant. Err otherwise.
19
+ */
20
+
21
+ export function constant(value) {
22
+ return function (blob) {
23
+ return blob === value ? Result.ok(value) : Result.err(annotate(blob, "Must be constant " + String(value)));
24
+ };
25
+ }
26
+ /**
27
+ * Decoder that always returns Ok for the given hardcoded value, no matter what the input.
28
+ */
29
+
30
+ export function hardcoded(value) {
31
+ return function () {
32
+ return Result.ok(value);
33
+ };
34
+ }
35
+ /**
36
+ * Decoder that always returns Ok for the given hardcoded value, no matter what the input.
37
+ */
38
+
39
+ export var unknown = function unknown(blob) {
40
+ return Result.ok(blob);
41
+ };
42
+ /**
43
+ * Alias of unknown.
44
+ */
45
+
46
+ export var mixed = unknown;
@@ -1,21 +1,22 @@
1
1
  // @flow strict
2
2
 
3
- import { annotate } from 'debrief';
4
- import { Err, Ok } from 'lemons/Result';
5
-
6
- import type { Decoder, Scalar } from './types';
3
+ import * as Result from '../result';
4
+ import { annotate } from '../annotate';
5
+ import type { Decoder, Scalar } from '../_types';
7
6
 
8
7
  /**
9
8
  * Decoder that only returns Ok for `null` inputs. Err otherwise.
10
9
  */
11
10
  export const null_: Decoder<null> = (blob: mixed) =>
12
- blob === null ? Ok(blob) : Err(annotate(blob, 'Must be null'));
11
+ blob === null ? Result.ok(blob) : Result.err(annotate(blob, 'Must be null'));
13
12
 
14
13
  /**
15
14
  * Decoder that only returns Ok for `undefined` inputs. Err otherwise.
16
15
  */
17
16
  export const undefined_: Decoder<void> = (blob: mixed) =>
18
- blob === undefined ? Ok(blob) : Err(annotate(blob, 'Must be undefined'));
17
+ blob === undefined
18
+ ? Result.ok(blob)
19
+ : Result.err(annotate(blob, 'Must be undefined'));
19
20
 
20
21
  /**
21
22
  * Decoder that only returns Ok for the given value constant. Err otherwise.
@@ -23,23 +24,23 @@ export const undefined_: Decoder<void> = (blob: mixed) =>
23
24
  export function constant<T: Scalar>(value: T): Decoder<T> {
24
25
  return (blob: mixed) =>
25
26
  blob === value
26
- ? Ok(value)
27
- : Err(annotate(blob, `Must be constant ${String(value)}`));
27
+ ? Result.ok(value)
28
+ : Result.err(annotate(blob, `Must be constant ${String(value)}`));
28
29
  }
29
30
 
30
31
  /**
31
32
  * Decoder that always returns Ok for the given hardcoded value, no matter what the input.
32
33
  */
33
34
  export function hardcoded<T>(value: T): Decoder<T> {
34
- return (_: mixed) => Ok(value);
35
+ return () => Result.ok(value);
35
36
  }
36
37
 
37
38
  /**
38
39
  * Decoder that always returns Ok for the given hardcoded value, no matter what the input.
39
40
  */
40
- export const mixed: Decoder<mixed> = (blob: mixed) => Ok((blob: mixed));
41
+ export const unknown: Decoder<mixed> = (blob: mixed) => Result.ok(blob);
41
42
 
42
43
  /**
43
- * Alias of mixed.
44
+ * Alias of unknown.
44
45
  */
45
- export const unknown = mixed;
46
+ export const mixed: Decoder<mixed> = unknown;
@@ -0,0 +1,28 @@
1
+ import * as Result from '../result';
2
+ import { annotate } from '../annotate';
3
+ import { isDate } from '../_utils';
4
+ import { map } from './composition';
5
+ import { regex } from './string';
6
+ // Only matches the shape. This "over-matches" some values that still aren't
7
+ // valid dates (like 9999-99-99), but those will be caught by JS Date's
8
+ // internal validations
9
+ var iso8601_re = /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(?:[.]\d+)?(?:Z|[+-]\d{2}:?\d{2})$/;
10
+ export var date = function date(value) {
11
+ return isDate(value) ? Result.ok(value) : Result.err(annotate(value, 'Must be a Date'));
12
+ };
13
+ /**
14
+ * Decoder that only returns Ok for strings that are valid ISO8601 date
15
+ * strings. Err otherwise.
16
+ */
17
+
18
+ export var iso8601 = map( // Input itself needs to match the ISO8601 regex...
19
+ regex(iso8601_re, 'Must be ISO8601 format'), // Make sure it is a _valid_ date
20
+ function (value) {
21
+ var date = new Date(value);
22
+
23
+ if (isNaN(date.getTime())) {
24
+ throw new Error('Must be valid date/time value');
25
+ }
26
+
27
+ return date;
28
+ });
@@ -1,11 +1,11 @@
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 { isDate } from '../_utils';
6
+ import { map } from './composition';
6
7
  import { regex } from './string';
7
- import type { Decoder } from './types';
8
- import { isDate, map } from './utils';
8
+ import type { Decoder } from '../_types';
9
9
 
10
10
  // $FlowFixMe[unclear-type] (not really an issue) - deliberate casting
11
11
  type cast = any;
@@ -17,7 +17,9 @@ const iso8601_re =
17
17
  /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(?:[.]\d+)?(?:Z|[+-]\d{2}:?\d{2})$/;
18
18
 
19
19
  export const date: Decoder<Date> = (value: mixed) =>
20
- isDate(value) ? Ok(((value: cast): Date)) : Err(annotate(value, 'Must be a Date'));
20
+ isDate(value)
21
+ ? Result.ok(((value: cast): Date))
22
+ : Result.err(annotate(value, 'Must be a Date'));
21
23
 
22
24
  /**
23
25
  * Decoder that only returns Ok for strings that are valid ISO8601 date
@@ -34,5 +36,5 @@ export const iso8601: Decoder<Date> = map(
34
36
  throw new Error('Must be valid date/time value');
35
37
  }
36
38
  return date;
37
- }
39
+ },
38
40
  );
@@ -1,11 +1,5 @@
1
- "use strict";
2
-
3
- Object.defineProperty(exports, "__esModule", {
4
- value: true
5
- });
6
- exports.describe = describe;
7
-
8
- var _debrief = require("debrief");
1
+ import * as Result from '../result';
2
+ import { annotate } from '../annotate';
9
3
 
10
4
  /**
11
5
  * Wrap another decoder, and override the error message in case it fails. This
@@ -13,10 +7,10 @@ var _debrief = require("debrief");
13
7
  * use language in those error messages that can be relayed to end users (for
14
8
  * example to show in form errors).
15
9
  */
16
- function describe(decoder, message) {
10
+ export function describe(decoder, message) {
17
11
  return function (blob) {
18
- return decoder(blob).mapError(function (err) {
19
- return (0, _debrief.annotate)(err, message);
12
+ return Result.mapError(decoder(blob), function (err) {
13
+ return annotate(err, message);
20
14
  });
21
15
  };
22
16
  }
@@ -1,8 +1,8 @@
1
1
  // @flow strict
2
2
 
3
- import { annotate } from 'debrief';
4
-
5
- import type { Decoder } from './types';
3
+ import * as Result from '../result';
4
+ import { annotate } from '../annotate';
5
+ import type { Decoder } from '../_types';
6
6
 
7
7
  /**
8
8
  * Wrap another decoder, and override the error message in case it fails. This
@@ -12,6 +12,6 @@ import type { Decoder } from './types';
12
12
  */
13
13
  export function describe<T>(decoder: Decoder<T>, message: string): Decoder<T> {
14
14
  return (blob: mixed) => {
15
- return decoder(blob).mapError((err) => annotate(err, message));
15
+ return Result.mapError(decoder(blob), (err) => annotate(err, message));
16
16
  };
17
17
  }
@@ -1,15 +1,6 @@
1
- "use strict";
2
-
3
- Object.defineProperty(exports, "__esModule", {
4
- value: true
5
- });
6
- exports.dispatch = dispatch;
7
-
8
- var _either = require("./either");
9
-
10
- var _object2 = require("./object");
11
-
12
- function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
1
+ import * as Result from '../result';
2
+ import { object } from './object';
3
+ import { oneOf } from './either';
13
4
 
14
5
  /**
15
6
  * Dispatches to one of several given decoders, based on the value found at
@@ -46,10 +37,12 @@ function _defineProperty(obj, key, value) { if (key in obj) { Object.definePrope
46
37
  *
47
38
  * Also, the error messages will be less ambiguous using `dispatch()`.
48
39
  */
49
- function dispatch(field, mapping) {
50
- var base = (0, _object2.object)(_defineProperty({}, field, (0, _either.oneOf)(Object.keys(mapping))));
40
+ export function dispatch(field, mapping) {
41
+ var _object;
42
+
43
+ var base = object((_object = {}, _object[field] = oneOf(Object.keys(mapping)), _object));
51
44
  return function (blob) {
52
- return base(blob).andThen(function (baseObj) {
45
+ return Result.andThen(base(blob), function (baseObj) {
53
46
  var decoderName = baseObj[field];
54
47
  var decoder = mapping[decoderName];
55
48
  return decoder(blob);
@@ -1,8 +1,9 @@
1
1
  // @flow strict
2
2
 
3
- import { oneOf } from './either';
3
+ import * as Result from '../result';
4
4
  import { object } from './object';
5
- import type { $DecoderType, Decoder } from './types';
5
+ import { oneOf } from './either';
6
+ import type { Decoder, DecoderType } from '../_types';
6
7
 
7
8
  // $FlowFixMe[unclear-type] (not really an issue) - deliberate use of `any` - not sure how we should get rid of this
8
9
  type anything = any;
@@ -44,11 +45,11 @@ type anything = any;
44
45
  */
45
46
  export function dispatch<O: { +[field: string]: Decoder<anything>, ... }>(
46
47
  field: string,
47
- mapping: O
48
- ): Decoder<$Values<$ObjMap<O, $DecoderType>>> {
48
+ mapping: O,
49
+ ): Decoder<$Values<$ObjMap<O, DecoderType>>> {
49
50
  const base = object({ [field]: oneOf(Object.keys(mapping)) });
50
51
  return (blob: mixed) => {
51
- return base(blob).andThen((baseObj) => {
52
+ return Result.andThen(base(blob), (baseObj) => {
52
53
  const decoderName = baseObj[field];
53
54
  const decoder = mapping[decoderName];
54
55
  return decoder(blob);