decoders 1.26.0-test1 → 2.0.0-beta12

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 (128) hide show
  1. package/CHANGELOG.md +53 -3
  2. package/Decoder.d.ts +28 -0
  3. package/Decoder.js +223 -0
  4. package/Decoder.js.flow +250 -0
  5. package/Decoder.mjs +216 -0
  6. package/NotSupportedTSVersion.d.ts +1 -0
  7. package/README.md +105 -961
  8. package/_utils.d.ts +9 -0
  9. package/_utils.js +102 -0
  10. package/_utils.js.flow +93 -0
  11. package/_utils.mjs +89 -0
  12. package/annotate.d.ts +62 -0
  13. package/annotate.js +161 -0
  14. package/annotate.js.flow +218 -0
  15. package/annotate.mjs +144 -0
  16. package/format.d.ts +4 -0
  17. package/format.js +151 -0
  18. package/format.js.flow +126 -0
  19. package/format.mjs +140 -0
  20. package/index.d.ts +40 -0
  21. package/index.js +90 -0
  22. package/index.js.flow +44 -0
  23. package/index.mjs +11 -0
  24. package/{ts/helpers.d.ts → lib/_helpers.d.ts} +0 -0
  25. package/lib/arrays.d.ts +37 -0
  26. package/lib/arrays.js +138 -0
  27. package/lib/arrays.js.flow +138 -0
  28. package/lib/arrays.mjs +123 -0
  29. package/lib/basics.d.ts +38 -0
  30. package/lib/basics.js +134 -0
  31. package/lib/basics.js.flow +113 -0
  32. package/lib/basics.mjs +110 -0
  33. package/{ts/boolean.d.ts → lib/booleans.d.ts} +1 -1
  34. package/lib/booleans.js +35 -0
  35. package/lib/booleans.js.flow +22 -0
  36. package/lib/booleans.mjs +25 -0
  37. package/{ts/date.d.ts → lib/dates.d.ts} +1 -1
  38. package/lib/dates.js +44 -0
  39. package/lib/dates.js.flow +40 -0
  40. package/lib/dates.mjs +34 -0
  41. package/{ts → lib}/json.d.ts +1 -1
  42. package/lib/json.js +55 -0
  43. package/lib/json.js.flow +50 -0
  44. package/lib/json.mjs +40 -0
  45. package/{ts/number.d.ts → lib/numbers.d.ts} +2 -1
  46. package/lib/numbers.js +52 -0
  47. package/lib/numbers.js.flow +49 -0
  48. package/lib/numbers.mjs +42 -0
  49. package/{ts/object.d.ts → lib/objects.d.ts} +11 -6
  50. package/lib/objects.js +240 -0
  51. package/lib/objects.js.flow +246 -0
  52. package/lib/objects.mjs +223 -0
  53. package/lib/strings.d.ts +13 -0
  54. package/lib/strings.js +101 -0
  55. package/lib/strings.js.flow +90 -0
  56. package/lib/strings.mjs +82 -0
  57. package/lib/unions.d.ts +78 -0
  58. package/lib/unions.js +161 -0
  59. package/lib/unions.js.flow +158 -0
  60. package/lib/unions.mjs +145 -0
  61. package/lib/utilities.d.ts +10 -0
  62. package/lib/utilities.js +94 -0
  63. package/lib/utilities.js.flow +84 -0
  64. package/lib/utilities.mjs +79 -0
  65. package/package.json +79 -25
  66. package/result.d.ts +16 -0
  67. package/result.js +34 -0
  68. package/result.js.flow +26 -0
  69. package/result.mjs +27 -0
  70. package/cjs/array.js +0 -133
  71. package/cjs/array.js.flow +0 -106
  72. package/cjs/boolean.js +0 -42
  73. package/cjs/boolean.js.flow +0 -28
  74. package/cjs/constants.js +0 -67
  75. package/cjs/constants.js.flow +0 -45
  76. package/cjs/date.js +0 -42
  77. package/cjs/date.js.flow +0 -38
  78. package/cjs/describe.js +0 -22
  79. package/cjs/describe.js.flow +0 -17
  80. package/cjs/dispatch.js +0 -58
  81. package/cjs/dispatch.js.flow +0 -57
  82. package/cjs/either.js +0 -85
  83. package/cjs/either.js.flow +0 -131
  84. package/cjs/fail.js +0 -19
  85. package/cjs/fail.js.flow +0 -13
  86. package/cjs/guard.js +0 -30
  87. package/cjs/guard.js.flow +0 -36
  88. package/cjs/index.js +0 -397
  89. package/cjs/index.js.flow +0 -56
  90. package/cjs/instanceOf.js +0 -17
  91. package/cjs/instanceOf.js.flow +0 -21
  92. package/cjs/json.js +0 -33
  93. package/cjs/json.js.flow +0 -28
  94. package/cjs/lazy.js +0 -18
  95. package/cjs/lazy.js.flow +0 -15
  96. package/cjs/mapping.js +0 -113
  97. package/cjs/mapping.js.flow +0 -71
  98. package/cjs/number.js +0 -38
  99. package/cjs/number.js.flow +0 -35
  100. package/cjs/object.js +0 -254
  101. package/cjs/object.js.flow +0 -211
  102. package/cjs/optional.js +0 -52
  103. package/cjs/optional.js.flow +0 -42
  104. package/cjs/string.js +0 -93
  105. package/cjs/string.js.flow +0 -81
  106. package/cjs/tuple.js +0 -199
  107. package/cjs/tuple.js.flow +0 -221
  108. package/cjs/types.js +0 -5
  109. package/cjs/types.js.flow +0 -26
  110. package/cjs/utils.js +0 -70
  111. package/cjs/utils.js.flow +0 -58
  112. package/es/index.js +0 -1212
  113. package/ts/array.d.ts +0 -5
  114. package/ts/constants.d.ts +0 -11
  115. package/ts/describe.d.ts +0 -3
  116. package/ts/dispatch.d.ts +0 -8
  117. package/ts/either.d.ts +0 -61
  118. package/ts/fail.d.ts +0 -3
  119. package/ts/guard.d.ts +0 -7
  120. package/ts/index.d.ts +0 -38
  121. package/ts/instanceOf.d.ts +0 -3
  122. package/ts/lazy.d.ts +0 -3
  123. package/ts/mapping.d.ts +0 -4
  124. package/ts/optional.d.ts +0 -5
  125. package/ts/string.d.ts +0 -7
  126. package/ts/tuple.d.ts +0 -30
  127. package/ts/types.d.ts +0 -18
  128. package/ts/utils.d.ts +0 -13
