decoders 2.0.0-beta5 → 2.0.0-beta9

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 (139) hide show
  1. package/CHANGELOG.md +19 -0
  2. package/NotSupportedTSVersion.d.ts +1 -0
  3. package/README.md +934 -387
  4. package/_guard.d.ts +7 -0
  5. package/_guard.js +2 -6
  6. package/_guard.js.flow +3 -3
  7. package/{_esm/_guard.js → _guard.mjs} +3 -3
  8. package/_types.d.ts +13 -0
  9. package/{_esm/_types.js → _types.mjs} +0 -0
  10. package/_utils.d.ts +10 -0
  11. package/_utils.js +1 -1
  12. package/_utils.js.flow +3 -3
  13. package/{_esm/_utils.js → _utils.mjs} +1 -1
  14. package/annotate.d.ts +62 -0
  15. package/{_esm/annotate.js → annotate.mjs} +0 -0
  16. package/core/_helpers.d.ts +79 -0
  17. package/core/array.d.ts +8 -0
  18. package/core/array.js +19 -12
  19. package/core/array.js.flow +15 -11
  20. package/{_esm/core/array.js → core/array.mjs} +19 -10
  21. package/core/boolean.d.ts +5 -0
  22. package/core/boolean.js +5 -9
  23. package/core/boolean.js.flow +5 -7
  24. package/{_esm/core/boolean.js → core/boolean.mjs} +7 -7
  25. package/core/composition.d.ts +18 -0
  26. package/core/composition.js +41 -15
  27. package/core/composition.js.flow +41 -10
  28. package/core/composition.mjs +70 -0
  29. package/core/constants.d.ts +11 -0
  30. package/core/constants.js +6 -10
  31. package/core/constants.js.flow +7 -9
  32. package/{_esm/core/constants.js → core/constants.mjs} +7 -7
  33. package/core/date.d.ts +4 -0
  34. package/core/date.js +5 -9
  35. package/core/date.js.flow +4 -6
  36. package/{_esm/core/date.js → core/date.mjs} +7 -7
  37. package/core/describe.d.ts +3 -0
  38. package/core/describe.js +2 -6
  39. package/core/describe.js.flow +2 -2
  40. package/{_esm/core/describe.js → core/describe.mjs} +3 -3
  41. package/core/dispatch.d.ts +8 -0
  42. package/core/dispatch.js +11 -13
  43. package/core/dispatch.js.flow +13 -12
  44. package/{_esm/core/dispatch.js → core/dispatch.mjs} +12 -11
  45. package/core/either.d.ts +66 -0
  46. package/core/either.js +34 -50
  47. package/core/either.js.flow +40 -86
  48. package/core/either.mjs +90 -0
  49. package/core/fail.d.ts +3 -0
  50. package/core/fail.js +2 -6
  51. package/core/fail.js.flow +2 -2
  52. package/{_esm/core/fail.js → core/fail.mjs} +3 -3
  53. package/core/instanceOf.d.ts +3 -0
  54. package/core/instanceOf.js +2 -6
  55. package/core/instanceOf.js.flow +3 -3
  56. package/core/instanceOf.mjs +8 -0
  57. package/core/json.d.ts +11 -0
  58. package/core/json.js +3 -3
  59. package/core/json.js.flow +3 -3
  60. package/core/json.mjs +15 -0
  61. package/core/lazy.d.ts +3 -0
  62. package/{_esm/core/lazy.js → core/lazy.mjs} +0 -0
  63. package/core/number.d.ts +6 -0
  64. package/core/number.js +9 -13
  65. package/core/number.js.flow +18 -12
  66. package/core/number.mjs +25 -0
  67. package/core/object.d.ts +38 -0
  68. package/core/object.js +66 -13
  69. package/core/object.js.flow +84 -28
  70. package/{_esm/core/object.js → core/object.mjs} +64 -11
  71. package/core/optional.d.ts +5 -0
  72. package/core/optional.js +4 -8
  73. package/core/optional.js.flow +3 -3
  74. package/{_esm/core/optional.js → core/optional.mjs} +6 -6
  75. package/core/string.d.ts +13 -0
  76. package/core/string.js +31 -49
  77. package/core/string.js.flow +29 -39
  78. package/core/string.mjs +58 -0
  79. package/core/tuple.d.ts +30 -0
  80. package/core/tuple.js +30 -149
  81. package/core/tuple.js.flow +33 -197
  82. package/core/tuple.mjs +45 -0
  83. package/format.d.ts +4 -0
  84. package/{format/inline.js → format.js} +6 -1
  85. package/{_esm/format/inline.js.flow → format.js.flow} +6 -2
  86. package/{_esm/format/inline.js → format.mjs} +4 -1
  87. package/index.d.ts +42 -0
  88. package/index.js +33 -42
  89. package/index.js.flow +17 -18
  90. package/{_esm/index.js → index.mjs} +18 -19
  91. package/package.json +15 -3
  92. package/result.d.ts +39 -0
  93. package/result.js +9 -90
  94. package/result.js.flow +11 -87
  95. package/result.mjs +81 -0
  96. package/_esm/_guard.js.flow +0 -20
  97. package/_esm/_types.js.flow +0 -20
  98. package/_esm/_utils.js.flow +0 -97
  99. package/_esm/annotate.js.flow +0 -218
  100. package/_esm/core/array.js.flow +0 -103
  101. package/_esm/core/boolean.js.flow +0 -29
  102. package/_esm/core/composition.js +0 -42
  103. package/_esm/core/composition.js.flow +0 -43
  104. package/_esm/core/constants.js.flow +0 -46
  105. package/_esm/core/date.js.flow +0 -40
  106. package/_esm/core/describe.js.flow +0 -17
  107. package/_esm/core/dispatch.js.flow +0 -58
  108. package/_esm/core/either.js +0 -90
  109. package/_esm/core/either.js.flow +0 -151
  110. package/_esm/core/fail.js.flow +0 -12
  111. package/_esm/core/instanceOf.js +0 -8
  112. package/_esm/core/instanceOf.js.flow +0 -20
  113. package/_esm/core/json.js +0 -15
  114. package/_esm/core/json.js.flow +0 -28
  115. package/_esm/core/lazy.js.flow +0 -15
  116. package/_esm/core/mapping.js +0 -54
  117. package/_esm/core/mapping.js.flow +0 -54
  118. package/_esm/core/number.js +0 -25
  119. package/_esm/core/number.js.flow +0 -34
  120. package/_esm/core/object.js.flow +0 -203
  121. package/_esm/core/optional.js.flow +0 -41
  122. package/_esm/core/string.js +0 -76
  123. package/_esm/core/string.js.flow +0 -82
  124. package/_esm/core/tuple.js +0 -155
  125. package/_esm/core/tuple.js.flow +0 -215
  126. package/_esm/format/index.js +0 -2
  127. package/_esm/format/index.js.flow +0 -4
  128. package/_esm/format/short.js +0 -4
  129. package/_esm/format/short.js.flow +0 -8
  130. package/_esm/index.js.flow +0 -63
  131. package/_esm/result.js +0 -148
  132. package/_esm/result.js.flow +0 -174
  133. package/core/mapping.js +0 -67
  134. package/core/mapping.js.flow +0 -54
  135. package/format/index.js +0 -12
  136. package/format/index.js.flow +0 -4
  137. package/format/inline.js.flow +0 -122
  138. package/format/short.js +0 -10
  139. package/format/short.js.flow +0 -8
