decoders 2.0.0 → 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/lib/basics.js CHANGED
@@ -1,144 +1,94 @@
1
- "use strict";
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("../Decoder");
8
+ var _Decoder = require('../Decoder')
9
9
 
10
- var _unions = require("./unions");
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) : // Combine error message into a single line for readability
29
- err('Must be undefined or null');
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
- /* defaultValue */
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 ( // If a default value is provided...
39
- arguments.length >= 2) {
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
- // Otherwise the "normal" empty case
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
- var nullable = _maybeish(null_);
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.nullable = nullable;
51
+ exports.optional = optional
74
52
 
75
- var optional = _maybeish(undefined_);
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("Must be constant " + String(value));
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)(typeof value === 'function' ? function (blob
109
- /* ignored */
110
- , ok, _) {
111
- return (// $FlowFixMe[incompatible-use]
112
- ok(value())
113
- );
114
- } : function (blob
115
- /* ignored */
116
- , ok, _) {
117
- return ok(value);
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
- * Alias of unknown.
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) : // Combine error message into a single line for readability
19
- err('Must be undefined or null');
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
- /* defaultValue */
25
- ) {
26
- var rv = either(emptyCase, decoder);
16
+ function _inner(decoder) {
17
+ var rv = either(emptyCase, decoder)
27
18
 
28
- if ( // If a default value is provided...
29
- arguments.length >= 2) {
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
- // Otherwise the "normal" empty case
40
- return rv;
28
+ return rv
41
29
  }
42
30
  }
43
31
 
44
- return _inner;
32
+ return _inner
45
33
  }
46
- /**
47
- * Accepts whatever the given decoder accepts, or `null`.
48
- *
49
- * If a default value is explicitly provided, return that instead in the `null`
50
- * case.
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("Must be constant " + String(value));
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(typeof value === 'function' ? function (blob
89
- /* ignored */
90
- , ok, _) {
91
- return (// $FlowFixMe[incompatible-use]
92
- ok(value())
93
- );
94
- } : function (blob
95
- /* ignored */
96
- , ok, _) {
97
- return ok(value);
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
- * Alias of always.
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
- "use strict";
1
+ 'use strict'
2
2
 
3
- exports.__esModule = true;
4
- exports.truthy = exports.numericBoolean = exports["boolean"] = void 0;
3
+ exports.__esModule = true
4
+ exports.truthy = exports.numericBoolean = exports['boolean'] = void 0
5
5
 
6
- var _Decoder = require("../Decoder");
6
+ var _Decoder = require('../Decoder')
7
7
 
8
- var _numbers = require("./numbers");
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
- "use strict";
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("../_utils");
6
+ var _utils = require('../_utils')
7
7
 
8
- var _Decoder = require("../Decoder");
8
+ var _Decoder = require('../Decoder')
9
9
 
10
- var _strings = require("./strings");
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
- * Accepts [ISO8601](https://en.wikipedia.org/wiki/ISO_8601)-formatted strings,
26
- * returns them as `Date` instances.
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
package/lib/dates.mjs CHANGED
@@ -1,34 +1,19 @@
1
- import { asDate } from '../_utils.mjs';
2
- import { define } from '../Decoder.mjs';
3
- import { regex } from './strings.mjs';
4
- // Only matches the shape. This "over-matches" some values that still aren't
5
- // valid dates (like 9999-99-99), but those will be caught by JS Date's
6
- // internal validations
7
- var iso8601_re = /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(?:[.]\d+)?(?:Z|[+-]\d{2}:?\d{2})$/;
8
- /**
9
- * Accepts and returns `Date` instances.
10
- */
1
+ import { asDate } from '../_utils.mjs'
2
+ import { define } from '../Decoder.mjs'
3
+ import { regex } from './strings.mjs'
4
+ var iso8601_re = /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(?:[.]\d+)?(?:Z|[+-]\d{2}:?\d{2})$/
11
5
 
12
6
  export var date = define(function (blob, ok, err) {
13
- var date = asDate(blob);
14
- return date !== null ? ok(date) : err('Must be a Date');
15
- });
16
- /**
17
- * Accepts [ISO8601](https://en.wikipedia.org/wiki/ISO_8601)-formatted strings,
18
- * returns them as `Date` instances.
19
- *
20
- * This is very useful for working with dates in APIs: serialize them as
21
- * `.toISOString()` when sending, decode them with `iso8601` when receiving.
22
- */
7
+ var date = asDate(blob)
8
+ return date !== null ? ok(date) : err('Must be a Date')
9
+ })
23
10
 
