decoders 2.0.0-beta8 → 2.0.1

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 (146) hide show
  1. package/CHANGELOG.md +67 -20
  2. package/Decoder.d.ts +94 -0
  3. package/Decoder.js +115 -0
  4. package/Decoder.js.flow +286 -0
  5. package/Decoder.mjs +108 -0
  6. package/README.md +122 -1345
  7. package/_utils.d.ts +0 -1
  8. package/_utils.js +51 -68
  9. package/_utils.js.flow +30 -20
  10. package/_utils.mjs +39 -54
  11. package/annotate.js +79 -72
  12. package/annotate.mjs +62 -58
  13. package/format.d.ts +4 -2
  14. package/format.js +64 -74
  15. package/format.js.flow +14 -12
  16. package/format.mjs +58 -68
  17. package/index.d.ts +38 -41
  18. package/index.js +66 -98
  19. package/index.js.flow +39 -58
  20. package/index.mjs +11 -37
  21. package/{core → lib}/_helpers.d.ts +0 -0
  22. package/lib/arrays.d.ts +59 -0
  23. package/lib/arrays.js +97 -0
  24. package/lib/arrays.js.flow +138 -0
  25. package/lib/arrays.mjs +84 -0
  26. package/lib/basics.d.ts +93 -0
  27. package/lib/basics.js +94 -0
  28. package/lib/basics.js.flow +124 -0
  29. package/lib/basics.mjs +75 -0
  30. package/lib/booleans.d.ts +16 -0
  31. package/lib/booleans.js +25 -0
  32. package/lib/booleans.js.flow +22 -0
  33. package/lib/booleans.mjs +15 -0
  34. package/lib/dates.d.ts +15 -0
  35. package/lib/dates.js +28 -0
  36. package/lib/dates.js.flow +40 -0
  37. package/lib/dates.mjs +19 -0
  38. package/lib/json.d.ts +35 -0
  39. package/lib/json.js +33 -0
  40. package/lib/json.js.flow +50 -0
  41. package/lib/json.mjs +18 -0
  42. package/lib/numbers.d.ts +31 -0
  43. package/lib/numbers.js +31 -0
  44. package/lib/numbers.js.flow +48 -0
  45. package/lib/numbers.mjs +21 -0
  46. package/lib/objects.d.ts +76 -0
  47. package/lib/objects.js +161 -0
  48. package/lib/objects.js.flow +238 -0
  49. package/lib/objects.mjs +147 -0
  50. package/lib/strings.d.ts +56 -0
  51. package/lib/strings.js +58 -0
  52. package/lib/strings.js.flow +90 -0
  53. package/lib/strings.mjs +40 -0
  54. package/lib/unions.d.ts +55 -0
  55. package/lib/unions.js +96 -0
  56. package/lib/unions.js.flow +155 -0
  57. package/lib/unions.mjs +83 -0
  58. package/lib/utilities.d.ts +40 -0
  59. package/lib/utilities.js +48 -0
  60. package/lib/utilities.js.flow +65 -0
  61. package/lib/utilities.mjs +37 -0
  62. package/package.json +64 -17
  63. package/result.d.ts +2 -25
  64. package/result.js +9 -90
  65. package/result.js.flow +4 -76
  66. package/result.mjs +5 -71
  67. package/_guard.d.ts +0 -7
  68. package/_guard.js +0 -22
  69. package/_guard.js.flow +0 -20
  70. package/_guard.mjs +0 -15
  71. package/_types.d.ts +0 -13
  72. package/_types.js +0 -1
  73. package/_types.js.flow +0 -20
  74. package/_types.mjs +0 -0
  75. package/core/array.d.ts +0 -5
  76. package/core/array.js +0 -104
  77. package/core/array.js.flow +0 -100
  78. package/core/array.mjs +0 -91
  79. package/core/boolean.d.ts +0 -5
  80. package/core/boolean.js +0 -40
  81. package/core/boolean.js.flow +0 -27
  82. package/core/boolean.mjs +0 -28
  83. package/core/composition.d.ts +0 -14
  84. package/core/composition.js +0 -54
  85. package/core/composition.js.flow +0 -48
  86. package/core/composition.mjs +0 -44
  87. package/core/constants.d.ts +0 -11
  88. package/core/constants.js +0 -65
  89. package/core/constants.js.flow +0 -44
  90. package/core/constants.mjs +0 -46
  91. package/core/date.d.ts +0 -4
  92. package/core/date.js +0 -42
  93. package/core/date.js.flow +0 -38
  94. package/core/date.mjs +0 -28
  95. package/core/describe.d.ts +0 -3
  96. package/core/describe.js +0 -22
  97. package/core/describe.js.flow +0 -17
  98. package/core/describe.mjs +0 -16
  99. package/core/dispatch.d.ts +0 -8
  100. package/core/dispatch.js +0 -58
  101. package/core/dispatch.js.flow +0 -58
  102. package/core/dispatch.mjs +0 -51
  103. package/core/either.d.ts +0 -61
  104. package/core/either.js +0 -113
  105. package/core/either.js.flow +0 -151
  106. package/core/either.mjs +0 -90
  107. package/core/fail.d.ts +0 -3
  108. package/core/fail.js +0 -17
  109. package/core/fail.js.flow +0 -12
  110. package/core/fail.mjs +0 -11
  111. package/core/instanceOf.d.ts +0 -3
  112. package/core/instanceOf.js +0 -15
  113. package/core/instanceOf.js.flow +0 -20
  114. package/core/instanceOf.mjs +0 -8
  115. package/core/json.d.ts +0 -11
  116. package/core/json.js +0 -31
  117. package/core/json.js.flow +0 -28
  118. package/core/json.mjs +0 -15
  119. package/core/lazy.d.ts +0 -3
  120. package/core/lazy.js +0 -16
  121. package/core/lazy.js.flow +0 -15
  122. package/core/lazy.mjs +0 -11
  123. package/core/mapping.d.ts +0 -6
  124. package/core/mapping.js +0 -67
  125. package/core/mapping.js.flow +0 -62
  126. package/core/mapping.mjs +0 -58
  127. package/core/number.d.ts +0 -6
  128. package/core/number.js +0 -36
  129. package/core/number.js.flow +0 -40
  130. package/core/number.mjs +0 -25
  131. package/core/object.d.ts +0 -33
  132. package/core/object.js +0 -190
  133. package/core/object.js.flow +0 -203
  134. package/core/object.mjs +0 -175
  135. package/core/optional.d.ts +0 -5
  136. package/core/optional.js +0 -50
  137. package/core/optional.js.flow +0 -41
  138. package/core/optional.mjs +0 -38
  139. package/core/string.d.ts +0 -10
  140. package/core/string.js +0 -68
  141. package/core/string.js.flow +0 -59
  142. package/core/string.mjs +0 -49
  143. package/core/tuple.d.ts +0 -30
  144. package/core/tuple.js +0 -177
  145. package/core/tuple.js.flow +0 -211
  146. package/core/tuple.mjs +0 -159
