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
package/tuple.js DELETED
@@ -1,199 +0,0 @@
1
- "use strict";
2
-
3
- Object.defineProperty(exports, "__esModule", {
4
- value: true
5
- });
6
- exports.tuple1 = tuple1;
7
- exports.tuple2 = tuple2;
8
- exports.tuple3 = tuple3;
9
- exports.tuple4 = tuple4;
10
- exports.tuple5 = tuple5;
11
- exports.tuple6 = tuple6;
12
-
13
- var _debrief = require("debrief");
14
-
15
- var _Result = require("lemons/Result");
16
-
17
- var _array = require("./array");
18
-
19
- var _utils = require("./utils");
20
-
21
- function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }
22
-
23
- function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
24
-
25
- function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
26
-
27
- function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; }
28
-
29
- function _iterableToArrayLimit(arr, i) { var _i = arr == null ? null : typeof Symbol !== "undefined" && arr[Symbol.iterator] || arr["@@iterator"]; if (_i == null) return; var _arr = []; var _n = true; var _d = false; var _s, _e; try { for (_i = _i.call(arr); !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"] != null) _i["return"](); } finally { if (_d) throw _e; } } return _arr; }
30
-
31
- function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
32
-
33
- var ntuple = function ntuple(n) {
34
- return (0, _utils.compose)(_array.poja, (0, _utils.predicate)(function (arr) {
35
- return arr.length === n;
36
- }, "Must be a ".concat(n, "-tuple")));
37
- };
38
- /**
39
- * Builds a Decoder that returns Ok for 1-tuple of [T], given a Decoder for T.
40
- * Err otherwise.
41
- */
42
-
43
-
44
- function tuple1(decoder1) {
45
- return (0, _utils.compose)(ntuple(1), function (blobs) {
46
- var _blobs = _slicedToArray(blobs, 1),
47
- blob1 = _blobs[0];
48
-
49
- var result1 = decoder1(blob1);
50
-
51
- try {
52
- return (0, _Result.Ok)([result1.unwrap()]);
53
- } catch (e) {
54
- // If a decoder error has happened while unwrapping all the
55
- // results, try to construct a good error message
56
- return (0, _Result.Err)((0, _debrief.annotate)(result1.errValue()));
57
- }
58
- });
59
- }
60
- /**
61
- * Builds a Decoder that returns Ok for 2-tuples of [T1, T2], given Decoders
62
- * for T1 and T2. Err otherwise.
63
- */
64
-
65
-
66
- function tuple2(decoder1, decoder2) {
67
- return (0, _utils.compose)(ntuple(2), function (blobs) {
68
- var _blobs2 = _slicedToArray(blobs, 2),
69
- blob1 = _blobs2[0],
70
- blob2 = _blobs2[1];
71
-
72
- var result1 = decoder1(blob1);
73
- var result2 = decoder2(blob2);
74
-
75
- try {
76
- return (0, _Result.Ok)([result1.unwrap(), result2.unwrap()]);
77
- } catch (e) {
78
- // If a decoder error has happened while unwrapping all the
79
- // results, try to construct a good error message
80
- return (0, _Result.Err)((0, _debrief.annotate)([result1.isErr() ? result1.errValue() : result1.value(), result2.isErr() ? result2.errValue() : result2.value()]));
81
- }
82
- });
83
- }
84
- /**
85
- * Builds a Decoder that returns Ok for 3-tuples of [T1, T2, T3], given
86
- * Decoders for T1, T2, and T3. Err otherwise.
87
- */
88
-
89
-
90
- function tuple3(decoder1, decoder2, decoder3) {
91
- return (0, _utils.compose)(ntuple(3), function (blobs) {
92
- var _blobs3 = _slicedToArray(blobs, 3),
93
- blob1 = _blobs3[0],
94
- blob2 = _blobs3[1],
95
- blob3 = _blobs3[2];
96
-
97
- var result1 = decoder1(blob1);
98
- var result2 = decoder2(blob2);
99
- var result3 = decoder3(blob3);
100
-
101
- try {
102
- return (0, _Result.Ok)([result1.unwrap(), result2.unwrap(), result3.unwrap()]);
103
- } catch (e) {
104
- // If a decoder error has happened while unwrapping all the
105
- // results, try to construct a good error message
106
- return (0, _Result.Err)((0, _debrief.annotate)([result1.isErr() ? result1.errValue() : result1.value(), result2.isErr() ? result2.errValue() : result2.value(), result3.isErr() ? result3.errValue() : result3.value()]));
107
- }
108
- });
109
- }
110
- /**
111
- * Builds a Decoder that returns Ok for 4-tuples of [T1, T2, T3, T4], given
112
- * Decoders for T1, T2, T3, and T4. Err otherwise.
113
- */
114
-
115
-
116
- function tuple4(decoder1, decoder2, decoder3, decoder4) {
117
- return (0, _utils.compose)(ntuple(4), function (blobs) {
118
- var _blobs4 = _slicedToArray(blobs, 4),
119
- blob1 = _blobs4[0],
120
- blob2 = _blobs4[1],
121
- blob3 = _blobs4[2],
122
- blob4 = _blobs4[3];
123
-
124
- var result1 = decoder1(blob1);
125
- var result2 = decoder2(blob2);
126
- var result3 = decoder3(blob3);
127
- var result4 = decoder4(blob4);
128
-
129
- try {
130
- return (0, _Result.Ok)([result1.unwrap(), result2.unwrap(), result3.unwrap(), result4.unwrap()]);
131
- } catch (e) {
132
- // If a decoder error has happened while unwrapping all the
133
- // results, try to construct a good error message
134
- return (0, _Result.Err)((0, _debrief.annotate)([result1.isErr() ? result1.errValue() : result1.value(), result2.isErr() ? result2.errValue() : result2.value(), result3.isErr() ? result3.errValue() : result3.value(), result4.isErr() ? result4.errValue() : result4.value()]));
135
- }
136
- });
137
- }
138
- /**
139
- * Builds a Decoder that returns Ok for 5-tuples of [T1, T2, T3, T4, T5], given
140
- * Decoders for T1, T2, T3, T4, and T5. Err otherwise.
141
- */
142
-
143
-
144
- function tuple5(decoder1, decoder2, decoder3, decoder4, decoder5) {
145
- return (0, _utils.compose)(ntuple(5), function (blobs) {
146
- var _blobs5 = _slicedToArray(blobs, 5),
147
- blob1 = _blobs5[0],
148
- blob2 = _blobs5[1],
149
- blob3 = _blobs5[2],
150
- blob4 = _blobs5[3],
151
- blob5 = _blobs5[4];
152
-
153
- var result1 = decoder1(blob1);
154
- var result2 = decoder2(blob2);
155
- var result3 = decoder3(blob3);
156
- var result4 = decoder4(blob4);
157
- var result5 = decoder5(blob5);
158
-
159
- try {
160
- return (0, _Result.Ok)([result1.unwrap(), result2.unwrap(), result3.unwrap(), result4.unwrap(), result5.unwrap()]);
161
- } catch (e) {
162
- // If a decoder error has happened while unwrapping all the
163
- // results, try to construct a good error message
164
- return (0, _Result.Err)((0, _debrief.annotate)([result1.isErr() ? result1.errValue() : result1.value(), result2.isErr() ? result2.errValue() : result2.value(), result3.isErr() ? result3.errValue() : result3.value(), result4.isErr() ? result4.errValue() : result4.value(), result5.isErr() ? result5.errValue() : result5.value()]));
165
- }
166
- });
167
- }
168
- /**
169
- * Builds a Decoder that returns Ok for 5-tuples of [T1, T2, T3, T4, T5], given
170
- * Decoders for T1, T2, T3, T4, T5, and T6. Err otherwise.
171
- */
172
-
173
-
174
- function tuple6(decoder1, decoder2, decoder3, decoder4, decoder5, decoder6) {
175
- return (0, _utils.compose)(ntuple(6), function (blobs) {
176
- var _blobs6 = _slicedToArray(blobs, 6),
177
- blob1 = _blobs6[0],
178
- blob2 = _blobs6[1],
179
- blob3 = _blobs6[2],
180
- blob4 = _blobs6[3],
181
- blob5 = _blobs6[4],
182
- blob6 = _blobs6[5];
183
-
184
- var result1 = decoder1(blob1);
185
- var result2 = decoder2(blob2);
186
- var result3 = decoder3(blob3);
187
- var result4 = decoder4(blob4);
188
- var result5 = decoder5(blob5);
189
- var result6 = decoder6(blob6);
190
-
191
- try {
192
- return (0, _Result.Ok)([result1.unwrap(), result2.unwrap(), result3.unwrap(), result4.unwrap(), result5.unwrap(), result6.unwrap()]);
193
- } catch (e) {
194
- // If a decoder error has happened while unwrapping all the
195
- // results, try to construct a good error message
196
- return (0, _Result.Err)((0, _debrief.annotate)([result1.isErr() ? result1.errValue() : result1.value(), result2.isErr() ? result2.errValue() : result2.value(), result3.isErr() ? result3.errValue() : result3.value(), result4.isErr() ? result4.errValue() : result4.value(), result5.isErr() ? result5.errValue() : result5.value(), result6.isErr() ? result6.errValue() : result6.value()]));
197
- }
198
- });
199
- }
package/types.js DELETED
@@ -1,5 +0,0 @@
1
- "use strict";
2
-
3
- var _Result = _interopRequireDefault(require("lemons/Result"));
4
-
5
- function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
package/types.js.flow DELETED
@@ -1,26 +0,0 @@
1
- // @flow strict
2
-
3
- import type { Annotation } from 'debrief';
4
- import Result from 'lemons/Result';
5
-
6
- export type Scalar = string | number | boolean | symbol | void | null;
7
-
8
- // NOTE:
9
- // Normally, we should not be discarding Flow warnings about the use of the
10
- // "any" type. But in the case of decoders, it's the very purpose of the
11
- // library to accept *anything* as input. To avoid suppressing the "any"
12
- // warnings everywhere throughout this library, we'll suppress it here once,
13
- // and use this re-aliased version of "any" elsewhere.
14
-
15
- export type Guard<T> = (mixed) => T;
16
- export type Predicate<T> = (T) => boolean;
17
- export type DecodeResult<T> = Result<Annotation, T>;
18
- export type Decoder<T, F = mixed> = (F) => DecodeResult<T>;
19
-
20
- /**
21
- * A "type function" which informs Flow about how a type will be modified at runtime.
22
- * Read this as "given a Guard of type T, I can produce a value of type T". This
23
- * definition helps construct $ObjMap types.
24
- */
25
- export type $DecoderType = <T>(Decoder<T>) => T;
26
- export type $GuardType = <T>(Guard<T>) => T;
package/utils.js DELETED
@@ -1,70 +0,0 @@
1
- "use strict";
2
-
3
- Object.defineProperty(exports, "__esModule", {
4
- value: true
5
- });
6
- exports.compose = compose;
7
- exports.isDate = void 0;
8
- exports.map = map;
9
- exports.predicate = predicate;
10
-
11
- var _debrief = require("debrief");
12
-
13
- var _Result = require("lemons/Result");
14
-
15
- /**
16
- * `x instanceof Date` checks are unreliable across stack frames (that information
17
- * might get lost by the JS runtime), so we'll have to reside to more runtime
18
- * inspection checks.
19
- *
20
- * Taken from https://stackoverflow.com/a/44198641
21
- */
22
- var isDate = function isDate(value) {
23
- return value !== undefined && value !== null && // $FlowFixMe[method-unbinding]
24
- Object.prototype.toString.call(value) === '[object Date]' && !isNaN(value);
25
- };
26
- /**
27
- * Given a decoder T and a mapping function from T's to V's, returns a decoder
28
- * for V's. This is useful to change the original input data.
29
- */
30
-
31
-
32
- exports.isDate = isDate;
33
-
34
- function map(decoder, mapper) {
35
- return compose(decoder, function (x) {
36
- try {
37
- return (0, _Result.Ok)(mapper(x));
38
- } catch (e) {
39
- return (0, _Result.Err)((0, _debrief.annotate)(x, e instanceof Error ? e.message : String(e)));
40
- }
41
- });
42
- }
43
- /**
44
- * Compose two decoders by passing the result of the first into the second.
45
- * The second decoder may assume as its input type the output type of the first
46
- * decoder (so it's not necessary to accept the typical "mixed"). This is
47
- * useful for "narrowing down" the checks. For example, if you want to write
48
- * a decoder for positive numbers, you can compose it from an existing decoder
49
- * for any number, and a decoder that, assuming a number, checks if it's
50
- * positive. Very often combined with the predicate() helper as the second
51
- * argument.
52
- */
53
-
54
-
55
- function compose(decoder, next) {
56
- return function (blob) {
57
- return decoder(blob).andThen(next);
58
- };
59
- }
60
- /**
61
- * Factory function returning a Decoder<T>, given a predicate function that
62
- * accepts/rejects the input of type T.
63
- */
64
-
65
-
66
- function predicate(predicate, msg) {
67
- return function (value) {
68
- return predicate(value) ? (0, _Result.Ok)(value) : (0, _Result.Err)((0, _debrief.annotate)(value, msg));
69
- };
70
- }