decoders 1.25.4 → 2.0.0-beta1

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 (132) hide show
  1. package/CHANGELOG.md +30 -3
  2. package/cjs/_guard.js +26 -0
  3. package/cjs/_guard.js.flow +20 -0
  4. package/cjs/_types.js +1 -0
  5. package/cjs/_types.js.flow +20 -0
  6. package/cjs/_utils.js +108 -0
  7. package/cjs/_utils.js.flow +97 -0
  8. package/cjs/annotate.js +161 -0
  9. package/cjs/annotate.js.flow +218 -0
  10. package/cjs/format/index.js +12 -0
  11. package/cjs/format/index.js.flow +4 -0
  12. package/cjs/format/inline.js +146 -0
  13. package/cjs/format/inline.js.flow +122 -0
  14. package/cjs/format/short.js +10 -0
  15. package/cjs/format/short.js.flow +8 -0
  16. package/cjs/index.js +120 -0
  17. package/{index.js.flow → cjs/index.js.flow} +31 -24
  18. package/cjs/result.js +172 -0
  19. package/cjs/result.js.flow +166 -0
  20. package/cjs/stdlib/array.js +108 -0
  21. package/{array.js.flow → cjs/stdlib/array.js.flow} +22 -25
  22. package/cjs/stdlib/boolean.js +44 -0
  23. package/{boolean.js.flow → cjs/stdlib/boolean.js.flow} +8 -7
  24. package/cjs/stdlib/composition.js +56 -0
  25. package/{utils.js.flow → cjs/stdlib/composition.js.flow} +7 -22
  26. package/cjs/stdlib/constants.js +69 -0
  27. package/{constants.js.flow → cjs/stdlib/constants.js.flow} +13 -12
  28. package/cjs/stdlib/date.js +46 -0
  29. package/{date.js.flow → cjs/stdlib/date.js.flow} +9 -7
  30. package/cjs/stdlib/describe.js +26 -0
  31. package/{describe.js.flow → cjs/stdlib/describe.js.flow} +4 -4
  32. package/cjs/stdlib/dispatch.js +62 -0
  33. package/{dispatch.js.flow → cjs/stdlib/dispatch.js.flow} +6 -5
  34. package/cjs/stdlib/either.js +117 -0
  35. package/{either.js.flow → cjs/stdlib/either.js.flow} +55 -35
  36. package/cjs/stdlib/fail.js +21 -0
  37. package/cjs/stdlib/fail.js.flow +12 -0
  38. package/cjs/stdlib/instanceOf.js +19 -0
  39. package/{instanceOf.js.flow → cjs/stdlib/instanceOf.js.flow} +7 -8
  40. package/{json.js → cjs/stdlib/json.js} +3 -5
  41. package/{json.js.flow → cjs/stdlib/json.js.flow} +4 -4
  42. package/{lazy.js → cjs/stdlib/lazy.js} +1 -3
  43. package/{lazy.js.flow → cjs/stdlib/lazy.js.flow} +1 -1
  44. package/cjs/stdlib/mapping.js +67 -0
  45. package/cjs/stdlib/mapping.js.flow +54 -0
  46. package/cjs/stdlib/number.js +40 -0
  47. package/{number.js.flow → cjs/stdlib/number.js.flow} +10 -11
  48. package/cjs/stdlib/object.js +194 -0
  49. package/{object.js.flow → cjs/stdlib/object.js.flow} +44 -52
  50. package/cjs/stdlib/optional.js +54 -0
  51. package/{optional.js.flow → cjs/stdlib/optional.js.flow} +6 -7
  52. package/cjs/stdlib/string.js +98 -0
  53. package/{string.js.flow → cjs/stdlib/string.js.flow} +13 -12
  54. package/cjs/stdlib/tuple.js +173 -0
  55. package/{tuple.js.flow → cjs/stdlib/tuple.js.flow} +62 -63
  56. package/es/_guard.js +15 -0
  57. package/es/_types.js +0 -0
  58. package/es/_utils.js +93 -0
  59. package/es/annotate.js +144 -0
  60. package/es/format/index.js +2 -0
  61. package/es/format/inline.js +137 -0
  62. package/es/format/short.js +4 -0
  63. package/es/index.js +37 -0
  64. package/es/result.js +139 -0
  65. package/es/stdlib/array.js +91 -0
  66. package/es/stdlib/boolean.js +28 -0
  67. package/es/stdlib/composition.js +42 -0
  68. package/es/stdlib/constants.js +46 -0
  69. package/es/stdlib/date.js +28 -0
  70. package/{describe.js → es/stdlib/describe.js} +5 -11
  71. package/{dispatch.js → es/stdlib/dispatch.js} +8 -15
  72. package/es/stdlib/either.js +90 -0
  73. package/es/stdlib/fail.js +11 -0
  74. package/es/stdlib/instanceOf.js +8 -0
  75. package/es/stdlib/json.js +15 -0
  76. package/es/stdlib/lazy.js +11 -0
  77. package/es/stdlib/mapping.js +54 -0
  78. package/es/stdlib/number.js +25 -0
  79. package/es/stdlib/object.js +175 -0
  80. package/es/stdlib/optional.js +38 -0
  81. package/{string.js → es/stdlib/string.js} +18 -35
  82. package/es/stdlib/tuple.js +155 -0
  83. package/package.json +12 -7
  84. package/ts/_guard.d.ts +7 -0
  85. package/ts/_helpers.d.ts +79 -0
  86. package/{types.d.ts → ts/_types.d.ts} +3 -5
  87. package/{utils.d.ts → ts/_utils.d.ts} +3 -3
  88. package/ts/annotate.d.ts +58 -0
  89. package/{array.d.ts → ts/array.d.ts} +1 -1
  90. package/{boolean.d.ts → ts/boolean.d.ts} +1 -1
  91. package/{constants.d.ts → ts/constants.d.ts} +1 -1
  92. package/{date.d.ts → ts/date.d.ts} +1 -1
  93. package/{describe.d.ts → ts/describe.d.ts} +1 -1
  94. package/{dispatch.d.ts → ts/dispatch.d.ts} +3 -3
  95. package/{either.d.ts → ts/either.d.ts} +8 -8
  96. package/{fail.d.ts → ts/fail.d.ts} +1 -1
  97. package/ts/index.d.ts +42 -0
  98. package/ts/inline.d.ts +3 -0
  99. package/{instanceOf.d.ts → ts/instanceOf.d.ts} +1 -1
  100. package/{json.d.ts → ts/json.d.ts} +1 -1
  101. package/{lazy.d.ts → ts/lazy.d.ts} +1 -1
  102. package/{mapping.d.ts → ts/mapping.d.ts} +1 -1
  103. package/{number.d.ts → ts/number.d.ts} +1 -1
  104. package/{object.d.ts → ts/object.d.ts} +8 -8
  105. package/{optional.d.ts → ts/optional.d.ts} +1 -1
  106. package/ts/result.d.ts +39 -0
  107. package/ts/short.d.ts +3 -0
  108. package/{string.d.ts → ts/string.d.ts} +1 -1
  109. package/{tuple.d.ts → ts/tuple.d.ts} +5 -5
  110. package/array.js +0 -133
  111. package/boolean.js +0 -42
  112. package/constants.js +0 -67
  113. package/date.js +0 -42
  114. package/either.js +0 -85
  115. package/fail.js +0 -19
  116. package/fail.js.flow +0 -13
  117. package/guard.d.ts +0 -7
  118. package/guard.js +0 -30
  119. package/guard.js.flow +0 -36
  120. package/helpers.d.ts +0 -62
  121. package/index.d.ts +0 -38
  122. package/index.js +0 -397
  123. package/instanceOf.js +0 -17
  124. package/mapping.js +0 -113
  125. package/mapping.js.flow +0 -71
  126. package/number.js +0 -38
  127. package/object.js +0 -254
  128. package/optional.js +0 -52
  129. package/tuple.js +0 -199
  130. package/types.js +0 -5
  131. package/types.js.flow +0 -26
  132. package/utils.js +0 -70