package/core/object.mjs DELETED
@@ -1,175 +0,0 @@
1
- function _extends() { _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }
2
-
3
- import { annotate, annotateObject, merge, updateText } from '../annotate.mjs';
4
- import { compose, map } from './composition.mjs';
5
- import { err, ok } from '../result.mjs';
6
-
7
- function isPojo(o) {
8
- return o !== null && o !== undefined && typeof o === 'object' && // This still seems to be the only reliable way to determine whether
9
- // something is a pojo... ¯\_(ツ)_/¯
10
- // $FlowFixMe[method-unbinding]
11
- Object.prototype.toString.call(o) === '[object Object]';
12
- }
13
-
14
- function subtract(xs, ys) {
15
- var result = new Set();
16
- xs.forEach(function (x) {
17
- if (!ys.has(x)) {
18
- result.add(x);
19
- }
20
- });
21
- return result;
22
- }
23
-
24
- export var pojo = function pojo(blob) {
25
- return isPojo(blob) ? ok( // NOTE:
26
- // Since Flow 0.98, typeof o === 'object' refines to
27
- // {| +[string]: mixed |}
28
- // instead of
29
- // {| [string]: mixed |}
30
- //
31
- // For rationale, see https://github.com/facebook/flow/issues/7685.
32
- // In this case, we don't want to output a read-only version of
33
- // the object because it's up to the user of decoders to
34
- // determine what they want to do with the decoded output. If they
35
- // want to write items into the array, that's fine! The fastest
36
- // way to turn a read-only Object to a writeable one in ES6 seems
37
- // to be to use object-spread. (Going off this benchmark:
38
- // https://thecodebarbarian.com/object-assign-vs-object-spread.html)
39
- _extends({}, blob)) : err(annotate(blob, 'Must be an object'));
40
- };
41
- /**
42
- * Given a mapping of fields-to-decoders, builds a decoder for an object type.
43
- *
44
- * For example, given decoders for a number and a string, we can construct an
45
- * "object description" like so:
46
- *
47
- * { id: number, name: string }
48
- *
49
- * Which is of type:
50
- *
51
- * { id: Decoder<number>, name: Decoder<string> }
52
- *
53
- * Passing this to object() will produce the following return type:
54
- *
55
- * Decoder<{ id: number, name: string }>
56
- *
57
- * Put simply: it'll "peel off" all of the nested Decoders, puts them together
58
- * in an object, and wraps it in a Decoder<...>.
59
- */
60
-
61
- export function object(mapping) {
62
- var known = new Set(Object.keys(mapping));
63
- return compose(pojo, function (blob) {
64
- var actual = new Set(Object.keys(blob)); // At this point, "missing" will also include all fields that may
65
- // validly be optional. We'll let the underlying decoder decide and
66
- // remove the key from this missing set if the decoder accepts the
67
- // value.
68
-
69
- var missing = subtract(known, actual);
70
- var record = {};
71
- var errors = null;
72
- Object.keys(mapping).forEach(function (key) {
73
- var decoder = mapping[key];
74
- var rawValue = blob[key];
75
- var result = decoder(rawValue);
76
-
77
- if (result.type === 'ok') {
78
- var value = result.value;
79
-
80
- if (value !== undefined) {
81
- record[key] = value;
82
- } // If this succeeded, remove the key from the missing keys
83
- // tracker
84
-
85
-
86
- missing["delete"](key);
87
- } else {
88
- var ann = result.error; // Keep track of the annotation, but don't return just yet. We
89
- // want to collect more error information.
90
-
91
- if (rawValue === undefined) {
92
- // Explicitly add it to the missing set if the value is
93
- // undefined. This covers explicit undefineds to be
94
- // treated the same as implicit undefineds (aka missing
95
- // keys).
96
- missing.add(key);
97
- } else {
98
- if (errors === null) {
99
- errors = {};
100
- }
101
-
102
- errors[key] = ann;
103
- }
104
- }
105
- }); // Deal with errors now. There are two classes of errors we want to
106
- // report. First of all, we want to report any inline errors in this
107
- // object. Lastly, any fields that are missing should be annotated on
108
- // the outer object itself.
109
-
110
- if (errors || missing.size > 0) {
111
- var objAnn = annotateObject(blob);
112
-
113
- if (errors) {
114
- objAnn = merge(objAnn, errors);
115
- }
116
-
117
- if (missing.size > 0) {
118
- var errMsg = Array.from(missing).map(function (key) {
119
- return "\"" + key + "\"";
120
- }).join(', ');
121
- var pluralized = missing.size > 1 ? 'keys' : 'key';
122
- objAnn = updateText(objAnn, "Missing " + pluralized + ": " + errMsg);
123
- }
124
-
125
- return err(objAnn);
126
- }
127
-
128
- return ok(record);
129
- });
130
- }
131
- export function exact(mapping) {
132
- // Check the inputted object for any superfluous keys
133
- var allowed = new Set(Object.keys(mapping));
134
- var checked = compose(pojo, function (blob) {
135
- var actual = new Set(Object.keys(blob));
136
- var superfluous = subtract(actual, allowed);
137
-
138
- if (superfluous.size > 0) {
139
- return err(annotate(blob, "Superfluous keys: " + Array.from(superfluous).join(', ')));
140
- }
141
-
142
- return ok(blob);
143
- }); // Defer to the "object" decoder for doing the real decoding work. Since
144
- // we made sure there are no superfluous keys in this structure, it's now
145
- // safe to force-cast it to an $Exact<> type.
146
-
147
- var decoder = object(mapping);
148
- return compose(checked, decoder);
149
- }
150
- export function inexact(mapping) {
151
- return compose(pojo, function (blob) {
152
- var allkeys = new Set(Object.keys(blob));
153
- var decoder = map(object(mapping), function (safepart) {
154
- var safekeys = new Set(Object.keys(mapping)); // To account for hard-coded keys that aren't part of the input
155
-
156
- safekeys.forEach(function (k) {
157
- return allkeys.add(k);
158
- });
159
- var rv = {};
160
- allkeys.forEach(function (k) {
161
- if (safekeys.has(k)) {
162
- var value = safepart[k];
163
-
164
- if (value !== undefined) {
165
- rv[k] = value;
166
- }
167
- } else {
168
- rv[k] = blob[k];
169
- }
170
- });
171
- return rv;
172
- });
173
- return decoder(blob);
174
- });
175
- }
@@ -1,5 +0,0 @@
1
- import { Decoder } from '../_types';
2
-
3
- export function optional<T>(decoder: Decoder<T>): Decoder<T | undefined>;
4
- export function nullable<T>(decoder: Decoder<T>): Decoder<T | null>;
5
- export function maybe<T>(decoder: Decoder<T>): Decoder<T | null | undefined>;
package/core/optional.js DELETED
@@ -1,50 +0,0 @@
1
- "use strict";
2
-
3
- exports.__esModule = true;
4
- exports.maybe = maybe;
5
- exports.nullable = nullable;
6
- exports.optional = optional;
7
-
8
- var _annotate = require("../annotate");
9
-
10
- var _either = require("./either");
11
-
12
- var _result = require("../result");
13
-
14
- var _constants = require("./constants");
15
-
16
- /**
17
- * Builds a Decoder that returns Ok for either `undefined` or `T` values,
18
- * given a Decoder for `T`. Err otherwise.
19
- */
20
- function optional(decoder) {
21
- return (0, _either.either)(_constants.undefined_, decoder);
22
- }
23
- /**
24
- * Builds a Decoder that returns Ok for either `null` or `T` values,
25
- * given a Decoder for `T`. Err otherwise.
26
- */
27
-
28
-
29
- function nullable(decoder) {
30
- return (0, _either.either)(_constants.null_, decoder);
31
- }
32
- /**
33
- * Decoder that only returns Ok for `null` or `undefined` inputs.
34
- * This is effectively equivalent to either(null_, undefined_), but combines
35
- * their error message output into a single line for convenience.
36
- */
37
-
38
-
39
- var undefined_or_null = function undefined_or_null(blob) {
40
- return blob === undefined || blob === null ? (0, _result.ok)(blob) : // Combine error message into a single line
41
- (0, _result.err)((0, _annotate.annotate)(blob, 'Must be undefined or null'));
42
- };
43
- /**
44
- * Decoder that only returns Ok for `null` or `undefined` inputs.
45
- */
46
-
47
-
48
- function maybe(decoder) {
49
- return (0, _either.either)(undefined_or_null, decoder);
50
- }
@@ -1,41 +0,0 @@
1
- // @flow strict
2
-
3
- import { annotate } from '../annotate';
4
- import { either } from './either';
5
- import { err, ok } from '../result';
6
- import { null_, undefined_ } from './constants';
7
- import type { Decoder } from '../_types';
8
-
9
- /**
10
- * Builds a Decoder that returns Ok for either `undefined` or `T` values,
11
- * given a Decoder for `T`. Err otherwise.
12
- */
13
- export function optional<T>(decoder: Decoder<T>): Decoder<void | T> {
14
- return either(undefined_, decoder);
15
- }
16
-
17
- /**
18
- * Builds a Decoder that returns Ok for either `null` or `T` values,
19
- * given a Decoder for `T`. Err otherwise.
20
- */
21
- export function nullable<T>(decoder: Decoder<T>): Decoder<null | T> {
22
- return either(null_, decoder);
23
- }
24
-
25
- /**
26
- * Decoder that only returns Ok for `null` or `undefined` inputs.
27
- * This is effectively equivalent to either(null_, undefined_), but combines
28
- * their error message output into a single line for convenience.
29
- */
30
- const undefined_or_null: Decoder<null | void> = (blob: mixed) =>
31
- blob === undefined || blob === null
32
- ? ok(blob)
33
- : // Combine error message into a single line
34
- err(annotate(blob, 'Must be undefined or null'));
35
-
36
- /**
37
- * Decoder that only returns Ok for `null` or `undefined` inputs.
38
- */
39
- export function maybe<T>(decoder: Decoder<T>): Decoder<?T> {
40
- return either(undefined_or_null, decoder);
41
- }
package/core/optional.mjs DELETED
@@ -1,38 +0,0 @@
1
- import { annotate } from '../annotate.mjs';
2
- import { either } from './either.mjs';
3
- import { err, ok } from '../result.mjs';
4
- import { null_, undefined_ } from './constants.mjs';
5
-
6
- /**
7
- * Builds a Decoder that returns Ok for either `undefined` or `T` values,
8
- * given a Decoder for `T`. Err otherwise.
9
- */
10
- export function optional(decoder) {
11
- return either(undefined_, decoder);
12
- }
13
- /**
14
- * Builds a Decoder that returns Ok for either `null` or `T` values,
15
- * given a Decoder for `T`. Err otherwise.
16
- */
17
-
18
- export function nullable(decoder) {
19
- return either(null_, decoder);
20
- }
21
- /**
22
- * Decoder that only returns Ok for `null` or `undefined` inputs.
23
- * This is effectively equivalent to either(null_, undefined_), but combines
24
- * their error message output into a single line for convenience.
25
- */
26
-
27
- var undefined_or_null = function undefined_or_null(blob) {
28
- return blob === undefined || blob === null ? ok(blob) : // Combine error message into a single line
29
- err(annotate(blob, 'Must be undefined or null'));
30
- };
31
- /**
32
- * Decoder that only returns Ok for `null` or `undefined` inputs.
33
- */
34
-
35
-
36
- export function maybe(decoder) {
37
- return either(undefined_or_null, decoder);
38
- }
package/core/string.d.ts DELETED
@@ -1,10 +0,0 @@
1
- /// <reference lib="dom" />
2
-
3
- import { Decoder } from '../_types';
4
-
5
- export const string: Decoder<string>;
6
- export const nonEmptyString: Decoder<string>;
7
- export function regex(regex: RegExp, msg: string): Decoder<string>;
8
- export const email: Decoder<string>;
9
- export const url: Decoder<URL>;
10
- export const httpsUrl: Decoder<URL>;
package/core/string.js DELETED
@@ -1,68 +0,0 @@
1
- "use strict";
2
-
3
- exports.__esModule = true;
4
- exports.nonEmptyString = exports.httpsUrl = exports.email = void 0;
5
- exports.regex = regex;
6
- exports.url = exports.string = void 0;
7
-
8
- var _annotate = require("../annotate");
9
-
10
- var _either = require("./either");
11
-
12
- var _result = require("../result");
13
-
14
- var _instanceOf = require("./instanceOf");
15
-
16
- var _composition = require("./composition");
17
-
18
- /** Match groups in this regex:
19
- * \1 - the scheme
20
- * \2 - the username/password (optional)
21
- * \3 - the host
22
- * \4 - the port (optional)
23
- * \5 - the path (optional)
24
- */
25
- 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]*)?)?$/;
26
- /**
27
- * Decoder that only returns Ok for string inputs. Err otherwise.
28
- */
29
-
30
- var string = function string(blob) {
31
- return typeof blob === 'string' ? (0, _result.ok)(blob) : (0, _result.err)((0, _annotate.annotate)(blob, 'Must be string'));
32
- };
33
- /**
34
- * Decoder that only returns Ok for non-empty string inputs. Err otherwise.
35
- */
36
-
37
-
38
- exports.string = string;
39
- var nonEmptyString = regex(/\S/, 'Must be non-empty string');
40
- /**
41
- * Decoder that only returns Ok for string inputs that match the regular
42
- * expression. Err otherwise. Will always validate that the input is a string
43
- * before testing the regex.
44
- */
45
-
46
- exports.nonEmptyString = nonEmptyString;
47
-
48
- function regex(regex, msg) {
49
- return (0, _composition.predicate)(string, function (s) {
50
- return regex.test(s);
51
- }, msg);
52
- }
53
- /**
54
- * Decoder that only returns Ok for string inputs that match the almost perfect
55
- * email regex, taken from http://emailregex.com. Err otherwise.
56
- */
57
-
58
-
59
- var email = regex(/^(([^<>()[\]\\.,;:\s@"]+(\.[^<>()[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/, 'Must be email');
60
- exports.email = email;
61
- var url = (0, _either.either)((0, _composition.map)(regex(url_re, 'Must be URL'), function (value) {
62
- return new URL(value);
63
- }), (0, _instanceOf.instanceOf)(URL));
64
- exports.url = url;
65
- var httpsUrl = (0, _composition.predicate)(url, function (value) {
66
- return value.protocol === 'https:';
67
- }, 'Must be an HTTPS URL');
68
- exports.httpsUrl = httpsUrl;
@@ -1,59 +0,0 @@
1
- // @flow strict
2
-
3
- import { annotate } from '../annotate';
4
- import { either } from './either';
5
- import { err, ok } from '../result';
6
- import { instanceOf } from './instanceOf';
7
- import { map, predicate } from './composition';
8
- import type { Decoder } from '../_types';
9
-
10
- /** Match groups in this regex:
11
- * \1 - the scheme
12
- * \2 - the username/password (optional)
13
- * \3 - the host
14
- * \4 - the port (optional)
15
- * \5 - the path (optional)
16
- */
17
- const url_re =
18
- /^([A-Za-z]{3,9}(?:[+][A-Za-z]{3,9})?):\/\/(?:([-;:&=+$,\w]+)@)?(?:([A-Za-z0-9.-]+)(?::([0-9]{2,5}))?)(\/(?:[-+~%/.,\w]*)?(?:\?[-+=&;%@.,\w]*)?(?:#[.,!/\w]*)?)?$/;
19
-
20
- /**
21
- * Decoder that only returns Ok for string inputs. Err otherwise.
22
- */
23
- export const string: Decoder<string> = (blob: mixed) => {
24
- return typeof blob === 'string' ? ok(blob) : err(annotate(blob, 'Must be string'));
25
- };
26
-
27
- /**
28
- * Decoder that only returns Ok for non-empty string inputs. Err otherwise.
29
- */
30
- export const nonEmptyString: Decoder<string> = regex(/\S/, 'Must be non-empty string');
31
-
32
- /**
33
- * Decoder that only returns Ok for string inputs that match the regular
34
- * expression. Err otherwise. Will always validate that the input is a string
35
- * before testing the regex.
36
- */
37
- export function regex(regex: RegExp, msg: string): Decoder<string> {
38
- return predicate(string, (s) => regex.test(s), msg);
39
- }
40
-
41
- /**
42
- * Decoder that only returns Ok for string inputs that match the almost perfect
43
- * email regex, taken from http://emailregex.com. Err otherwise.
44
- */
45
- export const email: Decoder<string> = regex(
46
- /^(([^<>()[\]\\.,;:\s@"]+(\.[^<>()[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/,
47
- 'Must be email',
48
- );
49
-
50
- export const url: Decoder<URL> = either(
51
- map(regex(url_re, 'Must be URL'), (value) => new URL(value)),
52
- instanceOf(URL),
53
- );
54
-
55
- export const httpsUrl: Decoder<URL> = predicate(
56
- url,
57
- (value) => value.protocol === 'https:',
58
- 'Must be an HTTPS URL',
59
- );
package/core/string.mjs DELETED
@@ -1,49 +0,0 @@
1
- import { annotate } from '../annotate.mjs';
2
- import { either } from './either.mjs';
3
- import { err, ok } from '../result.mjs';
4
- import { instanceOf } from './instanceOf.mjs';
5
- import { map, predicate } from './composition.mjs';
6
-
7
- /** Match groups in this regex:
8
- * \1 - the scheme
9
- * \2 - the username/password (optional)
10
- * \3 - the host
11
- * \4 - the port (optional)
12
- * \5 - the path (optional)
13
- */
14
- 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]*)?)?$/;
15
- /**
16
- * Decoder that only returns Ok for string inputs. Err otherwise.
17
- */
18
-
19
- export var string = function string(blob) {
20
- return typeof blob === 'string' ? ok(blob) : err(annotate(blob, 'Must be string'));
21
- };
22
- /**
23
- * Decoder that only returns Ok for non-empty string inputs. Err otherwise.
24
- */
25
-
26
- export var nonEmptyString = regex(/\S/, 'Must be non-empty string');
27
- /**
28
- * Decoder that only returns Ok for string inputs that match the regular
29
- * expression. Err otherwise. Will always validate that the input is a string
30
- * before testing the regex.
31
- */
32
-
33
- export function regex(regex, msg) {
34
- return predicate(string, function (s) {
35
- return regex.test(s);
36
- }, msg);
37
- }
38
- /**
39
- * Decoder that only returns Ok for string inputs that match the almost perfect
40
- * email regex, taken from http://emailregex.com. Err otherwise.
41
- */
42
-
43
- export var email = regex(/^(([^<>()[\]\\.,;:\s@"]+(\.[^<>()[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/, 'Must be email');
44
- export var url = either(map(regex(url_re, 'Must be URL'), function (value) {
45
- return new URL(value);
46
- }), instanceOf(URL));
47
- export var httpsUrl = predicate(url, function (value) {
48
- return value.protocol === 'https:';
49
- }, 'Must be an HTTPS URL');
package/core/tuple.d.ts DELETED
@@ -1,30 +0,0 @@
1
- import { Decoder } from '../_types';
2
-
3
- export function tuple1<T1>(d1: Decoder<T1>): Decoder<[T1]>;
4
- export function tuple2<T1, T2>(d1: Decoder<T1>, d2: Decoder<T2>): Decoder<[T1, T2]>;
5
- export function tuple3<T1, T2, T3>(
6
- d1: Decoder<T1>,
7
- d2: Decoder<T2>,
8
- d3: Decoder<T3>,
9
- ): Decoder<[T1, T2, T3]>;
10
- export function tuple4<T1, T2, T3, T4>(
11
- d1: Decoder<T1>,
12
- d2: Decoder<T2>,
13
- d3: Decoder<T3>,
14
- d4: Decoder<T4>,
15
- ): Decoder<[T1, T2, T3, T4]>;
16
- export function tuple5<T1, T2, T3, T4, T5>(
17
- d1: Decoder<T1>,
18
- d2: Decoder<T2>,
19
- d3: Decoder<T3>,
20
- d4: Decoder<T4>,
21
- d5: Decoder<T5>,
22
- ): Decoder<[T1, T2, T3, T4, T5]>;
23
- export function tuple6<T1, T2, T3, T4, T5, T6>(
24
- d1: Decoder<T1>,
25
- d2: Decoder<T2>,
26
- d3: Decoder<T3>,
27
- d4: Decoder<T4>,
28
- d5: Decoder<T5>,
29
- d6: Decoder<T6>,
30
- ): Decoder<[T1, T2, T3, T4, T5, T6]>;