24
- export var iso8601 = // Input itself needs to match the ISO8601 regex...
25
- regex(iso8601_re, 'Must be ISO8601 format').transform( // Make sure it is a _valid_ date
26
- function (value) {
27
- var date = new Date(value);
11
+ export var iso8601 = regex(iso8601_re, 'Must be ISO8601 format').transform(function (value) {
12
+ var date = new Date(value)
28
13
 
29
14
  if (isNaN(date.getTime())) {
30
- throw new Error('Must be valid date/time value');
15
+ throw new Error('Must be valid date/time value')
31
16
  }
32
17
 
33
- return date;
34
- });
18
+ return date
19
+ })
package/lib/json.d.ts CHANGED
@@ -2,7 +2,7 @@ import { Decoder } from '../Decoder';
2
2
 
3
3
  export type JSONValue = null | string | number | boolean | JSONObject | JSONArray;
4
4
  export interface JSONObject {
5
- [key: string]: JSONValue;
5
+ [key: string]: JSONValue | undefined;
6
6
  }
7
7
  export type JSONArray = JSONValue[];
8
8
 
@@ -25,11 +25,11 @@ export type JSONArray = JSONValue[];
25
25
  export const json: Decoder<JSONValue>;
26
26
 
27
27
  /**
28
- * Like `json`, but will only decode when the JSON value is an array.
28
+ * Accepts arrays that contain only valid JSON values.
29
29
  */
30
30
  export const jsonArray: Decoder<JSONArray>;
31
31
 
32
32
  /**
33
- * Like `json`, but will only decode when the JSON value is an object.
33
+ * Accepts objects that contain only valid JSON values.
34
34
  */
35
35
  export const jsonObject: Decoder<JSONObject>;
package/lib/json.js CHANGED
@@ -1,55 +1,33 @@
1
- "use strict";
1
+ 'use strict'
2
2
 
3
- exports.__esModule = true;
4
- exports.jsonObject = exports.jsonArray = exports.json = void 0;
3
+ exports.__esModule = true
4
+ exports.jsonObject = exports.jsonArray = exports.json = void 0
5
5
 
6
- var _arrays = require("./arrays");
6
+ var _arrays = require('./arrays')
7
7
 
8
- var _booleans = require("./booleans");
8
+ var _booleans = require('./booleans')
9
9
 
10
- var _objects = require("./objects");
10
+ var _objects = require('./objects')
11
11
 
12
- var _unions = require("./unions");
12
+ var _unions = require('./unions')
13
13
 
14
- var _utilities = require("./utilities");
14
+ var _utilities = require('./utilities')
15
15
 
16
- var _basics = require("./basics");
16
+ var _basics = require('./basics')
17
17
 
18
- var _numbers = require("./numbers");
18
+ var _numbers = require('./numbers')
19
19
 
20
- var _strings = require("./strings");
20
+ var _strings = require('./strings')
21
21
 
22
- /**
23
- * Like `json`, but will only decode when the JSON value is an object.
24
- */
25
22
  var jsonObject = (0, _utilities.lazy)(function () {
26
- return (0, _objects.dict)(json);
27
- });
28
- /**
29
- * Like `json`, but will only decode when the JSON value is an array.
30
- */
23
+ return (0, _objects.dict)(json)
24
+ })
31
25
 
32
- exports.jsonObject = jsonObject;
26
+ exports.jsonObject = jsonObject
33
27
  var jsonArray = (0, _utilities.lazy)(function () {
34
- return (0, _arrays.array)(json);
35
- });
36
- /**
37
- * Accepts any value that's a valid JSON value.
38
- *
39
- * In other words: any value returned by `JSON.parse()` should decode without
40
- * failure.
41
- *
42
- * ```typescript
43
- * type JSONValue =
44
- * | null
45
- * | string
46
- * | number
47
- * | boolean
48
- * | { [string]: JSONValue }
49
- * | JSONValue[]
50
- * ```
51
- */
52
-
53
- exports.jsonArray = jsonArray;
54
- var json = (0, _unions.either)(_basics.null_, _strings.string, _numbers.number, _booleans["boolean"], jsonObject, jsonArray).describe('Must be valid JSON value');
55
- exports.json = json;
28
+ return (0, _arrays.array)(json)
29
+ })
30
+
31
+ exports.jsonArray = jsonArray
32
+ var json = (0, _unions.either)(_basics.null_, _strings.string, _numbers.number, _booleans['boolean'], jsonObject, jsonArray).describe('Must be valid JSON value')
33
+ exports.json = json