@@ -1,54 +0,0 @@
1
- // @flow strict
2
-
3
- import * as Result from '../result';
4
- import { annotateObject } from '../annotate';
5
- import { compose, map } from './composition';
6
- import { merge } from '../annotate';
7
- import { pojo } from './object';
8
- import type { Annotation } from '../annotate';
9
- import type { Decoder } from '../_types';
10
-
11
- /**
12
- * Given an object, will decode a Map of string keys to whatever values.
13
- *
14
- * For example, given a decoder for a Person, we can verify a Person lookup
15
- * table structure (of type Map<string, Person>) like so:
16
- *
17
- * mapping(person)
18
- *
19
- */
20
- export function mapping<T>(decoder: Decoder<T>): Decoder<Map<string, T>> {
21
- return compose(pojo, (blob: { +[key: string]: mixed }) => {
22
- let tuples: Array<[string, T]> = [];
23
- let errors: { [key: string]: Annotation } | null = null;
24
-
25
- Object.keys(blob).forEach((key: string) => {
26
- const value = blob[key];
27
- const result = decoder(value);
28
- if (result.type === 'ok') {
29
- if (errors === null) {
30
- tuples.push([key, result.value]);
31
- }
32
- } else {
33
- tuples.length = 0; // Clear the tuples array
34
- if (errors === null) {
35
- errors = {};
36
- }
37
- errors[key] = result.error;
38
- }
39
- });
40
-
41
- if (errors !== null) {
42
- return Result.err(merge(annotateObject(blob), errors));
43
- } else {
44
- return Result.ok(new Map(tuples));
45
- }
46
- });
47
- }
48
-
49
- /**
50
- * Like mapping(), but returns an object rather than a Map instance.
51
- */
52
- export function dict<T>(decoder: Decoder<T>): Decoder<{ [string]: T }> {
53
- return map(mapping(decoder), (m) => Object.fromEntries(m));
54
- }
package/format/index.js DELETED
@@ -1,12 +0,0 @@
1
- "use strict";
2
-
3
- exports.__esModule = true;
4
- exports.formatShort = exports.formatInline = void 0;
5
-
6
- var _inline = require("./inline");
7
-
8
- exports.formatInline = _inline.formatInline;
9
-
10
- var _short = require("./short");
11
-
12
- exports.formatShort = _short.formatShort;
@@ -1,4 +0,0 @@
1
- // @flow strict
2
-
3
- export { formatInline } from './inline';
4
- export { formatShort } from './short';
@@ -1,122 +0,0 @@
1
- // @flow strict
2
-
3
- import { asDate, indent, INDENT, isMultiline } from '../_utils';
4
- import type { Annotation, ArrayAnnotation, ObjectAnnotation } from '../annotate';
5
-
6
- function serializeString(s: string, width: number = 80): string {
7
- // Full string
8
- // Abbreviated to $maxlen i.e. "Vincent Driess..." [truncated]
9
- let ser = JSON.stringify(s);
10
- if (ser.length <= width) {
11
- return ser;
12
- }
13
-
14
- // Cut off a bit
15
- const truncated = s.substring(0, width - 15) + '...';
16
- ser = JSON.stringify(truncated) + ' [truncated]';
17
- return ser;
18
- }
19
-
20
- function serializeArray(annotation: ArrayAnnotation, prefix: string): string {
21
- const { items } = annotation;
22
- if (items.length === 0) {
23
- return '[]';
24
- }
25
-
26
- const result = [];
27
- items.forEach((item) => {
28
- const [ser, ann] = serializeAnnotation(item, prefix + INDENT);
29
- result.push(prefix + INDENT + ser + ',');
30
- if (ann !== undefined) {
31
- result.push(indent(ann, prefix + INDENT));
32
- }
33
- });
34
- return ['[', ...result, prefix + ']'].join('\n');
35
- }
36
-
37
- function serializeObject(annotation: ObjectAnnotation, prefix: string): string {
38
- const { fields } = annotation;
39
-
40
- const fieldNames = Object.keys(fields);
41
- if (fieldNames.length === 0) {
42
- return '{}';
43
- }
44
-
45
- const result = [];
46
- fieldNames.forEach((key) => {
47
- const valueAnnotation = fields[key];
48
- const kser = serializeValue(key);
49
-
50
- const valPrefix = prefix + INDENT + ' '.repeat(kser.length + 2);
51
- const [vser, vann] = serializeAnnotation(valueAnnotation, prefix + INDENT);
52
-
53
- result.push(prefix + INDENT + kser + ': ' + vser + ',');
54
- if (vann !== undefined) {
55
- result.push(indent(vann, valPrefix));
56
- }
57
- });
58
- return ['{', ...result, prefix + '}'].join('\n');
59
- }
60
-
61
- export function serializeValue(value: mixed): string {
62
- // istanbul ignore else
63
- if (typeof value === 'string') {
64
- return serializeString(value);
65
- } else if (typeof value === 'number' || typeof value === 'boolean') {
66
- return value.toString();
67
- } else if (value === null) {
68
- return 'null';
69
- } else if (value === undefined) {
70
- return 'undefined';
71
- } else {
72
- const valueAsDate = asDate(value);
73
- if (valueAsDate !== null) {
74
- return `new Date(${JSON.stringify(valueAsDate.toISOString())})`;
75
- } else if (value instanceof Date) {
76
- // NOTE: Using `instanceof Date` is unreliable way of checking dates.
77
- // If this case occurs (and it didn't pass the prior isDate())
78
- // check, then this must be the case where it's an invalid date.
79
- return '(Invalid Date)';
80
- } else {
81
- return '(unserializable)';
82
- }
83
- }
84
- }
85
-
86
- export function serializeAnnotation(
87
- ann: Annotation,
88
- prefix: string = '',
89
- ): [string, string | void] {
90
- // The serialized data (the input object echoed back)
91
- let serialized;
92
- if (ann.type === 'array') {
93
- serialized = serializeArray(ann, prefix);
94
- } else if (ann.type === 'object') {
95
- serialized = serializeObject(ann, prefix);
96
- } else if (ann.type === 'function') {
97
- serialized = '<function>';
98
- } else if (ann.type === 'circular-ref') {
99
- serialized = '<circular ref>';
100
- } else if (ann.type === 'unknown') {
101
- serialized = '???';
102
- } else {
103
- serialized = serializeValue(ann.value);
104
- }
105
-
106
- const text = ann.text;
107
- if (text !== undefined) {
108
- const sep = '^'.repeat(isMultiline(serialized) ? 1 : serialized.length);
109
- return [serialized, [sep, text].join(isMultiline(text) ? '\n' : ' ')];
110
- } else {
111
- return [serialized, undefined];
112
- }
113
- }
114
-
115
- export function formatInline(ann: Annotation): string {
116
- const [serialized, annotation] = serializeAnnotation(ann);
117
- if (annotation !== undefined) {
118
- return serialized + '\n' + annotation;
119
- } else {
120
- return serialized;
121
- }
122
- }
package/format/short.js DELETED
@@ -1,10 +0,0 @@
1
- "use strict";
2
-
3
- exports.__esModule = true;
4
- exports.formatShort = formatShort;
5
-
6
- var _utils = require("../_utils");
7
-
8
- function formatShort(ann) {
9
- return (0, _utils.summarize)(ann, []).join('\n');
10
- }
@@ -1,8 +0,0 @@
1
- // @flow strict
2
-
3
- import { summarize as _summarize } from '../_utils';
4
- import type { Annotation } from '../annotate';
5
-
6
- export function formatShort(ann: Annotation): string {
7
- return _summarize(ann, []).join('\n');
8
- }