package/Decoder.mjs ADDED
@@ -0,0 +1,216 @@
1
+ import { annotate } from './annotate.mjs';
2
+ import { formatInline } from './format.mjs';
3
+ import { err as makeErr, ok as makeOk } from './result.mjs';
4
+
5
+ function noThrow(fn) {
6
+ return function (t) {
7
+ try {
8
+ var v = fn(t);
9
+ return makeOk(v);
10
+ } catch (e) {
11
+ return makeErr(annotate(t, e instanceof Error ? e.message : String(e)));
12
+ }
13
+ };
14
+ }
15
+ /**
16
+ * Defines a new `Decoder<T>`, by implementing a custom acceptance function.
17
+ * The function receives three arguments:
18
+ *
19
+ * 1. `blob` - the raw/unknown input (aka your external data)
20
+ * 2. `ok` - Call `ok(value)` to accept the input and return ``value``
21
+ * 3. `err` - Call `err(message)` to reject the input and use "message" in the
22
+ * annotation
23
+ *
24
+ * The expected return value should be a `DecodeResult<T>`, which can be
25
+ * obtained by returning the result from the provided `ok` or `err` helper
26
+ * functions.
27
+ */
28
+
29
+
30
+ export function define(decodeFn) {
31
+ /**
32
+ * Validates the raw/untrusted/unknown input and either accepts or rejects
33
+ * it.
34
+ *
35
+ * Contrasted with `.verify()`, calls to `.decode()` will never fail and
36
+ * instead return a result type.
37
+ */
38
+ function decode(blob) {
39
+ return decodeFn(blob, makeOk, function (msg) {
40
+ return makeErr(typeof msg === 'string' ? annotate(blob, msg) : msg);
41
+ });
42
+ }
43
+ /**
44
+ * Verified the (raw/untrusted/unknown) input and either accepts or rejects
45
+ * it. When accepted, returns the decoded `T` value directly. Otherwise
46
+ * fail with a runtime error.
47
+ */
48
+
49
+
50
+ function verify(blob, formatter) {
51
+ if (formatter === void 0) {
52
+ formatter = formatInline;
53
+ }
54
+
55
+ var result = decode(blob);
56
+
57
+ if (result.ok) {
58
+ return result.value;
59
+ } else {
60
+ // Formatters may return a string or an error for convenience of
61
+ // writing them. If it already returns an Error, throw it
62
+ // unmodified. If it returns a string, wrap it in a "Decoding
63
+ // error" instance from it and throw that.
64
+ var strOrErr = formatter(result.error);
65
+
66
+ if (typeof strOrErr === 'string') {
67
+ var _err = new Error('\n' + strOrErr);
68
+
69
+ _err.name = 'Decoding error';
70
+ throw _err;
71
+ } else {
72
+ throw strOrErr;
73
+ }
74
+ }
75
+ }
76
+ /**
77
+ * Verified the (raw/untrusted/unknown) input and either accepts or rejects
78
+ * it. When accepted, returns the decoded `T` value directly. Otherwise
79
+ * returns `undefined`.
80
+ *
81
+ * Use this when you're not interested in programmatically handling the
82
+ * error message.
83
+ */
84
+
85
+
86
+ function value(blob) {
87
+ return decode(blob).value;
88
+ }
89
+ /**
90
+ * Accepts any value the given decoder accepts, and on success, will call
91
+ * the given function **on the decoded result**. If the transformation
92
+ * function throws an error, the whole decoder will fail using the error
93
+ * message as the failure reason.
94
+ */
95
+
96
+
97
+ function transform(transformFn) {
98
+ return then(noThrow(transformFn));
99
+ }
100
+ /**
101
+ * Adds an extra predicate to a decoder. The new decoder is like the
102
+ * original decoder, but only accepts values that also meet the
103
+ * predicate.
104
+ */
105
+
106
+
107
+ function refine(predicateFn, errmsg) {
108
+ return reject(function (value) {
109
+ return predicateFn(value) ? // Don't reject
110
+ null : // Reject with the given error message
111
+ errmsg;
112
+ });
113
+ }
114
+ /**
115
+ * Chain together the current decoder with another.
116
+ *
117
+ * First, the current decoder must accept the input. If so, it will pass
118
+ * the successfully decoded result to the given ``next`` function to
119
+ * further decide whether or not the value should get accepted or rejected.
120
+ *
121
+ * The argument to `.then()` is a decoding function, just like one you
122
+ * would pass to `define()`. The key difference with `define()` is that
123
+ * `define()` must always assume an ``unknown`` input, whereas with
124
+ * a `.then()` call the provided ``next`` function will receive a ``T`` as
125
+ * its input. This will allow the function to make a stronger assumption
126
+ * about its input.
127
+ *
128
+ * If it helps, you can think of `define(nextFn)` as equivalent to
129
+ * `unknown.then(nextFn)`.
130
+ *
131
+ * This is an advanced, low-level, decoder. It's not recommended to reach
132
+ * for this low-level construct when implementing custom decoders. Most
133
+ * cases can be covered by `.transform()` or `.refine()`.
134
+ */
135
+
136
+
137
+ function then(next) {
138
+ return define(function (blob, ok, err) {
139
+ var result = decode(blob);
140
+ return result.ok ? next(result.value, ok, err) : result;
141
+ });
142
+ }
143
+ /**
144
+ * Adds an extra predicate to a decoder. The new decoder is like the
145
+ * original decoder, but only accepts values that aren't rejected by the
146
+ * given function.
147
+ *
148
+ * The given function can return `null` to accept the decoded value, or
149
+ * return a specific error message to reject.
150
+ *
151
+ * Unlike `.refine()`, you can use this function to return a dynamic error
152
+ * message.
153
+ */
154
+
155
+
156
+ function reject(rejectFn) {
157
+ return then(function (value, ok, err) {
158
+ var errmsg = rejectFn(value);
159
+ return errmsg === null ? ok(value) : err(typeof errmsg === 'string' ? annotate(value, errmsg) : errmsg);
160
+ });
161
+ }
162
+ /**
163
+ * Uses the given decoder, but will use an alternative error message in
164
+ * case it rejects. This can be used to simplify or shorten otherwise
165
+ * long or low-level/technical errors.
166
+ */
167
+
168
+
169
+ function describe(message) {
170
+ return define(function (blob, _, err) {
171
+ // Decode using the given decoder...
172
+ var result = decode(blob);
173
+
174
+ if (result.ok) {
175
+ return result;
176
+ } else {
177
+ // ...but in case of error, annotate this with the custom given
178
+ // message instead
179
+ return err(annotate(result.error, message));
180
+ }
181
+ });
182
+ }
183
+ /**
184
+ * WARNING: This is an EXPERIMENTAL API that will likely change in the
185
+ * future. Please DO NOT rely on it.
186
+ *
187
+ * Chain together the current decoder with another, but also pass along
188
+ * the original input.
189
+ *
190
+ * This is like `.then()`, but instead of this function receiving just
191
+ * the decoded result ``T``, it also receives the original input.
192
+ *
193
+ * This is an advanced, low-level, decoder.
194
+ */
195
+
196
+
197
+ function peek_UNSTABLE(next) {
198
+ return define(function (blob, ok, err) {
199
+ var result = decode(blob);
200
+ return result.ok ? next([blob, result.value], ok, err) : result;
201
+ });
202
+ }
203
+
204
+ return Object.freeze({
205
+ verify: verify,
206
+ value: value,
207
+ decode: decode,
208
+ transform: transform,
209
+ refine: refine,
210
+ reject: reject,
211
+ describe: describe,
212
+ then: then,
213
+ // EXPERIMENTAL - please DO NOT rely on this method
214
+ peek_UNSTABLE: peek_UNSTABLE
215
+ });
216
+ }
@@ -0,0 +1 @@
1
+ "Package `decoders` requires TypeScript >= 4.1.0"