decoders 2.1.0 → 2.2.0-test2

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 (100) hide show
  1. package/README.md +12 -12
  2. package/annotate-0PUmWHxH.d.ts +33 -0
  3. package/chunk-BPSZE2VX.js +13 -0
  4. package/chunk-RUMDX66L.js +179 -0
  5. package/dist/annotate-0PUmWHxH.d.mts +33 -0
  6. package/dist/annotate-0PUmWHxH.d.ts +33 -0
  7. package/dist/chunk-BPSZE2VX.js +13 -0
  8. package/dist/chunk-BPSZE2VX.js.map +1 -0
  9. package/dist/chunk-HBFFQIIN.mjs +13 -0
  10. package/dist/chunk-HBFFQIIN.mjs.map +1 -0
  11. package/dist/chunk-RUMDX66L.js +179 -0
  12. package/dist/chunk-RUMDX66L.js.map +1 -0
  13. package/dist/chunk-ZTKFAKRL.mjs +179 -0
  14. package/dist/chunk-ZTKFAKRL.mjs.map +1 -0
  15. package/dist/format.d.mts +9 -0
  16. package/dist/format.d.ts +9 -0
  17. package/dist/format.js +13 -0
  18. package/dist/format.js.map +1 -0
  19. package/dist/format.mjs +13 -0
  20. package/dist/format.mjs.map +1 -0
  21. package/dist/index.d.mts +454 -0
  22. package/dist/index.d.ts +454 -0
  23. package/dist/index.js +659 -0
  24. package/dist/index.js.map +1 -0
  25. package/dist/index.mjs +659 -0
  26. package/dist/index.mjs.map +1 -0
  27. package/dist/result.d.mts +26 -0
  28. package/dist/result.d.ts +26 -0
  29. package/dist/result.js +9 -0
  30. package/dist/result.js.map +1 -0
  31. package/dist/result.mjs +9 -0
  32. package/dist/result.mjs.map +1 -0
  33. package/format.d.ts +7 -4
  34. package/format.js +12 -116
  35. package/index.d.ts +454 -40
  36. package/index.js +659 -67
  37. package/package.json +163 -100
  38. package/result.d.ts +25 -15
  39. package/result.js +6 -18
  40. package/CHANGELOG.md +0 -670
  41. package/Decoder.d.ts +0 -94
  42. package/Decoder.js +0 -105
  43. package/Decoder.js.flow +0 -286
  44. package/Decoder.mjs +0 -101
  45. package/NotSupportedTSVersion.d.ts +0 -1
  46. package/_utils.d.ts +0 -9
  47. package/_utils.js +0 -80
  48. package/_utils.js.flow +0 -107
  49. package/_utils.mjs +0 -70
  50. package/annotate.d.ts +0 -62
  51. package/annotate.js +0 -145
  52. package/annotate.js.flow +0 -218
  53. package/annotate.mjs +0 -131
  54. package/format.js.flow +0 -128
  55. package/format.mjs +0 -110
  56. package/index.js.flow +0 -44
  57. package/index.mjs +0 -11
  58. package/lib/_helpers.d.ts +0 -79
  59. package/lib/arrays.d.ts +0 -59
  60. package/lib/arrays.js +0 -85
  61. package/lib/arrays.js.flow +0 -138
  62. package/lib/arrays.mjs +0 -75
  63. package/lib/basics.d.ts +0 -93
  64. package/lib/basics.js +0 -74
  65. package/lib/basics.js.flow +0 -124
  66. package/lib/basics.mjs +0 -60
  67. package/lib/booleans.d.ts +0 -16
  68. package/lib/booleans.js +0 -21
  69. package/lib/booleans.js.flow +0 -22
  70. package/lib/booleans.mjs +0 -15
  71. package/lib/dates.d.ts +0 -15
  72. package/lib/dates.js +0 -23
  73. package/lib/dates.js.flow +0 -40
  74. package/lib/dates.mjs +0 -17
  75. package/lib/json.d.ts +0 -35
  76. package/lib/json.js +0 -25
  77. package/lib/json.js.flow +0 -50
  78. package/lib/json.mjs +0 -18
  79. package/lib/numbers.d.ts +0 -31
  80. package/lib/numbers.js +0 -34
  81. package/lib/numbers.js.flow +0 -46
  82. package/lib/numbers.mjs +0 -25
  83. package/lib/objects.d.ts +0 -76
  84. package/lib/objects.js +0 -138
  85. package/lib/objects.js.flow +0 -238
  86. package/lib/objects.mjs +0 -128
  87. package/lib/strings.d.ts +0 -54
  88. package/lib/strings.js +0 -54
  89. package/lib/strings.js.flow +0 -90
  90. package/lib/strings.mjs +0 -40
  91. package/lib/unions.d.ts +0 -55
  92. package/lib/unions.js +0 -82
  93. package/lib/unions.js.flow +0 -155
  94. package/lib/unions.mjs +0 -75
  95. package/lib/utilities.d.ts +0 -40
  96. package/lib/utilities.js +0 -44
  97. package/lib/utilities.js.flow +0 -65
  98. package/lib/utilities.mjs +0 -35
  99. package/result.js.flow +0 -26
  100. package/result.mjs +0 -15