@@ -0,0 +1,12 @@
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;
@@ -0,0 +1,4 @@
1
+ // @flow strict
2
+
3
+ export { formatInline } from './inline';
4
+ export { formatShort } from './short';
@@ -0,0 +1,146 @@
1
+ "use strict";
2
+
3
+ exports.__esModule = true;
4
+ exports.formatInline = formatInline;
5
+ exports.serializeAnnotation = serializeAnnotation;
6
+ exports.serializeValue = serializeValue;
7
+
8
+ var _utils = require("../_utils");
9
+
10
+ function serializeString(s, width) {
11
+ if (width === void 0) {
12
+ width = 80;
13
+ }
14
+
15
+ // Full string
16
+ // Abbreviated to $maxlen i.e. "Vincent Driess..." [truncated]
17
+ var ser = JSON.stringify(s);
18
+
19
+ if (ser.length <= width) {
20
+ return ser;
21
+ } // Cut off a bit
22
+
23
+
24
+ var truncated = s.substring(0, width - 15) + '...';
25
+ ser = JSON.stringify(truncated) + ' [truncated]';
26
+ return ser;
27
+ }
28
+
29
+ function serializeArray(annotation, prefix) {
30
+ var items = annotation.items;
31
+
32
+ if (items.length === 0) {
33
+ return '[]';
34
+ }
35
+
36
+ var result = [];
37
+ items.forEach(function (item) {
38
+ var _serializeAnnotation = serializeAnnotation(item, prefix + _utils.INDENT),
39
+ ser = _serializeAnnotation[0],
40
+ ann = _serializeAnnotation[1];
41
+
42
+ result.push(prefix + _utils.INDENT + ser + ',');
43
+
44
+ if (ann !== undefined) {
45
+ result.push((0, _utils.indent)(ann, prefix + _utils.INDENT));
46
+ }
47
+ });
48
+ return ['['].concat(result, [prefix + ']']).join('\n');
49
+ }
50
+
51
+ function serializeObject(annotation, prefix) {
52
+ var fields = annotation.fields;
53
+ var fieldNames = Object.keys(fields);
54
+
55
+ if (fieldNames.length === 0) {
56
+ return '{}';
57
+ }
58
+
59
+ var result = [];
60
+ fieldNames.forEach(function (key) {
61
+ var valueAnnotation = fields[key];
62
+ var kser = serializeValue(key);
63
+ var valPrefix = prefix + _utils.INDENT + ' '.repeat(kser.length + 2);
64
+
65
+ var _serializeAnnotation2 = serializeAnnotation(valueAnnotation, prefix + _utils.INDENT),
66
+ vser = _serializeAnnotation2[0],
67
+ vann = _serializeAnnotation2[1];
68
+
69
+ result.push(prefix + _utils.INDENT + kser + ': ' + vser + ',');
70
+
71
+ if (vann !== undefined) {
72
+ result.push((0, _utils.indent)(vann, valPrefix));
73
+ }
74
+ });
75
+ return ['{'].concat(result, [prefix + '}']).join('\n');
76
+ }
77
+
78
+ function serializeValue(value) {
79
+ // istanbul ignore else
80
+ if (typeof value === 'string') {
81
+ return serializeString(value);
82
+ } else if (typeof value === 'number' || typeof value === 'boolean') {
83
+ return value.toString();
84
+ } else if (value === null) {
85
+ return 'null';
86
+ } else if (value === undefined) {
87
+ return 'undefined';
88
+ } else {
89
+ var valueAsDate = (0, _utils.asDate)(value);
90
+
91
+ if (valueAsDate !== null) {
92
+ return "new Date(" + JSON.stringify(valueAsDate.toISOString()) + ")";
93
+ } else if (value instanceof Date) {
94
+ // NOTE: Using `instanceof Date` is unreliable way of checking dates.
95
+ // If this case occurs (and it didn't pass the prior isDate())
96
+ // check, then this must be the case where it's an invalid date.
97
+ return '(Invalid Date)';
98
+ } else {
99
+ return '(unserializable)';
100
+ }
101
+ }
102
+ }
103
+
104
+ function serializeAnnotation(ann, prefix) {
105
+ if (prefix === void 0) {
106
+ prefix = '';
107
+ }
108
+
109
+ // The serialized data (the input object echoed back)
110
+ var serialized;
111
+
112
+ if (ann.type === 'array') {
113
+ serialized = serializeArray(ann, prefix);
114
+ } else if (ann.type === 'object') {
115
+ serialized = serializeObject(ann, prefix);
116
+ } else if (ann.type === 'function') {
117
+ serialized = '<function>';
118
+ } else if (ann.type === 'circular-ref') {
119
+ serialized = '<circular ref>';
120
+ } else if (ann.type === 'unknown') {
121
+ serialized = '???';
122
+ } else {
123
+ serialized = serializeValue(ann.value);
124
+ }
125
+
126
+ var text = ann.text;
127
+
128
+ if (text !== undefined) {
129
+ var sep = '^'.repeat((0, _utils.isMultiline)(serialized) ? 1 : serialized.length);
130
+ return [serialized, [sep, text].join((0, _utils.isMultiline)(text) ? '\n' : ' ')];
131
+ } else {
132
+ return [serialized, undefined];
133
+ }
134
+ }
135
+
136
+ function formatInline(ann) {
137
+ var _serializeAnnotation3 = serializeAnnotation(ann),
138
+ serialized = _serializeAnnotation3[0],
139
+ annotation = _serializeAnnotation3[1];
140
+
141
+ if (annotation !== undefined) {
142
+ return serialized + '\n' + annotation;
143
+ } else {
144
+ return serialized;
145
+ }
146
+ }
@@ -0,0 +1,122 @@
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
+ }
@@ -0,0 +1,10 @@
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
+ }
@@ -0,0 +1,8 @@
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
+ }
package/cjs/index.js ADDED
@@ -0,0 +1,120 @@
1
+ "use strict";
2
+
3
+ exports.__esModule = true;
4
+ exports.url = exports.unknown = exports.undefined_ = exports.tuple6 = exports.tuple5 = exports.tuple4 = exports.tuple3 = exports.tuple2 = exports.tuple1 = exports.truthy = exports.string = exports.regex = exports.predicate = exports.positiveNumber = exports.positiveInteger = exports.pojo = exports.poja = exports.optional = exports.oneOf = exports.object = exports.numericBoolean = exports.number = exports.nullable = exports.null_ = exports.nonEmptyString = exports.nonEmptyArray = exports.mixed = exports.maybe = exports.mapping = exports.map = exports.lazy = exports.jsonObject = exports.jsonArray = exports.json = exports.iso8601 = exports.integer = exports.instanceOf = exports.inexact = exports.hardcoded = exports.guard = exports.fail = exports.exact = exports.email = exports.either9 = exports.either8 = exports.either7 = exports.either6 = exports.either5 = exports.either4 = exports.either3 = exports.either = exports.dispatch = exports.dict = exports.describe = exports.date = exports.constant = exports.compose = exports["boolean"] = exports.array = void 0;
5
+
6
+ var _guard = require("./_guard");
7
+
8
+ exports.guard = _guard.guard;
9
+
10
+ var _composition = require("./stdlib/composition");
11
+
12
+ exports.compose = _composition.compose;
13
+ exports.map = _composition.map;
14
+ exports.predicate = _composition.predicate;
15
+
16
+ var _array = require("./stdlib/array");
17
+
18
+ exports.array = _array.array;
19
+ exports.nonEmptyArray = _array.nonEmptyArray;
20
+ exports.poja = _array.poja;
21
+
22
+ var _boolean = require("./stdlib/boolean");
23
+
24
+ exports["boolean"] = _boolean["boolean"];
25
+ exports.numericBoolean = _boolean.numericBoolean;
26
+ exports.truthy = _boolean.truthy;
27
+
28
+ var _constants = require("./stdlib/constants");
29
+
30
+ exports.constant = _constants.constant;
31
+ exports.hardcoded = _constants.hardcoded;
32
+ exports.mixed = _constants.mixed;
33
+ exports.null_ = _constants.null_;
34
+ exports.undefined_ = _constants.undefined_;
35
+ exports.unknown = _constants.unknown;
36
+
37
+ var _date = require("./stdlib/date");
38
+
39
+ exports.date = _date.date;
40
+ exports.iso8601 = _date.iso8601;
41
+
42
+ var _describe = require("./stdlib/describe");
43
+
44
+ exports.describe = _describe.describe;
45
+
46
+ var _dispatch = require("./stdlib/dispatch");
47
+
48
+ exports.dispatch = _dispatch.dispatch;
49
+
50
+ var _either = require("./stdlib/either");
51
+
52
+ exports.either = _either.either;
53
+ exports.either3 = _either.either3;
54
+ exports.either4 = _either.either4;
55
+ exports.either5 = _either.either5;
56
+ exports.either6 = _either.either6;
57
+ exports.either7 = _either.either7;
58
+ exports.either8 = _either.either8;
59
+ exports.either9 = _either.either9;
60
+ exports.oneOf = _either.oneOf;
61
+
62
+ var _fail = require("./stdlib/fail");
63
+
64
+ exports.fail = _fail.fail;
65
+
66
+ var _instanceOf = require("./stdlib/instanceOf");
67
+
68
+ exports.instanceOf = _instanceOf.instanceOf;
69
+
70
+ var _json = require("./stdlib/json");
71
+
72
+ exports.json = _json.json;
73
+ exports.jsonObject = _json.jsonObject;
74
+ exports.jsonArray = _json.jsonArray;
75
+
76
+ var _lazy = require("./stdlib/lazy");
77
+
78
+ exports.lazy = _lazy.lazy;
79
+
80
+ var _mapping = require("./stdlib/mapping");
81
+
82
+ exports.mapping = _mapping.mapping;
83
+ exports.dict = _mapping.dict;
84
+
85
+ var _number = require("./stdlib/number");
86
+
87
+ exports.integer = _number.integer;
88
+ exports.number = _number.number;
89
+ exports.positiveInteger = _number.positiveInteger;
90
+ exports.positiveNumber = _number.positiveNumber;
91
+
92
+ var _object = require("./stdlib/object");
93
+
94
+ exports.exact = _object.exact;
95
+ exports.inexact = _object.inexact;
96
+ exports.object = _object.object;
97
+ exports.pojo = _object.pojo;
98
+
99
+ var _optional = require("./stdlib/optional");
100
+
101
+ exports.maybe = _optional.maybe;
102
+ exports.nullable = _optional.nullable;
103
+ exports.optional = _optional.optional;
104
+
105
+ var _string = require("./stdlib/string");
106
+
107
+ exports.email = _string.email;
108
+ exports.nonEmptyString = _string.nonEmptyString;
109
+ exports.regex = _string.regex;
110
+ exports.string = _string.string;
111
+ exports.url = _string.url;
112
+
113
+ var _tuple = require("./stdlib/tuple");
114
+
115
+ exports.tuple1 = _tuple.tuple1;
116
+ exports.tuple2 = _tuple.tuple2;
117
+ exports.tuple3 = _tuple.tuple3;
118
+ exports.tuple4 = _tuple.tuple4;
119
+ exports.tuple5 = _tuple.tuple5;
120
+ exports.tuple6 = _tuple.tuple6;
@@ -18,17 +18,28 @@
18
18
  * type. In our case, it's fine to fail with a runtime error.
