decoders 2.3.0-test4 → 2.3.0

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 CHANGED
@@ -1,110 +1,45 @@
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;
1
+ "use strict";Object.defineProperty(exports, "__esModule", {value: true}); function _nullishCoalesce(lhs, rhsFn) { if (lhs != null) { return lhs; } else { return rhsFn(); } } function _optionalChain(ops) { let lastAccessLHS = undefined; let value = ops[0]; let i = 1; while (i < ops.length) { const op = ops[i]; const fn = ops[i + 1]; i += 2; if ((op === 'optionalAccess' || op === 'optionalCall') && value == null) { return undefined; } if (op === 'access' || op === 'optionalAccess') { lastAccessLHS = value; value = fn(value); } else if (op === 'call' || op === 'optionalCall') { value = fn((...args) => value.call(lastAccessLHS, ...args)); lastAccessLHS = undefined; } } return value; }// src/lib/utils.ts
2
+ function isNumber(value) {
3
+ return typeof value === "number";
5
4
  }
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;
5
+ function isString(value) {
6
+ return typeof value === "string";
7
+ }
8
+ function isBigInt(value) {
9
+ return typeof value === "bigint";
14
10
  }
15
- function asDate(value) {
16
- return !!value && Object.prototype.toString.call(value) === "[object Date]" && !isNaN(value) ? value : null;
11
+ function isDate(value) {
12
+ return !!value && Object.prototype.toString.call(value) === "[object Date]" && !isNaN(value);
17
13
  }
18
14
  function isPojo(value) {
19
15
  return value !== null && value !== void 0 && typeof value === "object" && // This still seems to be the only reliable way to determine whether
20
16
  // something is a pojo... ¯\_(ツ)_/¯
21
17
  Object.prototype.toString.call(value) === "[object Object]";
22
18
  }
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
- }
66
19
 
67
- // src/annotate.ts
20
+ // src/core/annotate.ts
68
21
  var _register = /* @__PURE__ */ new WeakSet();
69
22
  function brand(ann) {
70
23
  _register.add(ann);
71
24
  return ann;
72
25
  }