package/annotate.mjs DELETED
@@ -1,131 +0,0 @@
1
- function _extends() {
2
- _extends = Object.assign
3
- ? Object.assign.bind()
4
- : function (target) {
5
- for (var i = 1; i < arguments.length; i++) {
6
- var source = arguments[i]
7
- for (var key in source) {
8
- if (Object.prototype.hasOwnProperty.call(source, key)) {
9
- target[key] = source[key]
10
- }
11
- }
12
- }
13
- return target
14
- }
15
- return _extends.apply(this, arguments)
16
- }
17
- var _register = new WeakSet()
18
- function brand(ann) {
19
- _register.add(ann)
20
- return ann
21
- }
22
- export function object(fields, text) {
23
- return brand({
24
- type: 'object',
25
- fields: fields,
26
- text: text,
27
- })
28
- }
29
- export function array(items, text) {
30
- return brand({
31
- type: 'array',
32
- items: items,
33
- text: text,
34
- })
35
- }
36
- export function func(text) {
37
- return brand({
38
- type: 'function',
39
- text: text,
40
- })
41
- }
42
- export function unknown(value, text) {
43
- return brand({
44
- type: 'unknown',
45
- value: value,
46
- text: text,
47
- })
48
- }
49
- export function scalar(value, text) {
50
- return brand({
51
- type: 'scalar',
52
- value: value,
53
- text: text,
54
- })
55
- }
56
- export function circularRef(text) {
57
- return brand({
58
- type: 'circular-ref',
59
- text: text,
60
- })
61
- }
62
-
63
- export function updateText(annotation, text) {
64
- if (text !== undefined) {
65
- return brand(
66
- _extends({}, annotation, {
67
- text: text,
68
- })
69
- )
70
- } else {
71
- return annotation
72
- }
73
- }
74
-
75
- export function merge(objAnnotation, fields) {
76
- var newFields = _extends({}, objAnnotation.fields, fields)
77
- return object(newFields, objAnnotation.text)
78
- }
79
- export function asAnnotation(thing) {
80
- return typeof thing === 'object' && thing !== null && _register.has(thing) ? thing : undefined
81
- }
82
- function annotateArray(value, text, seen) {
83
- seen.add(value)
84
- var items = value.map(function (v) {
85
- return annotate(v, undefined, seen)
86
- })
87
- return array(items, text)
88
- }
89
- function annotateObject(obj, text, seen) {
90
- seen.add(obj)
91
- var fields = {}
92
- Object.keys(obj).forEach(function (key) {
93
- var value = obj[key]
94
- fields[key] = annotate(value, undefined, seen)
95
- })
96
- return object(fields, text)
97
- }
98
- function annotate(value, text, seen) {
99
- if (value === null || value === undefined || typeof value === 'string' || typeof value === 'number' || typeof value === 'boolean' || typeof value === 'symbol' || typeof value.getMonth === 'function') {
100
- return scalar(value, text)
101
- }
102
- var ann = asAnnotation(value)
103
- if (ann) {
104
- return updateText(ann, text)
105
- }
106
- if (Array.isArray(value)) {
107
- if (seen.has(value)) {
108
- return circularRef(text)
109
- } else {
110
- return annotateArray(value, text, seen)
111
- }
112
- }
113
- if (typeof value === 'object') {
114
- if (seen.has(value)) {
115
- return circularRef(text)
116
- } else {
117
- return annotateObject(value, text, seen)
118
- }
119
- }
120
- if (typeof value === 'function') {
121
- return func(text)
122
- }
123
- return unknown(value, text)
124
- }
125
- function public_annotate(value, text) {
126
- return annotate(value, text, new WeakSet())
127
- }
128
- function public_annotateObject(obj, text) {
129
- return annotateObject(obj, text, new WeakSet())
130
- }
131
- export { public_annotate as annotate, public_annotateObject as annotateObject, annotate as __private_annotate }
package/format.js.flow DELETED
@@ -1,128 +0,0 @@
1
- // @flow strict
2
-
3
- import { summarize as _summarize, asDate, INDENT, indent, isMultiline } from './_utils';
4
- import type { Annotation, ArrayAnnotation, ObjectAnnotation } from './annotate';
5
-
6
- export type Formatter = (err: Annotation) => string | Error;
7
-
8
- function serializeString(s: string, width: number = 80): string {
9
- // Full string
10
- // Abbreviated to $maxlen i.e. "Vincent Driess..." [truncated]
11
- let ser = JSON.stringify(s);
12
- if (ser.length <= width) {
13
- return ser;
14
- }
15
-
16
- // Cut off a bit
17
- const truncated = `${s.substring(0, width - 15)}...`;
18
- ser = `${JSON.stringify(truncated)} [truncated]`;
19
- return ser;
20
- }
21
-
22
- function serializeArray(annotation: ArrayAnnotation, prefix: string): string {
23
- const { items } = annotation;
24
- if (items.length === 0) {
25
- return '[]';
26
- }
27
-
28
- const result = [];
29
- items.forEach((item) => {
30
- const [ser, ann] = serializeAnnotation(item, `${prefix}${INDENT}`);
31
- result.push(`${prefix}${INDENT}${ser}${','}`);
32
- if (ann !== undefined) {
33
- result.push(indent(ann, `${prefix}${INDENT}`));
34
- }
35
- });
36
- return ['[', ...result, `${prefix}]`].join('\n');
37
- }
38
-
39
- function serializeObject(annotation: ObjectAnnotation, prefix: string): string {
40
- const { fields } = annotation;
41
-
42
- const fieldNames = Object.keys(fields);
43
- if (fieldNames.length === 0) {
44
- return '{}';
45
- }
46
-
47
- const result = [];
48
- fieldNames.forEach((key) => {
49
- const valueAnnotation = fields[key];
50
- const kser = serializeValue(key);
51
-
52
- const valPrefix = `${prefix}${INDENT}${' '.repeat(kser.length + 2)}`;
53
- const [vser, vann] = serializeAnnotation(valueAnnotation, `${prefix}${INDENT}`);
54
-
55
- result.push(`${prefix}${INDENT}${kser}: ${vser},`);
56
- if (vann !== undefined) {
57
- result.push(indent(vann, valPrefix));
58
- }
59
- });
60
- return ['{', ...result, `${prefix}}`].join('\n');
61
- }
62
-
63
- export function serializeValue(value: mixed): string {
64
- // istanbul ignore else
65
- if (typeof value === 'string') {
66
- return serializeString(value);
67
- } else if (typeof value === 'number' || typeof value === 'boolean') {
68
- return value.toString();
69
- } else if (value === null) {
70
- return 'null';
71
- } else if (value === undefined) {
72
- return 'undefined';
73
- } else {
74
- const valueAsDate = asDate(value);
75
- if (valueAsDate !== null) {
76
- return `new Date(${JSON.stringify(valueAsDate.toISOString())})`;
77
- } else if (value instanceof Date) {
78
- // NOTE: Using `instanceof Date` is unreliable way of checking dates.
79
- // If this case occurs (and it didn't pass the prior asDate())
80
- // check, then this must be the case where it's an invalid date.
81
- return '(Invalid Date)';
82
- } else {
83
- return '(unserializable)';
84
- }
85
- }
86
- }
87
-
88
- export function serializeAnnotation(
89
- ann: Annotation,
90
- prefix: string = '',
91
- ): [string, string | void] {
92
- // The serialized data (the input object echoed back)
93
- let serialized;
94
- if (ann.type === 'array') {
95
- serialized = serializeArray(ann, prefix);
96
- } else if (ann.type === 'object') {
97
- serialized = serializeObject(ann, prefix);
98
- } else if (ann.type === 'function') {
99
- serialized = '<function>';
100
- } else if (ann.type === 'circular-ref') {
101
- serialized = '<circular ref>';
102
- } else if (ann.type === 'unknown') {
103
- serialized = '???';
104
- } else {
105
- serialized = serializeValue(ann.value);
106
- }
107
-
108
- const text = ann.text;
109
- if (text !== undefined) {
110
- const sep = '^'.repeat(isMultiline(serialized) ? 1 : serialized.length);
111
- return [serialized, [sep, text].join(isMultiline(text) ? '\n' : ' ')];
112
- } else {
113
- return [serialized, undefined];
114
- }
115
- }
116
-
117
- export function formatInline(ann: Annotation): string {
118
- const [serialized, annotation] = serializeAnnotation(ann);
119
- if (annotation !== undefined) {
120
- return `${serialized}\n${annotation}`;
121
- } else {
122
- return serialized;
123
- }
124
- }
125
-
126
- export function formatShort(ann: Annotation): string {
127
- return _summarize(ann, []).join('\n');
128
- }
package/format.mjs DELETED
@@ -1,110 +0,0 @@
1
- import { summarize as _summarize, asDate, INDENT, indent, isMultiline } from './_utils.mjs'
2
- function serializeString(s, width) {
3
- if (width === void 0) {
4
- width = 80
5
- }
6
- var ser = JSON.stringify(s)
7
- if (ser.length <= width) {
8
- return ser
9
- }
10
- var truncated = s.substring(0, width - 15) + '...'
11
- ser = JSON.stringify(truncated) + ' [truncated]'
12
- return ser
13
- }
14
- function serializeArray(annotation, prefix) {
15
- var items = annotation.items
16
- if (items.length === 0) {
17
- return '[]'
18
- }
19
- var result = []
20
- items.forEach(function (item) {
21
- var _serializeAnnotation = serializeAnnotation(item, '' + prefix + INDENT),
22
- ser = _serializeAnnotation[0],
23
- ann = _serializeAnnotation[1]
24
- result.push('' + prefix + INDENT + ser + ',')
25
- if (ann !== undefined) {
26
- result.push(indent(ann, '' + prefix + INDENT))
27
- }
28
- })
29
- return ['['].concat(result, [prefix + ']']).join('\n')
30
- }
31
- function serializeObject(annotation, prefix) {
32
- var fields = annotation.fields
33
- var fieldNames = Object.keys(fields)
34
- if (fieldNames.length === 0) {
35
- return '{}'
36
- }
37
- var result = []
38
- fieldNames.forEach(function (key) {
39
- var valueAnnotation = fields[key]
40
- var kser = serializeValue(key)
41
- var valPrefix = '' + prefix + INDENT + ' '.repeat(kser.length + 2)
42
- var _serializeAnnotation2 = serializeAnnotation(valueAnnotation, '' + prefix + INDENT),
43
- vser = _serializeAnnotation2[0],
44
- vann = _serializeAnnotation2[1]
45
- result.push('' + prefix + INDENT + kser + ': ' + vser + ',')
46
- if (vann !== undefined) {
47
- result.push(indent(vann, valPrefix))
48
- }
49
- })
50
- return ['{'].concat(result, [prefix + '}']).join('\n')
51
- }
52
- export function serializeValue(value) {
53
- if (typeof value === 'string') {
54
- return serializeString(value)
55
- } else if (typeof value === 'number' || typeof value === 'boolean') {
56
- return value.toString()
57
- } else if (value === null) {
58
- return 'null'
59
- } else if (value === undefined) {
60
- return 'undefined'
61
- } else {
62
- var valueAsDate = asDate(value)
63
- if (valueAsDate !== null) {
64
- return 'new Date(' + JSON.stringify(valueAsDate.toISOString()) + ')'
65
- } else if (value instanceof Date) {
66
- return '(Invalid Date)'
67
- } else {
68
- return '(unserializable)'
69
- }
70
- }
71
- }
72
- export function serializeAnnotation(ann, prefix) {
73
- if (prefix === void 0) {
74
- prefix = ''
75
- }
76
- var serialized
77
- if (ann.type === 'array') {
78
- serialized = serializeArray(ann, prefix)
79
- } else if (ann.type === 'object') {
80
- serialized = serializeObject(ann, prefix)
81
- } else if (ann.type === 'function') {
82
- serialized = '<function>'
83
- } else if (ann.type === 'circular-ref') {
84
- serialized = '<circular ref>'
85
- } else if (ann.type === 'unknown') {
86
- serialized = '???'
87
- } else {
88
- serialized = serializeValue(ann.value)
89
- }
90
- var text = ann.text
91
- if (text !== undefined) {
92
- var sep = '^'.repeat(isMultiline(serialized) ? 1 : serialized.length)
93
- return [serialized, [sep, text].join(isMultiline(text) ? '\n' : ' ')]
94
- } else {
95
- return [serialized, undefined]
96
- }
97
- }
98
- export function formatInline(ann) {
99
- var _serializeAnnotation3 = serializeAnnotation(ann),
100
- serialized = _serializeAnnotation3[0],
101
- annotation = _serializeAnnotation3[1]
102
- if (annotation !== undefined) {
103
- return serialized + '\n' + annotation
104
- } else {
105
- return serialized
106
- }
107
- }
108
- export function formatShort(ann) {
109
- return _summarize(ann, []).join('\n')
110
- }
package/index.js.flow DELETED
@@ -1,44 +0,0 @@
1
- // @flow strict
2
-
3
- export type { Decoder, DecodeResult, DecoderType, Scalar } from './Decoder';
4
- export type { JSONValue, JSONObject, JSONArray } from './lib/json';
5
-
6
- export { define } from './Decoder';
7
-
8
- export {
9
- always,
10
- constant,
11
- hardcoded,
12
- maybe,
13
- mixed,
14
- nullable,
15
- null_,
16
- optional,
17
- undefined_,
18
- unknown,
19
- } from './lib/basics';
20
- export { array, nonEmptyArray, poja, set, tuple } from './lib/arrays';
21
- export { boolean, numericBoolean, truthy } from './lib/booleans';
22
- export { date, iso8601 } from './lib/dates';
23
- export { dict, exact, inexact, mapping, object, pojo } from './lib/objects';
24
- export { either, oneOf, taggedUnion } from './lib/unions';
25
- export {
26
- email,
27
- httpsUrl,
28
- nonEmptyString,
29
- regex,
30
- string,
31
- url,
32
- uuid,
33
- uuidv1,
34
- uuidv4,
35
- } from './lib/strings';
36
- export { fail, instanceOf, lazy, never, prep } from './lib/utilities';
37
- export {
38
- anyNumber,
39
- integer,
40
- number,
41
- positiveInteger,
42
- positiveNumber,
43
- } from './lib/numbers';
44
- export { json, jsonObject, jsonArray } from './lib/json';
package/index.mjs DELETED
@@ -1,11 +0,0 @@
1
- export { define } from './Decoder.mjs'
2
- export { always, constant, hardcoded, maybe, mixed, nullable, null_, optional, undefined_, unknown } from './lib/basics.mjs'
3
- export { array, nonEmptyArray, poja, set, tuple } from './lib/arrays.mjs'
4
- export { boolean, numericBoolean, truthy } from './lib/booleans.mjs'
5
- export { date, iso8601 } from './lib/dates.mjs'
6
- export { dict, exact, inexact, mapping, object, pojo } from './lib/objects.mjs'
7
- export { either, oneOf, taggedUnion } from './lib/unions.mjs'
8
- export { email, httpsUrl, nonEmptyString, regex, string, url, uuid, uuidv1, uuidv4 } from './lib/strings.mjs'
9
- export { fail, instanceOf, lazy, never, prep } from './lib/utilities.mjs'
10
- export { anyNumber, integer, number, positiveInteger, positiveNumber } from './lib/numbers.mjs'
11
- export { json, jsonObject, jsonArray } from './lib/json.mjs'
package/lib/_helpers.d.ts DELETED
@@ -1,79 +0,0 @@
1
- /**
2
- * Given a type like:
3
- *
4
- * {
5
- * a: string;
6
- * b: number | undefined;
7
- * c: null | undefined;
8
- * d: null;
9
- * e: undefined;
10
- * }
11
- *
12
- * Will drop all the "undefined" types. In this case, only "e":
13
- *
14
- * {
15
- * a: string;
16
- * b: number | undefined;
17
- * c: null | undefined;
18
- * d: null;
19
- * }
20
- *
21
- */
22
- type Compact<T> = { [K in IsDefined<T, keyof T>]: T[K] };
23
-
24
- type IsDefined<T, K extends keyof T> = K extends any
25
- ? T[K] extends undefined
26
- ? never
27
- : K
28
- : never;
29
-
30
- //
31
- // HACK:
32
- // These weird conditionals test whether TypeScript is configured with the
33
- // `strictNullChecks` compiler option. We use these definitions to influence
34
- // what's considered a "required" vs an "optional" key for the AllowImplicit
35
- // type.
36
- //
37
- // If strictNullChecks is false, then we should not be emitting any `?` fields
38
- // and consider all fields "required" because everything is optional by default
39
- // in that mode anyway.
40
- //
41
- type NoStrictNullChecks = undefined extends string ? 1 : undefined;
42
- // ^^^^^^^^^^^^^^^^^^^^^^^^
43
- type StrictNullChecks = undefined extends string ? undefined : 1;
44
- // ^^^^^^^^^^^^^^^^^^^^^^^^
45
-
46
- export type RequiredKeys<T> = keyof Compact<{
47
- [K in keyof T]: undefined extends T[K] ? NoStrictNullChecks : 1;
48
- }>;
49
-
50
- export type OptionalKeys<T> = keyof Compact<{
51
- [K in keyof T]: undefined extends T[K] ? 1 : StrictNullChecks;
52
- }>;
53
-
54
- /**
55
- * Transforms an object type, by marking all fields that contain "undefined"
56
- * with a question mark, i.e. allowing implicit-undefineds when
57
- * explicit-undefined are also allowed.
58
- *
59
- * For example, if:
60
- *
61
- * type User = {
62
- * name: string;
63
- * age: number | null | undefined;
64
- * }
65
- *
66
- * Then AllowImplicit<User> will become equivalent to:
67
- *
68
- * {
69
- * name: string;
70
- * age?: number | null;
71
- * ^
72
- * Note the question mark
73
- * }
74
- */
75
- type AllowImplicit<T> = { [K in RequiredKeys<T>]-?: T[K] } & {
76
- [K in OptionalKeys<T>]+?: Exclude<T[K], undefined>;
77
- };
78
-
79
- export { AllowImplicit };
package/lib/arrays.d.ts DELETED
@@ -1,59 +0,0 @@
1
- /// <reference lib="es6" />
2
-
3
- import { Decoder } from '../Decoder';
4
-
5
- /**
6
- * Accepts any array, but doesn't validate its items further.
7
- *
8
- * "poja" means "plain old JavaScript array", a play on `pojo()`.
9
- */
10
- export const poja: Decoder<unknown[]>;
11
-
12
- /**
13
- * Accepts arrays of whatever the given decoder accepts.
14
- */
15
- export function array<T>(decoder: Decoder<T>): Decoder<T[]>;
16
-
17
- /**
18
- * Like `array()`, but will reject arrays with 0 elements.
19
- */
20
- export function nonEmptyArray<T>(decoder: Decoder<T>): Decoder<[T, ...T[]]>;
21
-
22
- /**
23
- * Similar to `array()`, but returns the result as an [ES6
24
- * Set](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Set).
25
- */
26
- export function set<T>(decoder: Decoder<T>): Decoder<Set<T>>;
27
-
28
- /**
29
- * Accepts a tuple (an array with exactly _n_ items) of values accepted by the
30
- * _n_ given decoders.
31
- */
32
- export function tuple<A>(a: Decoder<A>): Decoder<[A]>;
33
- export function tuple<A, B>(a: Decoder<A>, b: Decoder<B>): Decoder<[A, B]>;
34
- export function tuple<A, B, C>(
35
- a: Decoder<A>,
36
- b: Decoder<B>,
37
- c: Decoder<C>,
38
- ): Decoder<[A, B, C]>;
39
- export function tuple<A, B, C, D>(
40
- a: Decoder<A>,
41
- b: Decoder<B>,
42
- c: Decoder<C>,
43
- d: Decoder<D>,
44
- ): Decoder<[A, B, C, D]>;
45
- export function tuple<A, B, C, D, E>(
46
- a: Decoder<A>,
47
- b: Decoder<B>,
48
- c: Decoder<C>,
49
- d: Decoder<D>,
50
- e: Decoder<E>,
51
- ): Decoder<[A, B, C, D, E]>;
52
- export function tuple<A, B, C, D, E, F>(
53
- a: Decoder<A>,
54
- b: Decoder<B>,
55
- c: Decoder<C>,
56
- d: Decoder<D>,
57
- e: Decoder<E>,
58
- f: Decoder<F>,
59
- ): Decoder<[A, B, C, D, E, F]>;
package/lib/arrays.js DELETED
@@ -1,85 +0,0 @@
1
- 'use strict'
2
-
3
- exports.__esModule = true
4
- exports.array = array
5
- exports.nonEmptyArray = nonEmptyArray
6
- exports.poja = void 0
7
- exports.set = set
8
- exports.tuple = void 0
9
- var _annotate = require('../annotate')
10
- var _Decoder = require('../Decoder')
11
-
12
- var poja = (0, _Decoder.define)(function (blob, ok, err) {
13
- if (!Array.isArray(blob)) {
14
- return err('Must be an array')
15
- }
16
- return ok(blob.slice())
17
- })
18
-
19
- exports.poja = poja
20
- function all(items, blobs, ok, err) {
21
- var results = []
22
- for (var index = 0; index < items.length; ++index) {
23
- var result = items[index]
24
- if (result.ok) {
25
- results.push(result.value)
26
- } else {
27
- var ann = result.error
28
- var clone = [].concat(blobs)
29
- clone.splice(index, 1, (0, _annotate.annotate)(ann, ann.text ? ann.text + ' (at index ' + index + ')' : 'index ' + index))
30
- return err((0, _annotate.annotate)(clone))
31
- }
32
- }
33
- return ok(results)
34
- }
35
-
36
- function array(decoder) {
37
- return poja.then(function (blobs, ok, err) {
38
- var results = blobs.map(decoder.decode)
39
- return all(results, blobs, ok, err)
40
- })
41
- }
42
-
43
- function nonEmptyArray(decoder) {
44
- return array(decoder).refine(function (arr) {
45
- return arr.length > 0
46
- }, 'Must be non-empty array')
47
- }
48
-
49
- function set(decoder) {
50
- return array(decoder).transform(function (items) {
51
- return new Set(items)
52
- })
53
- }
54
- var ntuple = function ntuple(n) {
55
- return poja.refine(function (arr) {
56
- return arr.length === n
57
- }, 'Must be a ' + n + '-tuple')
58
- }
59
-
60
- function _tuple() {
61
- for (var _len = arguments.length, decoders = new Array(_len), _key = 0; _key < _len; _key++) {
62
- decoders[_key] = arguments[_key]
63
- }
64
- return ntuple(decoders.length).then(function (blobs, ok, err) {
65
- var allOk = true
66
- var rvs = decoders.map(function (decoder, i) {
67
- var blob = blobs[i]
68
- var result = decoder.decode(blob)
69
- if (result.ok) {
70
- return result.value
71
- } else {
72
- allOk = false
73
- return result.error
74
- }
75
- })
76
- if (allOk) {
77
- return ok(rvs)
78
- } else {
79
- return err((0, _annotate.annotate)(rvs))
80
- }
81
- })
82
- }
83
-
84
- var tuple = _tuple
85
- exports.tuple = tuple