19
19
  *
20
20
  */
21
- import type { $DecoderType, $GuardType, Decoder, Guard } from './types';
21
+ export type { Decoder, Guard } from './_types';
22
+ export type { DecoderType, GuardType } from './_types';
22
23
 
23
- export { guard } from './guard';
24
- export { compose, map, predicate } from './utils';
24
+ export type { JSONValue, JSONObject, JSONArray } from './stdlib/json';
25
25
 
26
- export { array, nonEmptyArray, poja } from './array';
27
- export { boolean, numericBoolean, truthy } from './boolean';
28
- export { constant, hardcoded, mixed, null_, undefined_, unknown } from './constants';
29
- export { date, iso8601 } from './date';
30
- export { describe } from './describe';
31
- export { dispatch } from './dispatch';
26
+ export { guard } from './_guard';
27
+
28
+ export { compose, map, predicate } from './stdlib/composition';
29
+
30
+ export { array, nonEmptyArray, poja } from './stdlib/array';
31
+ export { boolean, numericBoolean, truthy } from './stdlib/boolean';
32
+ export {
33
+ constant,
34
+ hardcoded,
35
+ mixed,
36
+ null_,
37
+ undefined_,
38
+ unknown,
39
+ } from './stdlib/constants';
40
+ export { date, iso8601 } from './stdlib/date';
41
+ export { describe } from './stdlib/describe';
42
+ export { dispatch } from './stdlib/dispatch';
32
43
  export {
33
44
  either,
34
45
  either3,
@@ -39,18 +50,14 @@ export {
39
50
  either8,
40
51
  either9,
41
52
  oneOf,
42
- } from './either';
43
- export { fail } from './fail';
44
- export { instanceOf } from './instanceOf';
45
- export { json, jsonObject, jsonArray } from './json';
46
- export { lazy } from './lazy';
47
- export { mapping, dict } from './mapping';
48
- export { integer, number, positiveInteger, positiveNumber } from './number';
49
- export { exact, inexact, object, pojo } from './object';
50
- export { maybe, nullable, optional } from './optional';
51
- export { email, nonEmptyString, regex, string, url } from './string';
52
- export { tuple1, tuple2, tuple3, tuple4, tuple5, tuple6 } from './tuple';
53
-
54
- export type { Decoder, Guard };
55
- export type { $DecoderType, $GuardType };
56
- export type { JSONValue, JSONObject, JSONArray } from './json';
53
+ } from './stdlib/either';
54
+ export { fail } from './stdlib/fail';
55
+ export { instanceOf } from './stdlib/instanceOf';
56
+ export { json, jsonObject, jsonArray } from './stdlib/json';
57
+ export { lazy } from './stdlib/lazy';
58
+ export { mapping, dict } from './stdlib/mapping';
59
+ export { integer, number, positiveInteger, positiveNumber } from './stdlib/number';
60
+ export { exact, inexact, object, pojo } from './stdlib/object';
61
+ export { maybe, nullable, optional } from './stdlib/optional';
62
+ export { email, nonEmptyString, regex, string, url } from './stdlib/string';
63
+ export { tuple1, tuple2, tuple3, tuple4, tuple5, tuple6 } from './stdlib/tuple';
package/cjs/result.js ADDED
@@ -0,0 +1,172 @@
1
+ "use strict";
2
+
3
+ exports.__esModule = true;
4
+ exports.andThen = andThen;
5
+ exports.dispatch = dispatch;
6
+ exports.err = err;
7
+ exports.errValue = errValue;
8
+ exports.expect = expect;
9
+ exports.isErr = isErr;
10
+ exports.isOk = isOk;
11
+ exports.map = map;
12
+ exports.mapError = mapError;
13
+ exports.ok = ok;
14
+ exports.orElse = orElse;
15
+ exports.toString = toString;
16
+ exports.unwrap = unwrap;
17
+ exports.value = value;
18
+ exports.withDefault = withDefault;
19
+
20
+ /**
21
+ * Result <value> <error>
22
+ * = Ok <value>
23
+ * | Err <error>
24
+ */
25
+
26
+ /**
27
+ * Create a new Result instance representing a successful computation.
28
+ */
29
+ function ok(value) {
30
+ return {
31
+ type: 'ok',
32
+ value: value
33
+ };
34
+ }
35
+ /**
36
+ * Create a new Result instance representing a failed computation.
37
+ */
38
+
39
+
40
+ function err(error) {
41
+ return {
42
+ type: 'err',
43
+ error: error
44
+ };
45
+ }
46
+
47
+ function toString(result) {
48
+ return result.type === 'ok' ? "Ok(" + String(result.value) + ")" : "Err(" + String(result.error) + ")";
49
+ }
50
+
51
+ function isOk(result) {
52
+ return result.type === 'ok';
53
+ }
54
+
55
+ function isErr(result) {
56
+ return result.type === 'err';
57
+ }
58
+
59
+ function withDefault(result, defaultValue) {
60
+ return result.type === 'ok' ? result.value : defaultValue;
61
+ }
62
+
63
+ function value(result) {
64
+ return result.type === 'ok' ? result.value : undefined;
65
+ }
66
+
67
+ function errValue(result) {
68
+ return result.type === 'err' ? result.error : undefined;
69
+ }
70
+ /**
71
+ * Unwrap the value from this Result instance if this is an "Ok" result.
72
+ * Otherwise, will throw the "Err" error via a runtime exception.
73
+ */
74
+
75
+
76
+ function unwrap(result) {
77
+ if (result.type === 'ok') {
78
+ return result.value;
79
+ } else {
80
+ throw result.error;
81
+ }
82
+ }
83
+
84
+ function expect(result, message) {
85
+ if (result.type === 'ok') {
86
+ return result.value;
87
+ } else {
88
+ throw message instanceof Error ? message : new Error(message);
89
+ }
90
+ }
91
+
92
+ function dispatch(result, okCallback, errCallback) {
93
+ return result.type === 'ok' ? okCallback(result.value) : errCallback(result.error);
94
+ }
95
+ /**
96
+ * If the given result is OK, defers to the other result. Otherwise returns the
97
+ * error result.
98
+ *
99
+ * It's like saying A && B, but on Result.
100
+ *
101
+ * Examples:
102
+ *
103
+ * Result.ok(42) && Result.ok('hi') // => Ok('hi')
104
+ * Result.err('boo') && Result.ok('hi') // => Err('boo')
105
+ * Result.ok(42) && Result.err('boo') // => Err('boo')
106
+ * Result.err('boo') && Result.err('boo') // => Err('boo')
107
+ *
108
+ */
109
+ // export function and<T, E, T2>(
110
+ // result1: Result<T, E>,
111
+ // result2: Result<T2, E>,
112
+ // ): Result<T2, E> {
113
+ // return result1.type === 'ok' ? result2 : result1;
114
+ // }
115
+
116
+ /**
117
+ * If the given result is OK, return that result. Otherwise, defers to the
118
+ * other result.
119
+ *
120
+ * It's like saying A || B, but on Result.
121
+ *
122
+ * Examples:
123
+ *
124
+ * Result.ok(42) || Result.ok('hi') // => Ok(42)
125
+ * Result.err('boo') || Result.ok('hi') // => Ok('hi')
126
+ * Result.ok(42) || Result.err('boo') // => Ok(42)
127
+ * Result.err('bleh') || Result.err('boo') // => Err('boo')
128
+ *
129
+ */
130
+ // export function or<T, E, E2>(
131
+ // result1: Result<T, E>,
132
+ // result2: Result<T, E2>,
133
+ // ): Result<T, E2> {
134
+ // return result1.type === 'ok' ? result1 : result2;
135
+ // }
136
+
137
+ /**
138
+ * Like .and(), aka &&, but the second argument gets evaluated lazily only if
139
+ * the first result is an Ok result. If so, it has access to the Ok value from
140
+ * the first argument.
141
+ */
142
+
143
+
144
+ function andThen(result1, lazyResult2) {
145
+ return result1.type === 'ok' ? lazyResult2(result1.value) : result1;
146
+ }
147
+ /**
148
+ * Like .or(), aka ||, but the second argument gets evaluated lazily only if
149
+ * the first result is an Err result. If so, it has access to the Err value
150
+ * from the first argument.
151
+ */
152
+
153
+
154
+ function orElse(result1, lazyResult2) {
155
+ return result1.type === 'ok' ? result1 : lazyResult2(result1.error);
156
+ }
157
+ /**
158
+ * Transform an Ok result. Will not touch Err results.
159
+ */
160
+
161
+
162
+ function map(result, mapper) {
163
+ return result.type === 'ok' ? ok(mapper(result.value)) : result;
164
+ }
165
+ /**
166
+ * Transform an Err value. Will not touch Ok results.
167
+ */
168
+
169
+
170
+ function mapError(result, mapper) {
171
+ return result.type === 'ok' ? result : err(mapper(result.error));
172
+ }