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.
- package/CHANGELOG.md +67 -20
- package/Decoder.d.ts +94 -0
- package/Decoder.js +115 -0
- package/Decoder.js.flow +286 -0
- package/Decoder.mjs +108 -0
- package/README.md +122 -1345
- package/_utils.d.ts +0 -1
- package/_utils.js +51 -68
- package/_utils.js.flow +30 -20
- package/_utils.mjs +39 -54
- package/annotate.js +79 -72
- package/annotate.mjs +62 -58
- package/format.d.ts +4 -2
- package/format.js +64 -74
- package/format.js.flow +14 -12
- package/format.mjs +58 -68
- package/index.d.ts +38 -41
- package/index.js +66 -98
- package/index.js.flow +39 -58
- package/index.mjs +11 -37
- package/{core → lib}/_helpers.d.ts +0 -0
- package/lib/arrays.d.ts +59 -0
- package/lib/arrays.js +97 -0
- package/lib/arrays.js.flow +138 -0
- package/lib/arrays.mjs +84 -0
- package/lib/basics.d.ts +93 -0
- package/lib/basics.js +94 -0
- package/lib/basics.js.flow +124 -0
- package/lib/basics.mjs +75 -0
- package/lib/booleans.d.ts +16 -0
- package/lib/booleans.js +25 -0
- package/lib/booleans.js.flow +22 -0
- package/lib/booleans.mjs +15 -0
- package/lib/dates.d.ts +15 -0
- package/lib/dates.js +28 -0
- package/lib/dates.js.flow +40 -0
- package/lib/dates.mjs +19 -0
- package/lib/json.d.ts +35 -0
- package/lib/json.js +33 -0
- package/lib/json.js.flow +50 -0
- package/lib/json.mjs +18 -0
- package/lib/numbers.d.ts +31 -0
- package/lib/numbers.js +31 -0
- package/lib/numbers.js.flow +48 -0
- package/lib/numbers.mjs +21 -0
- package/lib/objects.d.ts +76 -0
- package/lib/objects.js +161 -0
- package/lib/objects.js.flow +238 -0
- package/lib/objects.mjs +147 -0
- package/lib/strings.d.ts +56 -0
- package/lib/strings.js +58 -0
- package/lib/strings.js.flow +90 -0
- package/lib/strings.mjs +40 -0
- package/lib/unions.d.ts +55 -0
- package/lib/unions.js +96 -0
- package/lib/unions.js.flow +155 -0
- package/lib/unions.mjs +83 -0
- package/lib/utilities.d.ts +40 -0
- package/lib/utilities.js +48 -0
- package/lib/utilities.js.flow +65 -0
- package/lib/utilities.mjs +37 -0
- package/package.json +64 -17
- package/result.d.ts +2 -25
- package/result.js +9 -90
- package/result.js.flow +4 -76
- package/result.mjs +5 -71
- package/_guard.d.ts +0 -7
- package/_guard.js +0 -22
- package/_guard.js.flow +0 -20
- package/_guard.mjs +0 -15
- package/_types.d.ts +0 -13
- package/_types.js +0 -1
- package/_types.js.flow +0 -20
- package/_types.mjs +0 -0
- package/core/array.d.ts +0 -5
- package/core/array.js +0 -104
- package/core/array.js.flow +0 -100
- package/core/array.mjs +0 -91
- package/core/boolean.d.ts +0 -5
- package/core/boolean.js +0 -40
- package/core/boolean.js.flow +0 -27
- package/core/boolean.mjs +0 -28
- package/core/composition.d.ts +0 -14
- package/core/composition.js +0 -54
- package/core/composition.js.flow +0 -48
- package/core/composition.mjs +0 -44
- package/core/constants.d.ts +0 -11
- package/core/constants.js +0 -65
- package/core/constants.js.flow +0 -44
- package/core/constants.mjs +0 -46
- package/core/date.d.ts +0 -4
- package/core/date.js +0 -42
- package/core/date.js.flow +0 -38
- package/core/date.mjs +0 -28
- package/core/describe.d.ts +0 -3
- package/core/describe.js +0 -22
- package/core/describe.js.flow +0 -17
- package/core/describe.mjs +0 -16
- package/core/dispatch.d.ts +0 -8
- package/core/dispatch.js +0 -58
- package/core/dispatch.js.flow +0 -58
- package/core/dispatch.mjs +0 -51
- package/core/either.d.ts +0 -61
- package/core/either.js +0 -113
- package/core/either.js.flow +0 -151
- package/core/either.mjs +0 -90
- package/core/fail.d.ts +0 -3
- package/core/fail.js +0 -17
- package/core/fail.js.flow +0 -12
- package/core/fail.mjs +0 -11
- package/core/instanceOf.d.ts +0 -3
- package/core/instanceOf.js +0 -15
- package/core/instanceOf.js.flow +0 -20
- package/core/instanceOf.mjs +0 -8
- package/core/json.d.ts +0 -11
- package/core/json.js +0 -31
- package/core/json.js.flow +0 -28
- package/core/json.mjs +0 -15
- package/core/lazy.d.ts +0 -3
- package/core/lazy.js +0 -16
- package/core/lazy.js.flow +0 -15
- package/core/lazy.mjs +0 -11
- package/core/mapping.d.ts +0 -6
- package/core/mapping.js +0 -67
- package/core/mapping.js.flow +0 -62
- package/core/mapping.mjs +0 -58
- package/core/number.d.ts +0 -6
- package/core/number.js +0 -36
- package/core/number.js.flow +0 -40
- package/core/number.mjs +0 -25
- package/core/object.d.ts +0 -33
- package/core/object.js +0 -190
- package/core/object.js.flow +0 -203
- package/core/object.mjs +0 -175
- package/core/optional.d.ts +0 -5
- package/core/optional.js +0 -50
- package/core/optional.js.flow +0 -41
- package/core/optional.mjs +0 -38
- package/core/string.d.ts +0 -10
- package/core/string.js +0 -68
- package/core/string.js.flow +0 -59
- package/core/string.mjs +0 -49
- package/core/tuple.d.ts +0 -30
- package/core/tuple.js +0 -177
- package/core/tuple.js.flow +0 -211
- 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
|
-
}
|
package/core/optional.d.ts
DELETED
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
|
-
}
|
package/core/optional.js.flow
DELETED
|
@@ -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;
|
package/core/string.js.flow
DELETED
|
@@ -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]>;
|