73
- function object(fields, text) {
26
+ function makeObjectAnn(fields, text) {
74
27
  return brand({ type: "object", fields, text });
75
28
  }
76
- function array(items, text) {
77
- return brand({
78
- type: "array",
79
- items,
80
- text
81
- });
29
+ function makeArrayAnn(items, text) {
30
+ return brand({ type: "array", items, text });
82
31
  }
83
- function func(text) {
84
- return brand({
85
- type: "function",
86
- text
87
- });
32
+ function makeFunctionAnn(text) {
33
+ return brand({ type: "function", text });
88
34
  }
89
- function unknown(value, text) {
90
- return brand({
91
- type: "unknown",
92
- value,
93
- text
94
- });
35
+ function makeUnknownAnn(value, text) {
36
+ return brand({ type: "unknown", value, text });
95
37
  }
96
- function scalar(value, text) {
97
- return brand({
98
- type: "scalar",
99
- value,
100
- text
101
- });
38
+ function makeScalarAnn(value, text) {
39
+ return brand({ type: "scalar", value, text });
102
40
  }
103
- function circularRef(text) {
104
- return brand({
105
- type: "circular-ref",
106
- text
107
- });
41
+ function makeCircularRefAnn(text) {
42
+ return brand({ type: "circular-ref", text });
108
43
  }
109
44
  function updateText(annotation, text) {
110
45
  if (text !== void 0) {
@@ -114,52 +49,53 @@ function updateText(annotation, text) {
114
49
  }
115
50
  }
116
51
  function merge(objAnnotation, fields) {
117
- const newFields = { ...objAnnotation.fields, ...fields };
118
- return object(newFields, objAnnotation.text);
52
+ const newFields = new Map([...objAnnotation.fields, ...fields]);
53
+ return makeObjectAnn(newFields, objAnnotation.text);
119
54
  }
120
- function asAnnotation(thing) {
121
- return typeof thing === "object" && thing !== null && _register.has(thing) ? thing : void 0;
55
+ function isAnnotation(thing) {
56
+ return _register.has(thing);
122
57
  }
123
- function annotateArray(value, text, seen) {
124
- seen.add(value);
125
- const items = value.map((v) => annotate(v, void 0, seen));
126
- return array(items, text);
58
+ function annotateArray(arr, text, seen) {
59
+ seen.add(arr);
60
+ const items = [];
61
+ for (const value of arr) {
62
+ items.push(annotate(value, void 0, seen));
63
+ }
64
+ return makeArrayAnn(items, text);
127
65
  }
128
66
  function annotateObject(obj, text, seen) {
129
67
  seen.add(obj);
130
- const fields = {};
131
- for (const key of Object.keys(obj)) {
132
- const value = obj[key];
133
- fields[key] = annotate(value, void 0, seen);
68
+ const fields = /* @__PURE__ */ new Map();
69
+ for (const [key, value] of Object.entries(obj)) {
70
+ fields.set(key, annotate(value, void 0, seen));
134
71
  }
135
- return object(fields, text);
72
+ return makeObjectAnn(fields, text);
136
73
  }
137
74
  function annotate(value, text, seen) {
138
75
  if (value === null || value === void 0 || typeof value === "string" || typeof value === "number" || typeof value === "boolean" || typeof value === "symbol" || typeof value.getMonth === "function") {
139
- return scalar(value, text);
76
+ return makeScalarAnn(value, text);
140
77
  }
141
- const ann = asAnnotation(value);
142
- if (ann) {
143
- return updateText(ann, text);
78
+ if (isAnnotation(value)) {
79
+ return updateText(value, text);
144
80
  }
145
81
  if (Array.isArray(value)) {
146
82
  if (seen.has(value)) {
147
- return circularRef(text);
83
+ return makeCircularRefAnn(text);
148
84
  } else {
149
85
  return annotateArray(value, text, seen);
150
86
  }
151
87
  }
152
88
  if (isPojo(value)) {
153
89
  if (seen.has(value)) {
154
- return circularRef(text);
90
+ return makeCircularRefAnn(text);
155
91
  } else {
156
92
  return annotateObject(value, text, seen);
157
93
  }
158
94
  }
159
95
  if (typeof value === "function") {
160
- return func(text);
96
+ return makeFunctionAnn(text);
161
97
  }
162
- return unknown(value, text);
98
+ return makeUnknownAnn(value, text);
163
99
  }
164
100
  function public_annotate(value, text) {
165
101
  return annotate(value, text, /* @__PURE__ */ new WeakSet());
@@ -168,7 +104,52 @@ function public_annotateObject(obj, text) {
168
104
  return annotateObject(obj, text, /* @__PURE__ */ new WeakSet());
169
105
  }
170
106
 
171
- // src/format.ts
107
+ // src/lib/text.ts
108
+ var INDENT = " ";
109
+ function isMultiline(s) {
110
+ return s.indexOf("\n") >= 0;
111
+ }
112
+ function indent(s, prefix = INDENT) {
113
+ if (isMultiline(s)) {
114
+ return s.split("\n").map((line) => `${prefix}${line}`).join("\n");
115
+ } else {
116
+ return `${prefix}${s}`;
117
+ }
118
+ }
119
+
120
+ // src/core/format.ts
121
+ function summarize(ann, keypath = []) {
122
+ const result = [];
123
+ if (ann.type === "array") {
124
+ const items = ann.items;
125
+ let index = 0;
126
+ for (const ann2 of items) {
127
+ for (const item of summarize(ann2, [...keypath, index++])) {
128
+ result.push(item);
129
+ }
130
+ }
131
+ } else if (ann.type === "object") {
132
+ const fields = ann.fields;
133
+ for (const [key, value] of fields) {
134
+ for (const item of summarize(value, [...keypath, key])) {
135
+ result.push(item);
136
+ }
137
+ }
138
+ }
139
+ const text = ann.text;
140
+ if (!text) {
141
+ return result;
142
+ }
143
+ let prefix;
144
+ if (keypath.length === 0) {
145
+ prefix = "";
146
+ } else if (keypath.length === 1) {
147
+ prefix = typeof keypath[0] === "number" ? `Value at index ${keypath[0]}: ` : `Value at key ${JSON.stringify(keypath[0])}: `;
148
+ } else {
149
+ prefix = `Value at keypath ${keypath.map(String).join(".")}: `;
150
+ }
151
+ return [...result, `${prefix}${text}`];
152
+ }
172
153
  function serializeString(s, width = 80) {
173
154
  let ser = JSON.stringify(s);
174
155
  if (ser.length <= width) {
@@ -195,13 +176,11 @@ function serializeArray(annotation, prefix) {
195
176
  }
196
177
  function serializeObject(annotation, prefix) {
197
178
  const { fields } = annotation;
198
- const fieldNames = Object.keys(fields);
199
- if (fieldNames.length === 0) {
179
+ if (fields.size === 0) {
200
180
  return "{}";
201
181
  }
202
182
  const result = [];
203
- for (const key of fieldNames) {
204
- const valueAnnotation = fields[key];
183
+ for (const [key, valueAnnotation] of fields) {
205
184
  const kser = serializeValue(key);
206
185
  const valPrefix = `${prefix}${INDENT}${" ".repeat(kser.length + 2)}`;
207
186
  const [vser, vann] = serializeAnnotation(valueAnnotation, `${prefix}${INDENT}`);
@@ -215,16 +194,15 @@ function serializeObject(annotation, prefix) {
215
194
  function serializeValue(value) {
216
195
  if (typeof value === "string") {
217
196
  return serializeString(value);
218
- } else if (typeof value === "number" || typeof value === "boolean") {
197
+ } else if (typeof value === "number" || typeof value === "boolean" || typeof value === "symbol") {
219
198
  return value.toString();
220
199
  } else if (value === null) {
221
200
  return "null";
222
201
  } else if (value === void 0) {
223
202
  return "undefined";
224
203
  } else {
225
- const valueAsDate = asDate(value);
226
- if (valueAsDate !== null) {
227
- return `new Date(${JSON.stringify(valueAsDate.toISOString())})`;
204
+ if (isDate(value)) {
205
+ return `new Date(${JSON.stringify(value.toISOString())})`;
228
206
  } else if (value instanceof Date) {
229
207
  return "(Invalid Date)";
230
208
  } else {
@@ -268,7 +246,7 @@ function formatShort(ann) {
268
246
  return summarize(ann, []).join("\n");
269
247
  }
270
248
 
271
- // src/result.ts
249
+ // src/core/Result.ts
272
250
  function ok(value) {
273
251
  return { ok: true, value, error: void 0 };
274
252
  }
@@ -276,7 +254,7 @@ function err(error) {
276
254
  return { ok: false, value: void 0, error };
277
255
  }
278
256
 
279
- // src/Decoder.ts
257
+ // src/core/Decoder.ts
280
258
  function noThrow(fn) {
281
259
  return (t) => {
282
260
  try {
@@ -303,7 +281,7 @@ function define(fn) {
303
281
  return fn(
304
282
  blob,
305
283
  ok,
306
- (msg) => err(typeof msg === "string" ? public_annotate(blob, msg) : msg)
284
+ (msg) => err(isAnnotation(msg) ? msg : public_annotate(blob, msg))
307
285
  );
308
286
  }
309
287
  function verify(blob, formatter = formatInline) {
@@ -338,9 +316,9 @@ function define(fn) {
338
316
  });
339
317
  }
340
318
  function reject(rejectFn) {
341
- return then((value2, ok2, err2) => {
342
- const errmsg = rejectFn(value2);
343
- return errmsg === null ? ok2(value2) : err2(typeof errmsg === "string" ? public_annotate(value2, errmsg) : errmsg);
319
+ return then((blob, ok2, err2) => {
320
+ const errmsg = rejectFn(blob);
321
+ return errmsg === null ? ok2(blob) : err2(typeof errmsg === "string" ? public_annotate(blob, errmsg) : errmsg);
344
322
  });
345
323
  }
346
324
  function describe(message) {
@@ -353,7 +331,7 @@ function define(fn) {
353
331
  }
354
332
  });
355
333
  }
356
- function peek_UNSTABLE(next) {
334
+ function peek(next) {
357
335
  return define((blob, ok2, err2) => {
358
336
  const result = decode(blob);
359
337
  return result.ok ? next([blob, result.value], ok2, err2) : result;
@@ -368,30 +346,129 @@ function define(fn) {
368
346
  reject,
369
347
  describe,
370
348
  then,
371
- // EXPERIMENTAL - please DO NOT rely on this method
372
- peek_UNSTABLE
349
+ peek
373
350
  });
374
351
  }
375
352
 
376
- // src/lib/objects.ts
353
+ // src/arrays.ts
354
+ var poja = define((blob, ok2, err2) => {
355
+ if (!Array.isArray(blob)) {
356
+ return err2("Must be an array");
357
+ }
358
+ return ok2(blob);
359
+ });
360
+ function all(items, blobs, ok2, err2) {
361
+ const results = [];
362
+ for (let index = 0; index < items.length; ++index) {
363
+ const result = items[index];
364
+ if (result.ok) {
365
+ results.push(result.value);
366
+ } else {
367
+ const ann = result.error;
368
+ const clone = blobs.slice();
369
+ clone.splice(
370
+ index,
371
+ 1,
372
+ public_annotate(ann, ann.text ? `${ann.text} (at index ${index})` : `index ${index}`)
373
+ );
374
+ return err2(public_annotate(clone));
375
+ }
376
+ }
377
+ return ok2(results);
378
+ }
379
+ function array(decoder) {
380
+ const decodeFn = decoder.decode;
381
+ return poja.then((blobs, ok2, err2) => {
382
+ const results = blobs.map(decodeFn);
383
+ return all(results, blobs, ok2, err2);
384
+ });
385
+ }
386
+ function isNonEmpty(arr) {
387
+ return arr.length > 0;
388
+ }
389
+ function nonEmptyArray(decoder) {
390
+ return array(decoder).refine(isNonEmpty, "Must be non-empty array");
391
+ }
392
+ var ntuple = (n) => poja.refine((arr) => arr.length === n, `Must be a ${n}-tuple`);
393
+ function tuple(...decoders) {
394
+ return ntuple(decoders.length).then((blobs, ok2, err2) => {
395
+ let allOk = true;
396
+ const rvs = decoders.map((decoder, i) => {
397
+ const blob = blobs[i];
398
+ const result = decoder.decode(blob);
399
+ if (result.ok) {
400
+ return result.value;
401
+ } else {
402
+ allOk = false;
403
+ return result.error;
404
+ }
405
+ });
406
+ if (allOk) {
407
+ return ok2(rvs);
408
+ } else {
409
+ return err2(public_annotate(rvs));
410
+ }
411
+ });
412
+ }
413
+
414
+ // src/misc.ts
415
+ function instanceOf(klass) {
416
+ return define(
417
+ (blob, ok2, err2) => blob instanceof klass ? ok2(blob) : err2(`Must be ${klass.name} instance`)
418
+ );
419
+ }
420
+ function lazy(decoderFn) {
421
+ return define((blob) => decoderFn().decode(blob));
422
+ }
423
+ function prep(mapperFn, decoder) {
424
+ return define((originalInput, _, err2) => {
425
+ let blob;
426
+ try {
427
+ blob = mapperFn(originalInput);
428
+ } catch (e) {
429
+ return err2(
430
+ public_annotate(
431
+ originalInput,
432
+ // istanbul ignore next
433
+ e instanceof Error ? e.message : String(e)
434
+ )
435
+ );
436
+ }
437
+ const r = decoder.decode(blob);
438
+ return r.ok ? r : err2(public_annotate(originalInput, r.error.text));
439
+ });
440
+ }
441
+
442
+ // src/lib/set-methods.ts
443
+ function difference(xs, ys) {
444
+ const result = /* @__PURE__ */ new Set();
445
+ for (const x of xs) {
446
+ if (!ys.has(x)) {
447
+ result.add(x);
448
+ }
449
+ }
450
+ return result;
451
+ }
452
+
453
+ // src/objects.ts
377
454
  var pojo = define(
378
455
  (blob, ok2, err2) => isPojo(blob) ? ok2(blob) : err2("Must be an object")
379
456
  );
380
- function object2(decodersByKey) {
381
- const knownKeys = new Set(Object.keys(decodersByKey));
457
+ function object(decoders) {
458
+ const knownKeys = new Set(Object.keys(decoders));
382
459
  return pojo.then((plainObj, ok2, err2) => {
383
460
  const actualKeys = new Set(Object.keys(plainObj));
384
- const missingKeys = subtract(knownKeys, actualKeys);
385
- const record = {};
461
+ const missingKeys = difference(knownKeys, actualKeys);
462
+ const record2 = {};
386
463
  let errors = null;
387
- for (const key of Object.keys(decodersByKey)) {
388
- const decoder = decodersByKey[key];
464
+ for (const key of Object.keys(decoders)) {
465
+ const decoder = decoders[key];
389
466
  const rawValue = plainObj[key];
390
467
  const result = decoder.decode(rawValue);
391
468
  if (result.ok) {
392
469
  const value = result.value;
393
470
  if (value !== void 0) {
394
- record[key] = value;
471
+ record2[key] = value;
395
472
  }
396
473
  missingKeys.delete(key);
397
474
  } else {
@@ -400,9 +477,9 @@ function object2(decodersByKey) {
400
477
  missingKeys.add(key);
401
478
  } else {
402
479
  if (errors === null) {
403
- errors = {};
480
+ errors = /* @__PURE__ */ new Map();
404
481
  }
405
- errors[key] = ann;
482
+ errors.set(key, ann);
406
483
  }
407
484
  }
408
485
  }
@@ -418,26 +495,26 @@ function object2(decodersByKey) {
418
495
  }
419
496
  return err2(objAnn);
420
497
  }
421
- return ok2(record);
498
+ return ok2(record2);
422
499
  });
423
500
  }
424
- function exact(decodersByKey) {
425
- const allowedKeys = new Set(Object.keys(decodersByKey));
501
+ function exact(decoders) {
502
+ const allowedKeys = new Set(Object.keys(decoders));
426
503
  const checked = pojo.reject((plainObj) => {
427
504
  const actualKeys = new Set(Object.keys(plainObj));
428
- const extraKeys = subtract(actualKeys, allowedKeys);
505
+ const extraKeys = difference(actualKeys, allowedKeys);
429
506
  return extraKeys.size > 0 ? `Unexpected extra keys: ${Array.from(extraKeys).join(", ")}` : (
430
507
  // Don't reject
431
508
  null
432
509
  );
433
510
  });
434
- return checked.then(object2(decodersByKey).decode);
511
+ return checked.then(object(decoders).decode);
435
512
  }
436
- function inexact(decodersByKey) {
513
+ function inexact(decoders) {
437
514
  return pojo.then((plainObj) => {
438
515
  const allkeys = new Set(Object.keys(plainObj));
439
- const decoder = object2(decodersByKey).transform((safepart) => {
440
- const safekeys = new Set(Object.keys(decodersByKey));
516
+ const decoder = object(decoders).transform((safepart) => {
517
+ const safekeys = new Set(Object.keys(decoders));
441
518
  for (const k of safekeys)
442
519
  allkeys.add(k);
443
520
  const rv = {};
@@ -456,77 +533,14 @@ function inexact(decodersByKey) {
456
533
  return decoder.decode(plainObj);
457
534
  });
458
535
  }
459
- function dict(decoder) {
460
- return pojo.then((plainObj, ok2, err2) => {
461
- let rv = {};
462
- let errors = null;
463
- for (const key of Object.keys(plainObj)) {
464
- const value = plainObj[key];
465
- const result = decoder.decode(value);
466
- if (result.ok) {
467
- if (errors === null) {
468
- rv[key] = result.value;
469
- }
470
- } else {
471
- rv = {};
472
- if (errors === null) {
473
- errors = {};
474
- }
475
- errors[key] = result.error;
476
- }
477
- }
478
- if (errors !== null) {
479
- return err2(merge(public_annotateObject(plainObj), errors));
480
- } else {
481
- return ok2(rv);
482
- }
483
- });
484
- }
485
- function mapping(decoder) {
486
- return dict(decoder).transform(
487
- (obj) => new Map(
488
- // This is effectively Object.entries(obj), but in a way that Flow
489
- // will know the types are okay
490
- Object.keys(obj).map((key) => [key, obj[key]])
491
- )
492
- );
493
- }
494
-
495
- // src/lib/utilities.ts
496
- function instanceOf(klass) {
497
- return define(
498
- (blob, ok2, err2) => blob instanceof klass ? ok2(blob) : err2(`Must be ${klass.name} instance`)
499
- );
500
- }
501
- function lazy(decoderFn) {
502
- return define((blob) => decoderFn().decode(blob));
503
- }
504
- function prep(mapperFn, decoder) {
505
- return define((originalInput, _, err2) => {
506
- let blob;
507
- try {
508
- blob = mapperFn(originalInput);
509
- } catch (e) {
510
- return err2(
511
- public_annotate(
512
- originalInput,
513
- // istanbul ignore next
514
- e instanceof Error ? e.message : String(e)
515
- )
516
- );
517
- }
518
- const r = decoder.decode(blob);
519
- return r.ok ? r : err2(public_annotate(originalInput, r.error.text));
520
- });
521
- }
522
536
 
523
- // src/lib/unions.ts
537
+ // src/unions.ts
524
538
  var EITHER_PREFIX = "Either:\n";
525
539
  function itemize(s) {
526
540
  return `-${indent(s).substring(1)}`;
527
541
  }
528
542
  function nest(errText) {
529
- return errText.startsWith(EITHER_PREFIX) ? errText.substr(EITHER_PREFIX.length) : itemize(errText);
543
+ return errText.startsWith(EITHER_PREFIX) ? errText.substring(EITHER_PREFIX.length) : itemize(errText);
530
544
  }
531
545
  function either(...decoders) {
532
546
  if (decoders.length === 0) {
@@ -557,23 +571,45 @@ function oneOf(constants) {
557
571
  );
558
572
  });
559
573
  }
574
+ function enum_(enumObj) {
575
+ const values = Object.values(enumObj);
576
+ if (!values.some(isNumber)) {
577
+ return oneOf(values);
578
+ } else {
579
+ const nums = values.filter(isNumber);
580
+ const ignore = new Set(nums.map((val) => enumObj[val]));
581
+ const strings = values.filter(isString).filter((val) => !ignore.has(val));
582
+ return oneOf([...nums, ...strings]);
583
+ }
584
+ }
560
585
  function taggedUnion(field, mapping2) {
561
- const base = object2({
586
+ const scout = object({
562
587
  [field]: prep(String, oneOf(Object.keys(mapping2)))
563
588
  }).transform((o) => o[field]);
564
- return base.peek_UNSTABLE(([blob, key]) => {
565
- const decoder = mapping2[key];
589
+ return select(
590
+ scout,
591
+ // peek...
592
+ (key) => mapping2[key]
593
+ // ...then select
594
+ );
595
+ }
596
+ function select(scout, selectFn) {
597
+ return scout.peek(([blob, peekResult]) => {
598
+ const decoder = selectFn(peekResult);
566
599
  return decoder.decode(blob);
567
600
  });
568
601
  }
569
602
 
570
- // src/lib/basics.ts
603
+ // src/basics.ts
604
+ function lazyval(value) {
605
+ return typeof value === "function" ? value() : value;
606
+ }
571
607
  var null_ = constant(null);
572
608
  var undefined_ = constant(void 0);
573
609
  var nullish_ = define(
574
- (blob, ok2, err2) => blob == null ? ok2(blob) : (
575
- // Combine error message into a single line for readability
576
- err2("Must be undefined or null")
610
+ (blob, ok2, err2) => (
611
+ // Equiv to either(undefined_, null_), but combined for better error message
612
+ blob == null ? ok2(blob) : err2("Must be undefined or null")
577
613
  )
578
614
  );
579
615
  function optional(decoder, defaultValue) {
@@ -591,7 +627,9 @@ function nullish(decoder, defaultValue) {
591
627
  }
592
628
  function constant(value) {
593
629
  return define(
594
- (blob, ok2, err2) => blob === value ? ok2(value) : err2(`Must be ${JSON.stringify(value)}`)
630
+ (blob, ok2, err2) => blob === value ? ok2(value) : err2(
631
+ `Must be ${typeof value === "symbol" ? String(value) : JSON.stringify(value)}`
632
+ )
595
633
  );
596
634
  }
597
635
  function always(value) {
@@ -604,76 +642,12 @@ function never(msg) {
604
642
  }
605
643
  var fail = never;
606
644
  var hardcoded = always;
607
- var unknown2 = define((blob, ok2, _) => ok2(blob));
608
- var mixed = unknown2;
609
-
610
- // src/lib/arrays.ts
611
- var poja = define((blob, ok2, err2) => {
612
- if (!Array.isArray(blob)) {
613
- return err2("Must be an array");
614
- }
615
- return ok2(blob);
616
- });
617
- function all(items, blobs, ok2, err2) {
618
- const results = [];
619
- for (let index = 0; index < items.length; ++index) {
620
- const result = items[index];
621
- if (result.ok) {
622
- results.push(result.value);
623
- } else {
624
- const ann = result.error;
625
- const clone = blobs.slice();
626
- clone.splice(
627
- index,
628
- 1,
629
- public_annotate(ann, ann.text ? `${ann.text} (at index ${index})` : `index ${index}`)
630
- );
631
- return err2(public_annotate(clone));
632
- }
633
- }
634
- return ok2(results);
635
- }
636
- function array2(decoder) {
637
- const decodeFn = decoder.decode;
638
- return poja.then((blobs, ok2, err2) => {
639
- const results = blobs.map(decodeFn);
640
- return all(results, blobs, ok2, err2);
641
- });
642
- }
643
- function isNonEmpty(arr) {
644
- return arr.length > 0;
645
- }
646
- function nonEmptyArray(decoder) {
647
- return array2(decoder).refine(isNonEmpty, "Must be non-empty array");
648
- }
649
- function set(decoder) {
650
- return array2(decoder).transform((items) => new Set(items));
651
- }
652
- var ntuple = (n) => poja.refine((arr) => arr.length === n, `Must be a ${n}-tuple`);
653
- function tuple(...decoders) {
654
- return ntuple(decoders.length).then((blobs, ok2, err2) => {
655
- let allOk = true;
656
- const rvs = decoders.map((decoder, i) => {
657
- const blob = blobs[i];
658
- const result = decoder.decode(blob);
659
- if (result.ok) {
660
- return result.value;
661
- } else {
662
- allOk = false;
663
- return result.error;
664
- }
665
- });
666
- if (allOk) {
667
- return ok2(rvs);
668
- } else {
669
- return err2(public_annotate(rvs));
670
- }
671
- });
672
- }
645
+ var unknown = define((blob, ok2, _) => ok2(blob));
646
+ var mixed = unknown;
673
647
 
674
- // src/lib/numbers.ts
648
+ // src/numbers.ts
675
649
  var anyNumber = define(
676
- (blob, ok2, err2) => typeof blob === "number" ? ok2(blob) : err2("Must be number")
650
+ (blob, ok2, err2) => isNumber(blob) ? ok2(blob) : err2("Must be number")
677
651
  );
678
652
  var number = anyNumber.refine(
679
653
  (n) => Number.isFinite(n),
@@ -683,20 +657,73 @@ var integer = number.refine(
683
657
  (n) => Number.isInteger(n),
684
658
  "Number must be an integer"
685
659
  );
686
- var positiveNumber = number.refine((n) => n >= 0, "Number must be positive").transform(Math.abs);
687
- var positiveInteger = integer.refine((n) => n >= 0, "Number must be positive").transform(Math.abs);
660
+ var positiveNumber = number.refine(
661
+ (n) => n >= 0 && !Object.is(n, -0),
662
+ "Number must be positive"
663
+ );
664
+ var positiveInteger = integer.refine(
665
+ (n) => n >= 0 && !Object.is(n, -0),
666
+ "Number must be positive"
667
+ );
668
+ var bigint = define(
669
+ (blob, ok2, err2) => isBigInt(blob) ? ok2(blob) : err2("Must be bigint")
670
+ );
688
671
 
689
- // src/lib/booleans.ts
672
+ // src/booleans.ts
690
673
  var boolean = define((blob, ok2, err2) => {
691
674
  return typeof blob === "boolean" ? ok2(blob) : err2("Must be boolean");
692
675
  });
693
676
  var truthy = define((blob, ok2, _) => ok2(!!blob));
694
677
  var numericBoolean = number.transform((n) => !!n);
695
678
 
696
- // src/lib/strings.ts
679
+ // src/collections.ts
680
+ function record(fst, snd) {
681
+ const keyDecoder = snd !== void 0 ? fst : void 0;
682
+ const valueDecoder = snd !== void 0 ? snd : fst;
683
+ return pojo.then((input, ok2, err2) => {
684
+ let rv = {};
685
+ const errors = /* @__PURE__ */ new Map();
686
+ for (const [key, value] of Object.entries(input)) {
687
+ const keyResult = _optionalChain([keyDecoder, 'optionalAccess', _2 => _2.decode, 'call', _3 => _3(key)]);
688
+ if (_optionalChain([keyResult, 'optionalAccess', _4 => _4.ok]) === false) {
689
+ return err2(
690
+ public_annotate(
691
+ input,
692
+ `Invalid key ${JSON.stringify(key)}: ${formatShort(keyResult.error)}`
693
+ )
694
+ );
695
+ }
696
+ const k = _nullishCoalesce(_optionalChain([keyResult, 'optionalAccess', _5 => _5.value]), () => ( key));
697
+ const result = valueDecoder.decode(value);
698
+ if (result.ok) {
699
+ if (errors.size === 0) {
700
+ rv[k] = result.value;
701
+ }
702
+ } else {
703
+ errors.set(key, result.error);
704
+ rv = {};
705
+ }
706
+ }
707
+ if (errors.size > 0) {
708
+ return err2(merge(public_annotateObject(input), errors));
709
+ } else {
710
+ return ok2(rv);
711
+ }
712
+ });
713
+ }
714
+ var dict = record;
715
+ function setFromArray(decoder) {
716
+ return array(decoder).transform((items) => new Set(items));
717
+ }
718
+ var set = setFromArray;
719
+ function mapping(decoder) {
720
+ return record(decoder).transform((obj) => new Map(Object.entries(obj)));
721
+ }
722
+
723
+ // src/strings.ts
697
724
  var url_re = /^([A-Za-z]{3,9}(?:[+][A-Za-z]{3,9})?):\/\/(?:([-;:&=+$,\w]+)@)?(?:([A-Za-z0-9.-]+)(?::([0-9]{2,5}))?)(\/(?:[-+~%/.,\w]*)?(?:\?[-+=&;%@.,/\w]*)?(?:#[.,!/\w]*)?)?$/;
698
725
  var string = define(
699
- (blob, ok2, err2) => typeof blob === "string" ? ok2(blob) : err2("Must be string")
726
+ (blob, ok2, err2) => isString(blob) ? ok2(blob) : err2("Must be string")
700
727
  );
701
728
  var nonEmptyString = regex(/\S/, "Must be non-empty string");
702
729
  function regex(regex2, msg) {
@@ -727,12 +754,17 @@ var uuidv4 = (
727
754
  // https://en.wikipedia.org/wiki/Universally_unique_identifier#Version_4_(random)
728
755
  uuid.refine((value) => value[14] === "4", "Must be uuidv4")
729
756
  );
757
+ var decimal = regex(/^[0-9]+$/, "Must only contain digits");
758
+ var hexadecimal = regex(
759
+ /^[0-9a-f]+$/i,
760
+ "Must only contain hexadecimal digits"
761
+ );
762
+ var numeric = decimal.transform(Number);
730
763
 
731
- // src/lib/dates.ts
764
+ // src/dates.ts
732
765
  var iso8601_re = /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(?:[.]\d+)?(?:Z|[+-]\d{2}:?\d{2})$/;
733
766
  var date = define((blob, ok2, err2) => {
734
- const date2 = asDate(blob);
735
- return date2 !== null ? ok2(date2) : err2("Must be a Date");
767
+ return isDate(blob) ? ok2(blob) : err2("Must be a Date");
736
768
  });
737
769
  var iso8601 = (
738
770
  // Input itself needs to match the ISO8601 regex...
@@ -747,10 +779,11 @@ var iso8601 = (
747
779
  }
748
780
  )
749
781
  );
782
+ var datelike = either(date, iso8601).describe("Must be a Date or date string");
750
783
 
751
- // src/lib/json.ts
752
- var jsonObject = lazy(() => dict(json));
753
- var jsonArray = lazy(() => array2(json));
784
+ // src/json.ts
785
+ var jsonObject = lazy(() => record(json));
786
+ var jsonArray = lazy(() => array(json));
754
787
  var json = either(
755
788
  null_,
756
789
  string,
@@ -817,5 +850,13 @@ var json = either(
817
850
 
818
851
 
819
852
 
820
- 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.nullish = nullish; 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;
821
- //# sourceMappingURL=index.cjs.map
853
+
854
+
855
+
856
+
857
+
858
+
859
+
860
+
861
+
862
+ exports.always = always; exports.anyNumber = anyNumber; exports.array = array; exports.bigint = bigint; exports.boolean = boolean; exports.constant = constant; exports.date = date; exports.datelike = datelike; exports.decimal = decimal; exports.define = define; exports.dict = dict; exports.either = either; exports.email = email; exports.enum_ = enum_; exports.err = err; exports.exact = exact; exports.fail = fail; exports.formatInline = formatInline; exports.formatShort = formatShort; exports.hardcoded = hardcoded; exports.hexadecimal = hexadecimal; 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.nullish = nullish; exports.number = number; exports.numeric = numeric; exports.numericBoolean = numericBoolean; exports.object = object; 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.record = record; exports.regex = regex; exports.select = select; exports.set = set; exports.setFromArray = setFromArray; exports.string = string; exports.taggedUnion = taggedUnion; exports.truthy = truthy; exports.tuple = tuple; exports.undefined_ = undefined_; exports.unknown = unknown; exports.url = url; exports.uuid = uuid; exports.uuidv1 = uuidv1; exports.uuidv4 = uuidv4;