decoders 1.26.0-beta2 → 2.0.0-beta11

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 +52 -6
  2. package/Decoder.d.ts +28 -0
  3. package/Decoder.js +213 -0
  4. package/Decoder.js.flow +238 -0
  5. package/Decoder.mjs +206 -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 +14 -0
  30. package/lib/basics.js +109 -0
  31. package/lib/basics.js.flow +85 -0
  32. package/lib/basics.mjs +85 -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 -28
  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 -3
  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 -1039
  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,206 @@
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
+ var _err = new Error('\n' + formatter(result.error));
61
+
62
+ _err.name = 'Decoding error';
63
+ throw _err;
64
+ }
65
+ }
66
+ /**
67
+ * Verified the (raw/untrusted/unknown) input and either accepts or rejects
68
+ * it. When accepted, returns the decoded `T` value directly. Otherwise
69
+ * returns `undefined`.
70
+ *
71
+ * Use this when you're not interested in programmatically handling the
72
+ * error message.
73
+ */
74
+
75
+
76
+ function value(blob) {
77
+ return decode(blob).value;
78
+ }
79
+ /**
80
+ * Accepts any value the given decoder accepts, and on success, will call
81
+ * the given function **on the decoded result**. If the transformation
82
+ * function throws an error, the whole decoder will fail using the error
83
+ * message as the failure reason.
84
+ */
85
+
86
+
87
+ function transform(transformFn) {
88
+ return then(noThrow(transformFn));
89
+ }
90
+ /**
91
+ * Adds an extra predicate to a decoder. The new decoder is like the
92
+ * original decoder, but only accepts values that also meet the
93
+ * predicate.
94
+ */
95
+
96
+
97
+ function refine(predicateFn, errmsg) {
98
+ return reject(function (value) {
99
+ return predicateFn(value) ? // Don't reject
100
+ null : // Reject with the given error message
101
+ errmsg;
102
+ });
103
+ }
104
+ /**
105
+ * Chain together the current decoder with another.
106
+ *
107
+ * First, the current decoder must accept the input. If so, it will pass
108
+ * the successfully decoded result to the given ``next`` function to
109
+ * further decide whether or not the value should get accepted or rejected.
110
+ *
111
+ * The argument to `.then()` is a decoding function, just like one you
112
+ * would pass to `define()`. The key difference with `define()` is that
113
+ * `define()` must always assume an ``unknown`` input, whereas with
114
+ * a `.then()` call the provided ``next`` function will receive a ``T`` as
115
+ * its input. This will allow the function to make a stronger assumption
116
+ * about its input.
117
+ *
118
+ * If it helps, you can think of `define(nextFn)` as equivalent to
119
+ * `unknown.then(nextFn)`.
120
+ *
121
+ * This is an advanced, low-level, decoder. It's not recommended to reach
122
+ * for this low-level construct when implementing custom decoders. Most
123
+ * cases can be covered by `.transform()` or `.refine()`.
124
+ */
125
+
126
+
127
+ function then(next) {
128
+ return define(function (blob, ok, err) {
129
+ var result = decode(blob);
130
+ return result.ok ? next(result.value, ok, err) : result;
131
+ });
132
+ }
133
+ /**
134
+ * Adds an extra predicate to a decoder. The new decoder is like the
135
+ * original decoder, but only accepts values that aren't rejected by the
136
+ * given function.
137
+ *
138
+ * The given function can return `null` to accept the decoded value, or
139
+ * return a specific error message to reject.
140
+ *
141
+ * Unlike `.refine()`, you can use this function to return a dynamic error
142
+ * message.
143
+ */
144
+
145
+
146
+ function reject(rejectFn) {
147
+ return then(function (value, ok, err) {
148
+ var errmsg = rejectFn(value);
149
+ return errmsg === null ? ok(value) : err(typeof errmsg === 'string' ? annotate(value, errmsg) : errmsg);
150
+ });
151
+ }
152
+ /**
153
+ * Uses the given decoder, but will use an alternative error message in
154
+ * case it rejects. This can be used to simplify or shorten otherwise
155
+ * long or low-level/technical errors.
156
+ */
157
+
158
+
159
+ function describe(message) {
160
+ return define(function (blob, _, err) {
161
+ // Decode using the given decoder...
162
+ var result = decode(blob);
163
+
164
+ if (result.ok) {
165
+ return result;
166
+ } else {
167
+ // ...but in case of error, annotate this with the custom given
168
+ // message instead
169
+ return err(annotate(result.error, message));
170
+ }
171
+ });
172
+ }
173
+ /**
174
+ * WARNING: This is an EXPERIMENTAL API that will likely change in the
175
+ * future. Please DO NOT rely on it.
176
+ *
177
+ * Chain together the current decoder with another, but also pass along
178
+ * the original input.
179
+ *
180
+ * This is like `.then()`, but instead of this function receiving just
181
+ * the decoded result ``T``, it also receives the original input.
182
+ *
183
+ * This is an advanced, low-level, decoder.
184
+ */
185
+
186
+
187
+ function peek_UNSTABLE(next) {
188
+ return define(function (blob, ok, err) {
189
+ var result = decode(blob);
190
+ return result.ok ? next([blob, result.value], ok, err) : result;
191
+ });
192
+ }
193
+
194
+ return Object.freeze({
195
+ verify: verify,
196
+ value: value,
197
+ decode: decode,
198
+ transform: transform,
199
+ refine: refine,
200
+ reject: reject,
201
+ describe: describe,
202
+ then: then,
203
+ // EXPERIMENTAL - please DO NOT rely on this method
204
+ peek_UNSTABLE: peek_UNSTABLE
205
+ });
206
+ }
@@ -0,0 +1 @@
1
+ "Package `decoders` requires TypeScript >= 4.1.0"