decoders 2.0.0-beta8 → 2.0.1

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