decoders 2.0.0 → 2.0.2
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 +16 -0
- package/Decoder.js +41 -148
- package/Decoder.js.flow +1 -1
- package/Decoder.mjs +38 -145
- package/README.md +1 -1
- package/_utils.js +50 -61
- package/_utils.js.flow +18 -4
- package/_utils.mjs +41 -52
- package/annotate.js +79 -72
- package/annotate.mjs +62 -58
- package/format.d.ts +2 -2
- package/format.js +64 -74
- package/format.js.flow +11 -11
- package/format.mjs +58 -68
- package/index.js +66 -66
- package/index.mjs +11 -11
- package/lib/arrays.js +47 -89
- package/lib/arrays.mjs +38 -78
- package/lib/basics.js +59 -109
- package/lib/basics.mjs +49 -94
- package/lib/booleans.js +14 -24
- package/lib/booleans.mjs +9 -19
- package/lib/dates.js +18 -34
- package/lib/dates.mjs +12 -27
- package/lib/json.d.ts +3 -3
- package/lib/json.js +20 -42
- package/lib/json.js.flow +2 -2
- package/lib/json.mjs +13 -35
- package/lib/numbers.js +19 -39
- package/lib/numbers.mjs +11 -31
- package/lib/objects.d.ts +11 -10
- package/lib/objects.js +84 -163
- package/lib/objects.js.flow +4 -12
- package/lib/objects.mjs +74 -150
- package/lib/strings.js +41 -84
- package/lib/strings.mjs +25 -67
- package/lib/unions.d.ts +1 -1
- package/lib/unions.js +52 -116
- package/lib/unions.js.flow +1 -1
- package/lib/unions.mjs +46 -109
- package/lib/utilities.d.ts +7 -1
- package/lib/utilities.js +23 -50
- package/lib/utilities.mjs +15 -38
- package/package.json +1 -1
- package/result.js +9 -22
- package/result.mjs +5 -17
package/lib/arrays.mjs
CHANGED
|
@@ -1,124 +1,84 @@
|
|
|
1
|
-
import { annotate } from '../annotate.mjs'
|
|
2
|
-
import { define } from '../Decoder.mjs'
|
|
3
|
-
|
|
4
|
-
/**
|
|
5
|
-
* Accepts any array, but doesn't validate its items further.
|
|
6
|
-
*
|
|
7
|
-
* "poja" means "plain old JavaScript array", a play on `pojo()`.
|
|
8
|
-
*/
|
|
1
|
+
import { annotate } from '../annotate.mjs'
|
|
2
|
+
import { define } from '../Decoder.mjs'
|
|
3
|
+
|
|
9
4
|
export var poja = define(function (blob, ok, err) {
|
|
10
5
|
if (!Array.isArray(blob)) {
|
|
11
|
-
return err('Must be an array')
|
|
6
|
+
return err('Must be an array')
|
|
12
7
|
}
|
|
13
8
|
|
|
14
|
-
return ok(
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
// If they want to write items into the array, that's fine!
|
|
20
|
-
// The fastest way to turn a read-only array into a normal array in
|
|
21
|
-
// Javascript is to use .slice() on it, see this benchmark:
|
|
22
|
-
// http://jsben.ch/lO6C5
|
|
23
|
-
blob.slice());
|
|
24
|
-
});
|
|
25
|
-
/**
|
|
26
|
-
* Given an array of Result instances, loop over them all and return:
|
|
27
|
-
* - An [index, err] tuple, indicating the (index of the) first Err instance
|
|
28
|
-
* encountered; or
|
|
29
|
-
* - a new Ok with an array of all unwrapped Ok'ed values
|
|
30
|
-
*/
|
|
31
|
-
|
|
32
|
-
function all(items, blobs, // TODO: Make this less ugly
|
|
33
|
-
ok, err) {
|
|
34
|
-
var results = [];
|
|
9
|
+
return ok(blob.slice())
|
|
10
|
+
})
|
|
11
|
+
|
|
12
|
+
function all(items, blobs, ok, err) {
|
|
13
|
+
var results = []
|
|
35
14
|
|
|
36
15
|
for (var index = 0; index < items.length; ++index) {
|
|
37
|
-
var result = items[index]
|
|
16
|
+
var result = items[index]
|
|
38
17
|
|
|
39
18
|
if (result.ok) {
|
|
40
|
-
results.push(result.value)
|
|
19
|
+
results.push(result.value)
|
|
41
20
|
} else {
|
|
42
|
-
var ann = result.error
|
|
21
|
+
var ann = result.error
|
|
43
22
|
|
|
44
|
-
var clone = [].concat(blobs)
|
|
45
|
-
clone.splice(index, 1, annotate(ann, ann.text ? ann.text +
|
|
46
|
-
return err(annotate(clone))
|
|
23
|
+
var clone = [].concat(blobs)
|
|
24
|
+
clone.splice(index, 1, annotate(ann, ann.text ? ann.text + ' (at index ' + index + ')' : 'index ' + index))
|
|
25
|
+
return err(annotate(clone))
|
|
47
26
|
}
|
|
48
27
|
}
|
|
49
28
|
|
|
50
|
-
return ok(results)
|
|
29
|
+
return ok(results)
|
|
51
30
|
}
|
|
52
|
-
/**
|
|
53
|
-
* Accepts arrays of whatever the given decoder accepts.
|
|
54
|
-
*/
|
|
55
|
-
|
|
56
31
|
|
|
57
32
|
export function array(decoder) {
|
|
58
33
|
return poja.then(function (blobs, ok, err) {
|
|
59
|
-
var results = blobs.map(decoder.decode)
|
|
60
|
-
return all(results, blobs, ok, err)
|
|
61
|
-
})
|
|
34
|
+
var results = blobs.map(decoder.decode)
|
|
35
|
+
return all(results, blobs, ok, err)
|
|
36
|
+
})
|
|
62
37
|
}
|
|
63
|
-
/**
|
|
64
|
-
* Like `array()`, but will reject arrays with 0 elements.
|
|
65
|
-
*/
|
|
66
38
|
|
|
67
39
|
export function nonEmptyArray(decoder) {
|
|
68
40
|
return array(decoder).refine(function (arr) {
|
|
69
|
-
return arr.length > 0
|
|
70
|
-
}, 'Must be non-empty array')
|
|
41
|
+
return arr.length > 0
|
|
42
|
+
}, 'Must be non-empty array')
|
|
71
43
|
}
|
|
72
|
-
/**
|
|
73
|
-
* Similar to `array()`, but returns the result as an [ES6
|
|
74
|
-
* Set](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Set).
|
|
75
|
-
*/
|
|
76
44
|
|
|
77
45
|
export function set(decoder) {
|
|
78
46
|
return array(decoder).transform(function (items) {
|
|
79
|
-
return new Set(items)
|
|
80
|
-
})
|
|
47
|
+
return new Set(items)
|
|
48
|
+
})
|
|
81
49
|
}
|
|
82
50
|
|
|
83
51
|
var ntuple = function ntuple(n) {
|
|
84
52
|
return poja.refine(function (arr) {
|
|
85
|
-
return arr.length === n
|
|
86
|
-
},
|
|
87
|
-
}
|
|
88
|
-
|
|
53
|
+
return arr.length === n
|
|
54
|
+
}, 'Must be a ' + n + '-tuple')
|
|
55
|
+
}
|
|
89
56
|
|
|
90
57
|
function _tuple() {
|
|
91
58
|
for (var _len = arguments.length, decoders = new Array(_len), _key = 0; _key < _len; _key++) {
|
|
92
|
-
decoders[_key] = arguments[_key]
|
|
59
|
+
decoders[_key] = arguments[_key]
|
|
93
60
|
}
|
|
94
61
|
|
|
95
62
|
return ntuple(decoders.length).then(function (blobs, ok, err) {
|
|
96
|
-
var allOk = true
|
|
63
|
+
var allOk = true
|
|
97
64
|
var rvs = decoders.map(function (decoder, i) {
|
|
98
|
-
var blob = blobs[i]
|
|
99
|
-
var result = decoder.decode(blob)
|
|
65
|
+
var blob = blobs[i]
|
|
66
|
+
var result = decoder.decode(blob)
|
|
100
67
|
|
|
101
68
|
if (result.ok) {
|
|
102
|
-
return result.value
|
|
69
|
+
return result.value
|
|
103
70
|
} else {
|
|
104
|
-
allOk = false
|
|
105
|
-
return result.error
|
|
71
|
+
allOk = false
|
|
72
|
+
return result.error
|
|
106
73
|
}
|
|
107
|
-
})
|
|
74
|
+
})
|
|
108
75
|
|
|
109
76
|
if (allOk) {
|
|
110
|
-
return ok(rvs)
|
|
77
|
+
return ok(rvs)
|
|
111
78
|
} else {
|
|
112
|
-
|
|
113
|
-
// results, try to construct a good error message
|
|
114
|
-
return err(annotate(rvs));
|
|
79
|
+
return err(annotate(rvs))
|
|
115
80
|
}
|
|
116
|
-
})
|
|
81
|
+
})
|
|
117
82
|
}
|
|
118
|
-
/**
|
|
119
|
-
* Accepts a tuple (an array with exactly _n_ items) of values accepted by the
|
|
120
|
-
* _n_ given decoders.
|
|
121
|
-
*/
|
|
122
|
-
|
|
123
83
|
|
|
124
|
-
export var tuple = _tuple
|
|
84
|
+
export var tuple = _tuple
|
package/lib/basics.js
CHANGED
|
@@ -1,144 +1,94 @@
|
|
|
1
|
-
|
|
1
|
+
'use strict'
|
|
2
2
|
|
|
3
|
-
exports.__esModule = true
|
|
4
|
-
exports.always = always
|
|
5
|
-
exports.constant = constant
|
|
6
|
-
exports.unknown = exports.undefined_ = exports.optional = exports.nullable = exports.null_ = exports.mixed = exports.maybe = exports.hardcoded = void 0
|
|
3
|
+
exports.__esModule = true
|
|
4
|
+
exports.always = always
|
|
5
|
+
exports.constant = constant
|
|
6
|
+
exports.unknown = exports.undefined_ = exports.optional = exports.nullable = exports.null_ = exports.mixed = exports.maybe = exports.hardcoded = void 0
|
|
7
7
|
|
|
8
|
-
var _Decoder = require(
|
|
8
|
+
var _Decoder = require('../Decoder')
|
|
9
9
|
|
|
10
|
-
var _unions = require(
|
|
10
|
+
var _unions = require('./unions')
|
|
11
11
|
|
|
12
|
-
/**
|
|
13
|
-
* Accepts and returns only the literal `null` value.
|
|
14
|
-
*/
|
|
15
12
|
var null_ = (0, _Decoder.define)(function (blob, ok, err) {
|
|
16
|
-
return blob === null ? ok(blob) : err('Must be null')
|
|
17
|
-
})
|
|
18
|
-
/**
|
|
19
|
-
* Accepts and returns only the literal `undefined` value.
|
|
20
|
-
*/
|
|
13
|
+
return blob === null ? ok(blob) : err('Must be null')
|
|
14
|
+
})
|
|
21
15
|
|
|
22
|
-
exports.null_ = null_
|
|
16
|
+
exports.null_ = null_
|
|
23
17
|
var undefined_ = (0, _Decoder.define)(function (blob, ok, err) {
|
|
24
|
-
return blob === undefined ? ok(blob) : err('Must be undefined')
|
|
25
|
-
})
|
|
26
|
-
exports.undefined_ = undefined_
|
|
18
|
+
return blob === undefined ? ok(blob) : err('Must be undefined')
|
|
19
|
+
})
|
|
20
|
+
exports.undefined_ = undefined_
|
|
27
21
|
var undefined_or_null = (0, _Decoder.define)(function (blob, ok, err) {
|
|
28
|
-
return blob === undefined || blob === null ? ok(blob) :
|
|
29
|
-
|
|
30
|
-
});
|
|
22
|
+
return blob === undefined || blob === null ? ok(blob) : err('Must be undefined or null')
|
|
23
|
+
})
|
|
31
24
|
|
|
32
25
|
function _maybeish(emptyCase) {
|
|
33
|
-
function _inner(decoder
|
|
34
|
-
|
|
35
|
-
) {
|
|
36
|
-
var rv = (0, _unions.either)(emptyCase, decoder);
|
|
26
|
+
function _inner(decoder) {
|
|
27
|
+
var rv = (0, _unions.either)(emptyCase, decoder)
|
|
37
28
|
|
|
38
|
-
if (
|
|
39
|
-
|
|
40
|
-
// ...then return the default value
|
|
41
|
-
var _defaultValue = arguments[1];
|
|
29
|
+
if (arguments.length >= 2) {
|
|
30
|
+
var _defaultValue = arguments[1]
|
|
42
31
|
|
|
43
|
-
var _defaultValue2 = typeof _defaultValue === 'function' ? _defaultValue() : _defaultValue
|
|
32
|
+
var _defaultValue2 = typeof _defaultValue === 'function' ? _defaultValue() : _defaultValue
|
|
44
33
|
|
|
45
34
|
return rv.transform(function (value) {
|
|
46
|
-
return value != null ? value : _defaultValue2
|
|
47
|
-
})
|
|
35
|
+
return value != null ? value : _defaultValue2
|
|
36
|
+
})
|
|
48
37
|
} else {
|
|
49
|
-
|
|
50
|
-
return rv;
|
|
38
|
+
return rv
|
|
51
39
|
}
|
|
52
40
|
}
|
|
53
41
|
|
|
54
|
-
return _inner
|
|
42
|
+
return _inner
|
|
55
43
|
}
|
|
56
|
-
/**
|
|
57
|
-
* Accepts whatever the given decoder accepts, or `null`.
|
|
58
|
-
*
|
|
59
|
-
* If a default value is explicitly provided, return that instead in the `null`
|
|
60
|
-
* case.
|
|
61
|
-
*/
|
|
62
44
|
|
|
45
|
+
var nullable = _maybeish(null_)
|
|
63
46
|
|
|
64
|
-
|
|
65
|
-
/**
|
|
66
|
-
* Accepts whatever the given decoder accepts, or `undefined`.
|
|
67
|
-
*
|
|
68
|
-
* If a default value is explicitly provided, return that instead in the
|
|
69
|
-
* `undefined` case.
|
|
70
|
-
*/
|
|
47
|
+
exports.nullable = nullable
|
|
71
48
|
|
|
49
|
+
var optional = _maybeish(undefined_)
|
|
72
50
|
|
|
73
|
-
exports.
|
|
51
|
+
exports.optional = optional
|
|
74
52
|
|
|
75
|
-
var
|
|
76
|
-
/**
|
|
77
|
-
* Accepts whatever the given decoder accepts, or `null`, or `undefined`.
|
|
78
|
-
*
|
|
79
|
-
* If a default value is explicitly provided, return that instead in the
|
|
80
|
-
* `null`/`undefined` case.
|
|
81
|
-
*/
|
|
53
|
+
var maybe = _maybeish(undefined_or_null)
|
|
82
54
|
|
|
83
|
-
|
|
84
|
-
exports.optional = optional;
|
|
85
|
-
|
|
86
|
-
var maybe = _maybeish(undefined_or_null);
|
|
87
|
-
/**
|
|
88
|
-
* Accepts only the given constant value.
|
|
89
|
-
*/
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
exports.maybe = maybe;
|
|
55
|
+
exports.maybe = maybe
|
|
93
56
|
|
|
94
57
|
function constant(value) {
|
|
95
58
|
return (0, _Decoder.define)(function (blob, ok, err) {
|
|
96
|
-
return blob === value ? ok(value) : err(
|
|
97
|
-
})
|
|
59
|
+
return blob === value ? ok(value) : err('Must be constant ' + String(value))
|
|
60
|
+
})
|
|
98
61
|
}
|
|
99
|
-
/**
|
|
100
|
-
* Accepts anything, completely ignores it, and always returns the provided
|
|
101
|
-
* value instead.
|
|
102
|
-
*
|
|
103
|
-
* This is useful to manually add extra fields to object decoders.
|
|
104
|
-
*/
|
|
105
|
-
|
|
106
62
|
|
|
107
63
|
function always(value) {
|
|
108
|
-
return (0, _Decoder.define)(
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
64
|
+
return (0, _Decoder.define)(
|
|
65
|
+
typeof value === 'function'
|
|
66
|
+
? function (
|
|
67
|
+
blob,
|
|
68
|
+
|
|
69
|
+
ok,
|
|
70
|
+
_
|
|
71
|
+
) {
|
|
72
|
+
return ok(value())
|
|
73
|
+
}
|
|
74
|
+
: function (
|
|
75
|
+
blob,
|
|
76
|
+
|
|
77
|
+
ok,
|
|
78
|
+
_
|
|
79
|
+
) {
|
|
80
|
+
return ok(value)
|
|
81
|
+
}
|
|
82
|
+
)
|
|
119
83
|
}
|
|
120
|
-
/**
|
|
121
|
-
* Alias of always.
|
|
122
|
-
*/
|
|
123
|
-
|
|
124
84
|
|
|
125
|
-
var hardcoded = always
|
|
126
|
-
/**
|
|
127
|
-
* Accepts anything and returns it unchanged.
|
|
128
|
-
*
|
|
129
|
-
* Useful for situation in which you don't know or expect a specific type. Of
|
|
130
|
-
* course, the downside is that you won't know the type of the value statically
|
|
131
|
-
* and you'll have to further refine it yourself.
|
|
132
|
-
*/
|
|
85
|
+
var hardcoded = always
|
|
133
86
|
|
|
134
|
-
exports.hardcoded = hardcoded
|
|
87
|
+
exports.hardcoded = hardcoded
|
|
135
88
|
var unknown = (0, _Decoder.define)(function (blob, ok, _) {
|
|
136
|
-
return ok(blob)
|
|
137
|
-
})
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
exports.unknown = unknown;
|
|
143
|
-
var mixed = unknown;
|
|
144
|
-
exports.mixed = mixed;
|
|
89
|
+
return ok(blob)
|
|
90
|
+
})
|
|
91
|
+
|
|
92
|
+
exports.unknown = unknown
|
|
93
|
+
var mixed = unknown
|
|
94
|
+
exports.mixed = mixed
|
package/lib/basics.mjs
CHANGED
|
@@ -1,120 +1,75 @@
|
|
|
1
|
-
import { define } from '../Decoder.mjs'
|
|
2
|
-
import { either } from './unions.mjs'
|
|
1
|
+
import { define } from '../Decoder.mjs'
|
|
2
|
+
import { either } from './unions.mjs'
|
|
3
3
|
|
|
4
|
-
/**
|
|
5
|
-
* Accepts and returns only the literal `null` value.
|
|
6
|
-
*/
|
|
7
4
|
export var null_ = define(function (blob, ok, err) {
|
|
8
|
-
return blob === null ? ok(blob) : err('Must be null')
|
|
9
|
-
})
|
|
10
|
-
/**
|
|
11
|
-
* Accepts and returns only the literal `undefined` value.
|
|
12
|
-
*/
|
|
5
|
+
return blob === null ? ok(blob) : err('Must be null')
|
|
6
|
+
})
|
|
13
7
|
|
|
14
8
|
export var undefined_ = define(function (blob, ok, err) {
|
|
15
|
-
return blob === undefined ? ok(blob) : err('Must be undefined')
|
|
16
|
-
})
|
|
9
|
+
return blob === undefined ? ok(blob) : err('Must be undefined')
|
|
10
|
+
})
|
|
17
11
|
var undefined_or_null = define(function (blob, ok, err) {
|
|
18
|
-
return blob === undefined || blob === null ? ok(blob) :
|
|
19
|
-
|
|
20
|
-
});
|
|
12
|
+
return blob === undefined || blob === null ? ok(blob) : err('Must be undefined or null')
|
|
13
|
+
})
|
|
21
14
|
|
|
22
15
|
function _maybeish(emptyCase) {
|
|
23
|
-
function _inner(decoder
|
|
24
|
-
|
|
25
|
-
) {
|
|
26
|
-
var rv = either(emptyCase, decoder);
|
|
16
|
+
function _inner(decoder) {
|
|
17
|
+
var rv = either(emptyCase, decoder)
|
|
27
18
|
|
|
28
|
-
if (
|
|
29
|
-
|
|
30
|
-
// ...then return the default value
|
|
31
|
-
var _defaultValue = arguments[1];
|
|
19
|
+
if (arguments.length >= 2) {
|
|
20
|
+
var _defaultValue = arguments[1]
|
|
32
21
|
|
|
33
|
-
var _defaultValue2 = typeof _defaultValue === 'function' ? _defaultValue() : _defaultValue
|
|
22
|
+
var _defaultValue2 = typeof _defaultValue === 'function' ? _defaultValue() : _defaultValue
|
|
34
23
|
|
|
35
24
|
return rv.transform(function (value) {
|
|
36
|
-
return value != null ? value : _defaultValue2
|
|
37
|
-
})
|
|
25
|
+
return value != null ? value : _defaultValue2
|
|
26
|
+
})
|
|
38
27
|
} else {
|
|
39
|
-
|
|
40
|
-
return rv;
|
|
28
|
+
return rv
|
|
41
29
|
}
|
|
42
30
|
}
|
|
43
31
|
|
|
44
|
-
return _inner
|
|
32
|
+
return _inner
|
|
45
33
|
}
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
export var nullable = _maybeish(null_);
|
|
55
|
-
/**
|
|
56
|
-
* Accepts whatever the given decoder accepts, or `undefined`.
|
|
57
|
-
*
|
|
58
|
-
* If a default value is explicitly provided, return that instead in the
|
|
59
|
-
* `undefined` case.
|
|
60
|
-
*/
|
|
61
|
-
|
|
62
|
-
export var optional = _maybeish(undefined_);
|
|
63
|
-
/**
|
|
64
|
-
* Accepts whatever the given decoder accepts, or `null`, or `undefined`.
|
|
65
|
-
*
|
|
66
|
-
* If a default value is explicitly provided, return that instead in the
|
|
67
|
-
* `null`/`undefined` case.
|
|
68
|
-
*/
|
|
69
|
-
|
|
70
|
-
export var maybe = _maybeish(undefined_or_null);
|
|
71
|
-
/**
|
|
72
|
-
* Accepts only the given constant value.
|
|
73
|
-
*/
|
|
34
|
+
|
|
35
|
+
export var nullable = _maybeish(null_)
|
|
36
|
+
|
|
37
|
+
export var optional = _maybeish(undefined_)
|
|
38
|
+
|
|
39
|
+
export var maybe = _maybeish(undefined_or_null)
|
|
74
40
|
|
|
75
41
|
export function constant(value) {
|
|
76
42
|
return define(function (blob, ok, err) {
|
|
77
|
-
return blob === value ? ok(value) : err(
|
|
78
|
-
})
|
|
43
|
+
return blob === value ? ok(value) : err('Must be constant ' + String(value))
|
|
44
|
+
})
|
|
79
45
|
}
|
|
80
|
-
/**
|
|
81
|
-
* Accepts anything, completely ignores it, and always returns the provided
|
|
82
|
-
* value instead.
|
|
83
|
-
*
|
|
84
|
-
* This is useful to manually add extra fields to object decoders.
|
|
85
|
-
*/
|
|
86
46
|
|
|
87
47
|
export function always(value) {
|
|
88
|
-
return define(
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
48
|
+
return define(
|
|
49
|
+
typeof value === 'function'
|
|
50
|
+
? function (
|
|
51
|
+
blob,
|
|
52
|
+
|
|
53
|
+
ok,
|
|
54
|
+
_
|
|
55
|
+
) {
|
|
56
|
+
return ok(value())
|
|
57
|
+
}
|
|
58
|
+
: function (
|
|
59
|
+
blob,
|
|
60
|
+
|
|
61
|
+
ok,
|
|
62
|
+
_
|
|
63
|
+
) {
|
|
64
|
+
return ok(value)
|
|
65
|
+
}
|
|
66
|
+
)
|
|
99
67
|
}
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
*/
|
|
103
|
-
|
|
104
|
-
export var hardcoded = always;
|
|
105
|
-
/**
|
|
106
|
-
* Accepts anything and returns it unchanged.
|
|
107
|
-
*
|
|
108
|
-
* Useful for situation in which you don't know or expect a specific type. Of
|
|
109
|
-
* course, the downside is that you won't know the type of the value statically
|
|
110
|
-
* and you'll have to further refine it yourself.
|
|
111
|
-
*/
|
|
68
|
+
|
|
69
|
+
export var hardcoded = always
|
|
112
70
|
|
|
113
71
|
export var unknown = define(function (blob, ok, _) {
|
|
114
|
-
return ok(blob)
|
|
115
|
-
})
|
|
116
|
-
/**
|
|
117
|
-
* Alias of unknown.
|
|
118
|
-
*/
|
|
72
|
+
return ok(blob)
|
|
73
|
+
})
|
|
119
74
|
|
|
120
|
-
export var mixed = unknown
|
|
75
|
+
export var mixed = unknown
|
package/lib/booleans.js
CHANGED
|
@@ -1,35 +1,25 @@
|
|
|
1
|
-
|
|
1
|
+
'use strict'
|
|
2
2
|
|
|
3
|
-
exports.__esModule = true
|
|
4
|
-
exports.truthy = exports.numericBoolean = exports[
|
|
3
|
+
exports.__esModule = true
|
|
4
|
+
exports.truthy = exports.numericBoolean = exports['boolean'] = void 0
|
|
5
5
|
|
|
6
|
-
var _Decoder = require(
|
|
6
|
+
var _Decoder = require('../Decoder')
|
|
7
7
|
|
|
8
|
-
var _numbers = require(
|
|
8
|
+
var _numbers = require('./numbers')
|
|
9
9
|
|
|
10
|
-
/**
|
|
11
|
-
* Accepts and returns booleans.
|
|
12
|
-
*/
|
|
13
10
|
var _boolean = (0, _Decoder.define)(function (blob, ok, err) {
|
|
14
|
-
return typeof blob === 'boolean' ? ok(blob) : err('Must be boolean')
|
|
15
|
-
})
|
|
16
|
-
/**
|
|
17
|
-
* Accepts anything and will return its "truth" value. Will never reject.
|
|
18
|
-
*/
|
|
11
|
+
return typeof blob === 'boolean' ? ok(blob) : err('Must be boolean')
|
|
12
|
+
})
|
|
19
13
|
|
|
20
|
-
|
|
21
|
-
exports["boolean"] = _boolean;
|
|
14
|
+
exports['boolean'] = _boolean
|
|
22
15
|
var truthy = (0, _Decoder.define)(function (blob, ok, _) {
|
|
23
|
-
return ok(!!blob)
|
|
24
|
-
})
|
|
25
|
-
/**
|
|
26
|
-
* Accepts numbers, but return their boolean representation.
|
|
27
|
-
*/
|
|
16
|
+
return ok(!!blob)
|
|
17
|
+
})
|
|
28
18
|
|
|
29
|
-
exports.truthy = truthy
|
|
19
|
+
exports.truthy = truthy
|
|
30
20
|
|
|
31
21
|
var numericBoolean = _numbers.number.transform(function (n) {
|
|
32
|
-
return !!n
|
|
33
|
-
})
|
|
22
|
+
return !!n
|
|
23
|
+
})
|
|
34
24
|
|
|
35
|
-
exports.numericBoolean = numericBoolean
|
|
25
|
+
exports.numericBoolean = numericBoolean
|
package/lib/booleans.mjs
CHANGED
|
@@ -1,25 +1,15 @@
|
|
|
1
|
-
import { define } from '../Decoder.mjs'
|
|
2
|
-
import { number } from './numbers.mjs'
|
|
1
|
+
import { define } from '../Decoder.mjs'
|
|
2
|
+
import { number } from './numbers.mjs'
|
|
3
3
|
|
|
4
|
-
/**
|
|
5
|
-
* Accepts and returns booleans.
|
|
6
|
-
*/
|
|
7
4
|
var _boolean = define(function (blob, ok, err) {
|
|
8
|
-
return typeof blob === 'boolean' ? ok(blob) : err('Must be boolean')
|
|
9
|
-
})
|
|
10
|
-
/**
|
|
11
|
-
* Accepts anything and will return its "truth" value. Will never reject.
|
|
12
|
-
*/
|
|
5
|
+
return typeof blob === 'boolean' ? ok(blob) : err('Must be boolean')
|
|
6
|
+
})
|
|
13
7
|
|
|
14
|
-
|
|
15
|
-
export { _boolean as boolean };
|
|
8
|
+
export { _boolean as boolean }
|
|
16
9
|
export var truthy = define(function (blob, ok, _) {
|
|
17
|
-
return ok(!!blob)
|
|
18
|
-
})
|
|
19
|
-
/**
|
|
20
|
-
* Accepts numbers, but return their boolean representation.
|
|
21
|
-
*/
|
|
10
|
+
return ok(!!blob)
|
|
11
|
+
})
|
|
22
12
|
|
|
23
13
|
export var numericBoolean = number.transform(function (n) {
|
|
24
|
-
return !!n
|
|
25
|
-
})
|
|
14
|
+
return !!n
|
|
15
|
+
})
|
package/lib/dates.js
CHANGED
|
@@ -1,44 +1,28 @@
|
|
|
1
|
-
|
|
1
|
+
'use strict'
|
|
2
2
|
|
|
3
|
-
exports.__esModule = true
|
|
4
|
-
exports.iso8601 = exports.date = void 0
|
|
3
|
+
exports.__esModule = true
|
|
4
|
+
exports.iso8601 = exports.date = void 0
|
|
5
5
|
|
|
6
|
-
var _utils = require(
|
|
6
|
+
var _utils = require('../_utils')
|
|
7
7
|
|
|
8
|
-
var _Decoder = require(
|
|
8
|
+
var _Decoder = require('../Decoder')
|
|
9
9
|
|
|
10
|
-
var _strings = require(
|
|
11
|
-
|
|
12
|
-
// Only matches the shape. This "over-matches" some values that still aren't
|
|
13
|
-
// valid dates (like 9999-99-99), but those will be caught by JS Date's
|
|
14
|
-
// internal validations
|
|
15
|
-
var iso8601_re = /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(?:[.]\d+)?(?:Z|[+-]\d{2}:?\d{2})$/;
|
|
16
|
-
/**
|
|
17
|
-
* Accepts and returns `Date` instances.
|
|
18
|
-
*/
|
|
10
|
+
var _strings = require('./strings')
|
|
11
|
+
var iso8601_re = /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(?:[.]\d+)?(?:Z|[+-]\d{2}:?\d{2})$/
|
|
19
12
|
|
|
20
13
|
var date = (0, _Decoder.define)(function (blob, ok, err) {
|
|
21
|
-
var date = (0, _utils.asDate)(blob)
|
|
22
|
-
return date !== null ? ok(date) : err('Must be a Date')
|
|
23
|
-
})
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
* This is very useful for working with dates in APIs: serialize them as
|
|
29
|
-
* `.toISOString()` when sending, decode them with `iso8601` when receiving.
|
|
30
|
-
*/
|
|
31
|
-
|
|
32
|
-
exports.date = date;
|
|
33
|
-
var iso8601 = // Input itself needs to match the ISO8601 regex...
|
|
34
|
-
(0, _strings.regex)(iso8601_re, 'Must be ISO8601 format').transform( // Make sure it is a _valid_ date
|
|
35
|
-
function (value) {
|
|
36
|
-
var date = new Date(value);
|
|
14
|
+
var date = (0, _utils.asDate)(blob)
|
|
15
|
+
return date !== null ? ok(date) : err('Must be a Date')
|
|
16
|
+
})
|
|
17
|
+
|
|
18
|
+
exports.date = date
|
|
19
|
+
var iso8601 = (0, _strings.regex)(iso8601_re, 'Must be ISO8601 format').transform(function (value) {
|
|
20
|
+
var date = new Date(value)
|
|
37
21
|
|
|
38
22
|
if (isNaN(date.getTime())) {
|
|
39
|
-
throw new Error('Must be valid date/time value')
|
|
23
|
+
throw new Error('Must be valid date/time value')
|
|
40
24
|
}
|
|
41
25
|
|
|
42
|
-
return date
|
|
43
|
-
})
|
|
44
|
-
exports.iso8601 = iso8601
|
|
26
|
+
return date
|
|
27
|
+
})
|
|
28
|
+
exports.iso8601 = iso8601
|