decoders 2.2.0-test → 2.2.0-test3
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.
- package/dist/index.cjs +193 -29
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +63 -5
- package/dist/index.d.ts +63 -5
- package/dist/index.js +177 -13
- package/dist/index.js.map +1 -1
- package/package.json +16 -100
- package/dist/annotate-0PUmWHxH.d.cts +0 -33
- package/dist/annotate-0PUmWHxH.d.ts +0 -33
- package/dist/chunk-2C72BP5L.cjs +0 -179
- package/dist/chunk-2C72BP5L.cjs.map +0 -1
- package/dist/chunk-BPSZE2VX.js +0 -13
- package/dist/chunk-BPSZE2VX.js.map +0 -1
- package/dist/chunk-Q3YXBCTD.cjs +0 -13
- package/dist/chunk-Q3YXBCTD.cjs.map +0 -1
- package/dist/chunk-RUMDX66L.js +0 -179
- package/dist/chunk-RUMDX66L.js.map +0 -1
- package/dist/format.cjs +0 -13
- package/dist/format.cjs.map +0 -1
- package/dist/format.d.cts +0 -9
- package/dist/format.d.ts +0 -9
- package/dist/format.js +0 -13
- package/dist/format.js.map +0 -1
- package/dist/result.cjs +0 -9
- package/dist/result.cjs.map +0 -1
- package/dist/result.d.cts +0 -26
- package/dist/result.d.ts +0 -26
- package/dist/result.js +0 -9
- package/dist/result.js.map +0 -1
- package/format.cjs +0 -13
- package/result.cjs +0 -9
package/dist/index.cjs
CHANGED
|
@@ -1,16 +1,68 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports, "__esModule", {value: true}); function _nullishCoalesce(lhs, rhsFn) { if (lhs != null) { return lhs; } else { return rhsFn(); } }
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
1
|
+
"use strict";Object.defineProperty(exports, "__esModule", {value: true}); function _nullishCoalesce(lhs, rhsFn) { if (lhs != null) { return lhs; } else { return rhsFn(); } }// src/_utils.ts
|
|
2
|
+
var INDENT = " ";
|
|
3
|
+
function lazyval(value) {
|
|
4
|
+
return typeof value === "function" ? value() : value;
|
|
5
|
+
}
|
|
6
|
+
function subtract(xs, ys) {
|
|
7
|
+
const result = /* @__PURE__ */ new Set();
|
|
8
|
+
for (const x of xs) {
|
|
9
|
+
if (!ys.has(x)) {
|
|
10
|
+
result.add(x);
|
|
11
|
+
}
|
|
12
|
+
}
|
|
13
|
+
return result;
|
|
14
|
+
}
|
|
15
|
+
function asDate(value) {
|
|
16
|
+
return !!value && Object.prototype.toString.call(value) === "[object Date]" && !isNaN(value) ? value : null;
|
|
17
|
+
}
|
|
18
|
+
function isPojo(value) {
|
|
19
|
+
return value !== null && value !== void 0 && typeof value === "object" && // This still seems to be the only reliable way to determine whether
|
|
20
|
+
// something is a pojo... ¯\_(ツ)_/¯
|
|
21
|
+
Object.prototype.toString.call(value) === "[object Object]";
|
|
22
|
+
}
|
|
23
|
+
function isMultiline(s) {
|
|
24
|
+
return s.indexOf("\n") >= 0;
|
|
25
|
+
}
|
|
26
|
+
function indent(s, prefix = INDENT) {
|
|
27
|
+
if (isMultiline(s)) {
|
|
28
|
+
return s.split("\n").map((line) => `${prefix}${line}`).join("\n");
|
|
29
|
+
} else {
|
|
30
|
+
return `${prefix}${s}`;
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
function summarize(ann, keypath = []) {
|
|
34
|
+
const result = [];
|
|
35
|
+
if (ann.type === "array") {
|
|
36
|
+
const items = ann.items;
|
|
37
|
+
let index = 0;
|
|
38
|
+
for (const ann2 of items) {
|
|
39
|
+
for (const item of summarize(ann2, [...keypath, index++])) {
|
|
40
|
+
result.push(item);
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
} else if (ann.type === "object") {
|
|
44
|
+
const fields = ann.fields;
|
|
45
|
+
for (const key of Object.keys(fields)) {
|
|
46
|
+
const value = fields[key];
|
|
47
|
+
for (const item of summarize(value, [...keypath, key])) {
|
|
48
|
+
result.push(item);
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
const text = ann.text;
|
|
53
|
+
if (!text) {
|
|
54
|
+
return result;
|
|
55
|
+
}
|
|
56
|
+
let prefix;
|
|
57
|
+
if (keypath.length === 0) {
|
|
58
|
+
prefix = "";
|
|
59
|
+
} else if (keypath.length === 1) {
|
|
60
|
+
prefix = typeof keypath[0] === "number" ? `Value at index ${keypath[0]}: ` : `Value at key ${JSON.stringify(keypath[0])}: `;
|
|
61
|
+
} else {
|
|
62
|
+
prefix = `Value at keypath ${keypath.map(String).join(".")}: `;
|
|
63
|
+
}
|
|
64
|
+
return [...result, `${prefix}${text}`];
|
|
65
|
+
}
|
|
14
66
|
|
|
15
67
|
// src/annotate.ts
|
|
16
68
|
var _register = /* @__PURE__ */ new WeakSet();
|
|
@@ -97,7 +149,7 @@ function annotate(value, text, seen) {
|
|
|
97
149
|
return annotateArray(value, text, seen);
|
|
98
150
|
}
|
|
99
151
|
}
|
|
100
|
-
if (
|
|
152
|
+
if (isPojo(value)) {
|
|
101
153
|
if (seen.has(value)) {
|
|
102
154
|
return circularRef(text);
|
|
103
155
|
} else {
|
|
@@ -116,14 +168,122 @@ function public_annotateObject(obj, text) {
|
|
|
116
168
|
return annotateObject(obj, text, /* @__PURE__ */ new WeakSet());
|
|
117
169
|
}
|
|
118
170
|
|
|
171
|
+
// src/format.ts
|
|
172
|
+
function serializeString(s, width = 80) {
|
|
173
|
+
let ser = JSON.stringify(s);
|
|
174
|
+
if (ser.length <= width) {
|
|
175
|
+
return ser;
|
|
176
|
+
}
|
|
177
|
+
const truncated = `${s.substring(0, width - 15)}...`;
|
|
178
|
+
ser = `${JSON.stringify(truncated)} [truncated]`;
|
|
179
|
+
return ser;
|
|
180
|
+
}
|
|
181
|
+
function serializeArray(annotation, prefix) {
|
|
182
|
+
const { items } = annotation;
|
|
183
|
+
if (items.length === 0) {
|
|
184
|
+
return "[]";
|
|
185
|
+
}
|
|
186
|
+
const result = [];
|
|
187
|
+
for (const item of items) {
|
|
188
|
+
const [ser, ann] = serializeAnnotation(item, `${prefix}${INDENT}`);
|
|
189
|
+
result.push(`${prefix}${INDENT}${ser}${","}`);
|
|
190
|
+
if (ann !== void 0) {
|
|
191
|
+
result.push(indent(ann, `${prefix}${INDENT}`));
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
return ["[", ...result, `${prefix}]`].join("\n");
|
|
195
|
+
}
|
|
196
|
+
function serializeObject(annotation, prefix) {
|
|
197
|
+
const { fields } = annotation;
|
|
198
|
+
const fieldNames = Object.keys(fields);
|
|
199
|
+
if (fieldNames.length === 0) {
|
|
200
|
+
return "{}";
|
|
201
|
+
}
|
|
202
|
+
const result = [];
|
|
203
|
+
for (const key of fieldNames) {
|
|
204
|
+
const valueAnnotation = fields[key];
|
|
205
|
+
const kser = serializeValue(key);
|
|
206
|
+
const valPrefix = `${prefix}${INDENT}${" ".repeat(kser.length + 2)}`;
|
|
207
|
+
const [vser, vann] = serializeAnnotation(valueAnnotation, `${prefix}${INDENT}`);
|
|
208
|
+
result.push(`${prefix}${INDENT}${kser}: ${vser},`);
|
|
209
|
+
if (vann !== void 0) {
|
|
210
|
+
result.push(indent(vann, valPrefix));
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
return ["{", ...result, `${prefix}}`].join("\n");
|
|
214
|
+
}
|
|
215
|
+
function serializeValue(value) {
|
|
216
|
+
if (typeof value === "string") {
|
|
217
|
+
return serializeString(value);
|
|
218
|
+
} else if (typeof value === "number" || typeof value === "boolean") {
|
|
219
|
+
return value.toString();
|
|
220
|
+
} else if (value === null) {
|
|
221
|
+
return "null";
|
|
222
|
+
} else if (value === void 0) {
|
|
223
|
+
return "undefined";
|
|
224
|
+
} else {
|
|
225
|
+
const valueAsDate = asDate(value);
|
|
226
|
+
if (valueAsDate !== null) {
|
|
227
|
+
return `new Date(${JSON.stringify(valueAsDate.toISOString())})`;
|
|
228
|
+
} else if (value instanceof Date) {
|
|
229
|
+
return "(Invalid Date)";
|
|
230
|
+
} else {
|
|
231
|
+
return "(unserializable)";
|
|
232
|
+
}
|
|
233
|
+
}
|
|
234
|
+
}
|
|
235
|
+
function serializeAnnotation(ann, prefix = "") {
|
|
236
|
+
let serialized;
|
|
237
|
+
if (ann.type === "array") {
|
|
238
|
+
serialized = serializeArray(ann, prefix);
|
|
239
|
+
} else if (ann.type === "object") {
|
|
240
|
+
serialized = serializeObject(ann, prefix);
|
|
241
|
+
} else if (ann.type === "function") {
|
|
242
|
+
serialized = "<function>";
|
|
243
|
+
} else if (ann.type === "circular-ref") {
|
|
244
|
+
serialized = "<circular ref>";
|
|
245
|
+
} else if (ann.type === "unknown") {
|
|
246
|
+
serialized = "???";
|
|
247
|
+
} else {
|
|
248
|
+
serialized = serializeValue(ann.value);
|
|
249
|
+
}
|
|
250
|
+
const text = ann.text;
|
|
251
|
+
if (text !== void 0) {
|
|
252
|
+
const sep = "^".repeat(isMultiline(serialized) ? 1 : serialized.length);
|
|
253
|
+
return [serialized, [sep, text].join(isMultiline(text) ? "\n" : " ")];
|
|
254
|
+
} else {
|
|
255
|
+
return [serialized, void 0];
|
|
256
|
+
}
|
|
257
|
+
}
|
|
258
|
+
function formatInline(ann) {
|
|
259
|
+
const [serialized, annotation] = serializeAnnotation(ann);
|
|
260
|
+
if (annotation !== void 0) {
|
|
261
|
+
return `${serialized}
|
|
262
|
+
${annotation}`;
|
|
263
|
+
} else {
|
|
264
|
+
return serialized;
|
|
265
|
+
}
|
|
266
|
+
}
|
|
267
|
+
function formatShort(ann) {
|
|
268
|
+
return summarize(ann, []).join("\n");
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
// src/result.ts
|
|
272
|
+
function ok(value) {
|
|
273
|
+
return { ok: true, value, error: void 0 };
|
|
274
|
+
}
|
|
275
|
+
function err(error) {
|
|
276
|
+
return { ok: false, value: void 0, error };
|
|
277
|
+
}
|
|
278
|
+
|
|
119
279
|
// src/Decoder.ts
|
|
120
280
|
function noThrow(fn) {
|
|
121
281
|
return (t) => {
|
|
122
282
|
try {
|
|
123
283
|
const v = fn(t);
|
|
124
|
-
return
|
|
284
|
+
return ok(v);
|
|
125
285
|
} catch (e) {
|
|
126
|
-
return
|
|
286
|
+
return err(public_annotate(t, e instanceof Error ? e.message : String(e)));
|
|
127
287
|
}
|
|
128
288
|
};
|
|
129
289
|
}
|
|
@@ -142,11 +302,11 @@ function define(fn) {
|
|
|
142
302
|
function decode(blob) {
|
|
143
303
|
return fn(
|
|
144
304
|
blob,
|
|
145
|
-
|
|
146
|
-
(msg) =>
|
|
305
|
+
ok,
|
|
306
|
+
(msg) => err(typeof msg === "string" ? public_annotate(blob, msg) : msg)
|
|
147
307
|
);
|
|
148
308
|
}
|
|
149
|
-
function verify(blob, formatter =
|
|
309
|
+
function verify(blob, formatter = formatInline) {
|
|
150
310
|
const result = decode(blob);
|
|
151
311
|
if (result.ok) {
|
|
152
312
|
return result.value;
|
|
@@ -215,13 +375,13 @@ function define(fn) {
|
|
|
215
375
|
|
|
216
376
|
// src/lib/objects.ts
|
|
217
377
|
var pojo = define(
|
|
218
|
-
(blob, ok2, err2) =>
|
|
378
|
+
(blob, ok2, err2) => isPojo(blob) ? ok2(blob) : err2("Must be an object")
|
|
219
379
|
);
|
|
220
380
|
function object2(decodersByKey) {
|
|
221
381
|
const knownKeys = new Set(Object.keys(decodersByKey));
|
|
222
382
|
return pojo.then((plainObj, ok2, err2) => {
|
|
223
383
|
const actualKeys = new Set(Object.keys(plainObj));
|
|
224
|
-
const missingKeys =
|
|
384
|
+
const missingKeys = subtract(knownKeys, actualKeys);
|
|
225
385
|
const record = {};
|
|
226
386
|
let errors = null;
|
|
227
387
|
for (const key of Object.keys(decodersByKey)) {
|
|
@@ -265,7 +425,7 @@ function exact(decodersByKey) {
|
|
|
265
425
|
const allowedKeys = new Set(Object.keys(decodersByKey));
|
|
266
426
|
const checked = pojo.reject((plainObj) => {
|
|
267
427
|
const actualKeys = new Set(Object.keys(plainObj));
|
|
268
|
-
const extraKeys =
|
|
428
|
+
const extraKeys = subtract(actualKeys, allowedKeys);
|
|
269
429
|
return extraKeys.size > 0 ? `Unexpected extra keys: ${Array.from(extraKeys).join(", ")}` : (
|
|
270
430
|
// Don't reject
|
|
271
431
|
null
|
|
@@ -367,7 +527,7 @@ var fail = never;
|
|
|
367
527
|
// src/lib/unions.ts
|
|
368
528
|
var EITHER_PREFIX = "Either:\n";
|
|
369
529
|
function itemize(s) {
|
|
370
|
-
return `-${
|
|
530
|
+
return `-${indent(s).substring(1)}`;
|
|
371
531
|
}
|
|
372
532
|
function nest(errText) {
|
|
373
533
|
return errText.startsWith(EITHER_PREFIX) ? errText.substr(EITHER_PREFIX.length) : itemize(errText);
|
|
@@ -386,7 +546,7 @@ function either(...decoders) {
|
|
|
386
546
|
errors.push(result.error);
|
|
387
547
|
}
|
|
388
548
|
}
|
|
389
|
-
const text = EITHER_PREFIX + errors.map((err3) => nest(
|
|
549
|
+
const text = EITHER_PREFIX + errors.map((err3) => nest(summarize(err3).join("\n"))).join("\n");
|
|
390
550
|
return err2(text);
|
|
391
551
|
});
|
|
392
552
|
}
|
|
@@ -426,15 +586,15 @@ var undefined_or_null = define(
|
|
|
426
586
|
);
|
|
427
587
|
function optional(decoder, defaultValue) {
|
|
428
588
|
const rv = either(undefined_, decoder);
|
|
429
|
-
return arguments.length >= 2 ? rv.transform((value) => _nullishCoalesce(value, () => (
|
|
589
|
+
return arguments.length >= 2 ? rv.transform((value) => _nullishCoalesce(value, () => ( lazyval(defaultValue)))) : rv;
|
|
430
590
|
}
|
|
431
591
|
function nullable(decoder, defaultValue) {
|
|
432
592
|
const rv = either(null_, decoder);
|
|
433
|
-
return arguments.length >= 2 ? rv.transform((value) => _nullishCoalesce(value, () => (
|
|
593
|
+
return arguments.length >= 2 ? rv.transform((value) => _nullishCoalesce(value, () => ( lazyval(defaultValue)))) : rv;
|
|
434
594
|
}
|
|
435
595
|
function maybe(decoder, defaultValue) {
|
|
436
596
|
const rv = either(undefined_or_null, decoder);
|
|
437
|
-
return arguments.length >= 2 ? rv.transform((value) => _nullishCoalesce(value, () => (
|
|
597
|
+
return arguments.length >= 2 ? rv.transform((value) => _nullishCoalesce(value, () => ( lazyval(defaultValue)))) : rv;
|
|
438
598
|
}
|
|
439
599
|
function constant(value) {
|
|
440
600
|
return define(
|
|
@@ -574,7 +734,7 @@ var uuidv4 = (
|
|
|
574
734
|
// src/lib/dates.ts
|
|
575
735
|
var iso8601_re = /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(?:[.]\d+)?(?:Z|[+-]\d{2}:?\d{2})$/;
|
|
576
736
|
var date = define((blob, ok2, err2) => {
|
|
577
|
-
const date2 =
|
|
737
|
+
const date2 = asDate(blob);
|
|
578
738
|
return date2 !== null ? ok2(date2) : err2("Must be a Date");
|
|
579
739
|
});
|
|
580
740
|
var iso8601 = (
|
|
@@ -655,5 +815,9 @@ var json = either(
|
|
|
655
815
|
|
|
656
816
|
|
|
657
817
|
|
|
658
|
-
|
|
818
|
+
|
|
819
|
+
|
|
820
|
+
|
|
821
|
+
|
|
822
|
+
exports.always = always; exports.anyNumber = anyNumber; exports.array = array2; exports.boolean = boolean; exports.constant = constant; exports.date = date; exports.define = define; exports.dict = dict; exports.either = either; exports.email = email; exports.err = err; exports.exact = exact; exports.fail = fail; exports.formatInline = formatInline; exports.formatShort = formatShort; exports.hardcoded = hardcoded; exports.httpsUrl = httpsUrl; exports.inexact = inexact; exports.instanceOf = instanceOf; exports.integer = integer; exports.iso8601 = iso8601; exports.json = json; exports.jsonArray = jsonArray; exports.jsonObject = jsonObject; exports.lazy = lazy; exports.mapping = mapping; exports.maybe = maybe; exports.mixed = mixed; exports.never = never; exports.nonEmptyArray = nonEmptyArray; exports.nonEmptyString = nonEmptyString; exports.null_ = null_; exports.nullable = nullable; exports.number = number; exports.numericBoolean = numericBoolean; exports.object = object2; exports.ok = ok; exports.oneOf = oneOf; exports.optional = optional; exports.poja = poja; exports.pojo = pojo; exports.positiveInteger = positiveInteger; exports.positiveNumber = positiveNumber; exports.prep = prep; exports.regex = regex; exports.set = set; exports.string = string; exports.taggedUnion = taggedUnion; exports.truthy = truthy; exports.tuple = tuple; exports.undefined_ = undefined_; exports.unknown = unknown2; exports.url = url; exports.uuid = uuid; exports.uuidv1 = uuidv1; exports.uuidv4 = uuidv4;
|
|
659
823
|
//# sourceMappingURL=index.cjs.map
|