decoders 1.25.5 → 2.0.0-beta10

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 (124) hide show
  1. package/CHANGELOG.md +53 -3
  2. package/Decoder.d.ts +27 -0
  3. package/Decoder.js +199 -0
  4. package/Decoder.js.flow +224 -0
  5. package/Decoder.mjs +192 -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 +38 -36
  21. package/index.js +69 -376
  22. package/index.js.flow +39 -51
  23. package/index.mjs +11 -0
  24. package/{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/{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/{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/{json.d.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/{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/{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 +78 -23
  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/array.d.ts +0 -5
  71. package/array.js +0 -133
  72. package/array.js.flow +0 -106
  73. package/boolean.js +0 -42
  74. package/boolean.js.flow +0 -28
  75. package/constants.d.ts +0 -11
  76. package/constants.js +0 -67
  77. package/constants.js.flow +0 -45
  78. package/date.js +0 -42
  79. package/date.js.flow +0 -38
  80. package/describe.d.ts +0 -3
  81. package/describe.js +0 -22
  82. package/describe.js.flow +0 -17
  83. package/dispatch.d.ts +0 -8
  84. package/dispatch.js +0 -58
  85. package/dispatch.js.flow +0 -57
  86. package/either.d.ts +0 -61
  87. package/either.js +0 -85
  88. package/either.js.flow +0 -131
  89. package/fail.d.ts +0 -3
  90. package/fail.js +0 -19
  91. package/fail.js.flow +0 -13
  92. package/guard.d.ts +0 -7
  93. package/guard.js +0 -30
  94. package/guard.js.flow +0 -36
  95. package/instanceOf.d.ts +0 -3
  96. package/instanceOf.js +0 -17
  97. package/instanceOf.js.flow +0 -21
  98. package/json.js +0 -33
  99. package/json.js.flow +0 -28
  100. package/lazy.d.ts +0 -3
  101. package/lazy.js +0 -18
  102. package/lazy.js.flow +0 -15
  103. package/mapping.d.ts +0 -4
  104. package/mapping.js +0 -113
  105. package/mapping.js.flow +0 -71
  106. package/number.js +0 -38
  107. package/number.js.flow +0 -35
  108. package/object.js +0 -254
  109. package/object.js.flow +0 -211
  110. package/optional.d.ts +0 -5
  111. package/optional.js +0 -52
  112. package/optional.js.flow +0 -42
  113. package/string.d.ts +0 -7
  114. package/string.js +0 -93
  115. package/string.js.flow +0 -81
  116. package/tuple.d.ts +0 -30
  117. package/tuple.js +0 -199
  118. package/tuple.js.flow +0 -221
  119. package/types.d.ts +0 -18
  120. package/types.js +0 -5
  121. package/types.js.flow +0 -26
  122. package/utils.d.ts +0 -13
  123. package/utils.js +0 -70
  124. package/utils.js.flow +0 -58
package/Decoder.mjs ADDED
@@ -0,0 +1,192 @@
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
+ * Accepts any value the given decoder accepts, and on success, will call
68
+ * the given function **on the decoded result**. If the transformation
69
+ * function throws an error, the whole decoder will fail using the error
70
+ * message as the failure reason.
71
+ */
72
+
73
+
74
+ function transform(transformFn) {
75
+ return then(noThrow(transformFn));
76
+ }
77
+ /**
78
+ * Adds an extra predicate to a decoder. The new decoder is like the
79
+ * original decoder, but only accepts values that also meet the
80
+ * predicate.
81
+ */
82
+
83
+
84
+ function refine(predicateFn, errmsg) {
85
+ return reject(function (value) {
86
+ return predicateFn(value) ? // Don't reject
87
+ null : // Reject with the given error message
88
+ errmsg;
89
+ });
90
+ }
91
+ /**
92
+ * Chain together the current decoder with another.
93
+ *
94
+ * First, the current decoder must accept the input. If so, it will pass
95
+ * the successfully decoded result to the given ``next`` function to
96
+ * further decide whether or not the value should get accepted or rejected.
97
+ *
98
+ * The argument to `.then()` is a decoding function, just like one you
99
+ * would pass to `define()`. The key difference with `define()` is that
100
+ * `define()` must always assume an ``unknown`` input, whereas with
101
+ * a `.then()` call the provided ``next`` function will receive a ``T`` as
102
+ * its input. This will allow the function to make a stronger assumption
103
+ * about its input.
104
+ *
105
+ * If it helps, you can think of `define(nextFn)` as equivalent to
106
+ * `unknown.then(nextFn)`.
107
+ *
108
+ * This is an advanced, low-level, decoder. It's not recommended to reach
109
+ * for this low-level construct when implementing custom decoders. Most
110
+ * cases can be covered by `.transform()` or `.refine()`.
111
+ */
112
+
113
+
114
+ function then(next) {
115
+ return define(function (blob, ok, err) {
116
+ var result = decode(blob);
117
+ return result.ok ? next(result.value, ok, err) : result;
118
+ });
119
+ }
120
+ /**
121
+ * Adds an extra predicate to a decoder. The new decoder is like the
122
+ * original decoder, but only accepts values that aren't rejected by the
123
+ * given function.
124
+ *
125
+ * The given function can return `null` to accept the decoded value, or
126
+ * return a specific error message to reject.
127
+ *
128
+ * Unlike `.refine()`, you can use this function to return a dynamic error
129
+ * message.
130
+ */
131
+
132
+
133
+ function reject(rejectFn) {
134
+ return then(function (value, ok, err) {
135
+ var errmsg = rejectFn(value);
136
+ return errmsg === null ? ok(value) : err(typeof errmsg === 'string' ? annotate(value, errmsg) : errmsg);
137
+ });
138
+ }
139
+ /**
140
+ * Uses the given decoder, but will use an alternative error message in
141
+ * case it rejects. This can be used to simplify or shorten otherwise
142
+ * long or low-level/technical errors.
143
+ */
144
+
145
+
146
+ function describe(message) {
147
+ return define(function (blob, _, err) {
148
+ // Decode using the given decoder...
149
+ var result = decode(blob);
150
+
151
+ if (result.ok) {
152
+ return result;
153
+ } else {
154
+ // ...but in case of error, annotate this with the custom given
155
+ // message instead
156
+ return err(annotate(result.error, message));
157
+ }
158
+ });
159
+ }
160
+ /**
161
+ * WARNING: This is an EXPERIMENTAL API that will likely change in the
162
+ * future. Please DO NOT rely on it.
163
+ *
164
+ * Chain together the current decoder with another, but also pass along
165
+ * the original input.
166
+ *
167
+ * This is like `.then()`, but instead of this function receiving just
168
+ * the decoded result ``T``, it also receives the original input.
169
+ *
170
+ * This is an advanced, low-level, decoder.
171
+ */
172
+
173
+
174
+ function peek_UNSTABLE(next) {
175
+ return define(function (blob, ok, err) {
176
+ var result = decode(blob);
177
+ return result.ok ? next([blob, result.value], ok, err) : result;
178
+ });
179
+ }
180
+
181
+ return Object.freeze({
182
+ verify: verify,
183
+ decode: decode,
184
+ transform: transform,
185
+ refine: refine,
186
+ reject: reject,
187
+ describe: describe,
188
+ then: then,
189
+ // EXPERIMENTAL - please DO NOT rely on this method
190
+ peek_UNSTABLE: peek_UNSTABLE
191
+ });
192
+ }
@@ -0,0 +1 @@
1
+ "Package `decoders` requires TypeScript >= 4.1.0"