decoders 1.26.0-beta2 → 2.0.0-beta11

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 (128) hide show
  1. package/CHANGELOG.md +52 -6
  2. package/Decoder.d.ts +28 -0
  3. package/Decoder.js +213 -0
  4. package/Decoder.js.flow +238 -0
  5. package/Decoder.mjs +206 -0
  6. package/NotSupportedTSVersion.d.ts +1 -0
  7. package/README.md +105 -961
  8. package/_utils.d.ts +9 -0
  9. package/_utils.js +102 -0
  10. package/_utils.js.flow +93 -0
  11. package/_utils.mjs +89 -0
  12. package/annotate.d.ts +62 -0
  13. package/annotate.js +161 -0
  14. package/annotate.js.flow +218 -0
  15. package/annotate.mjs +144 -0
  16. package/format.d.ts +4 -0
  17. package/format.js +151 -0
  18. package/format.js.flow +126 -0
  19. package/format.mjs +140 -0
  20. package/index.d.ts +40 -0
  21. package/index.js +90 -0
  22. package/index.js.flow +44 -0
  23. package/index.mjs +11 -0
  24. package/{ts/helpers.d.ts → lib/_helpers.d.ts} +0 -0
  25. package/lib/arrays.d.ts +37 -0
  26. package/lib/arrays.js +138 -0
  27. package/lib/arrays.js.flow +138 -0
  28. package/lib/arrays.mjs +123 -0
  29. package/lib/basics.d.ts +14 -0
  30. package/lib/basics.js +109 -0
  31. package/lib/basics.js.flow +85 -0
  32. package/lib/basics.mjs +85 -0
  33. package/{ts/boolean.d.ts → lib/booleans.d.ts} +1 -1
  34. package/lib/booleans.js +35 -0
  35. package/lib/booleans.js.flow +22 -0
  36. package/lib/booleans.mjs +25 -0
  37. package/{ts/date.d.ts → lib/dates.d.ts} +1 -1
  38. package/lib/dates.js +44 -0
  39. package/lib/dates.js.flow +40 -0
  40. package/lib/dates.mjs +34 -0
  41. package/{ts → lib}/json.d.ts +1 -1
  42. package/lib/json.js +55 -0
  43. package/lib/json.js.flow +50 -0
  44. package/lib/json.mjs +40 -0
  45. package/{ts/number.d.ts → lib/numbers.d.ts} +2 -1
  46. package/lib/numbers.js +52 -0
  47. package/lib/numbers.js.flow +49 -0
  48. package/lib/numbers.mjs +42 -0
  49. package/{ts/object.d.ts → lib/objects.d.ts} +11 -6
  50. package/lib/objects.js +240 -0
  51. package/lib/objects.js.flow +246 -0
  52. package/lib/objects.mjs +223 -0
  53. package/lib/strings.d.ts +13 -0
  54. package/lib/strings.js +101 -0
  55. package/lib/strings.js.flow +90 -0
  56. package/lib/strings.mjs +82 -0
  57. package/lib/unions.d.ts +78 -0
  58. package/lib/unions.js +161 -0
  59. package/lib/unions.js.flow +158 -0
  60. package/lib/unions.mjs +145 -0
  61. package/lib/utilities.d.ts +10 -0
  62. package/lib/utilities.js +94 -0
  63. package/lib/utilities.js.flow +84 -0
  64. package/lib/utilities.mjs +79 -0
  65. package/package.json +79 -28
  66. package/result.d.ts +16 -0
  67. package/result.js +34 -0
  68. package/result.js.flow +26 -0
  69. package/result.mjs +27 -0
  70. package/cjs/array.js +0 -133
  71. package/cjs/array.js.flow +0 -106
  72. package/cjs/boolean.js +0 -42
  73. package/cjs/boolean.js.flow +0 -28
  74. package/cjs/constants.js +0 -67
  75. package/cjs/constants.js.flow +0 -45
  76. package/cjs/date.js +0 -42
  77. package/cjs/date.js.flow +0 -38
  78. package/cjs/describe.js +0 -22
  79. package/cjs/describe.js.flow +0 -17
  80. package/cjs/dispatch.js +0 -58
  81. package/cjs/dispatch.js.flow +0 -57
  82. package/cjs/either.js +0 -85
  83. package/cjs/either.js.flow +0 -131
  84. package/cjs/fail.js +0 -19
  85. package/cjs/fail.js.flow +0 -13
  86. package/cjs/guard.js +0 -30
  87. package/cjs/guard.js.flow +0 -36
  88. package/cjs/index.js +0 -397
  89. package/cjs/index.js.flow +0 -56
  90. package/cjs/instanceOf.js +0 -17
  91. package/cjs/instanceOf.js.flow +0 -21
  92. package/cjs/json.js +0 -33
  93. package/cjs/json.js.flow +0 -28
  94. package/cjs/lazy.js +0 -18
  95. package/cjs/lazy.js.flow +0 -15
  96. package/cjs/mapping.js +0 -113
  97. package/cjs/mapping.js.flow +0 -71
  98. package/cjs/number.js +0 -38
  99. package/cjs/number.js.flow +0 -35
  100. package/cjs/object.js +0 -254
  101. package/cjs/object.js.flow +0 -211
  102. package/cjs/optional.js +0 -52
  103. package/cjs/optional.js.flow +0 -42
  104. package/cjs/string.js +0 -93
  105. package/cjs/string.js.flow +0 -81
  106. package/cjs/tuple.js +0 -199
  107. package/cjs/tuple.js.flow +0 -221
  108. package/cjs/types.js +0 -3
  109. package/cjs/types.js.flow +0 -26
  110. package/cjs/utils.js +0 -70
  111. package/cjs/utils.js.flow +0 -58
  112. package/es/index.js +0 -1039
  113. package/ts/array.d.ts +0 -5
  114. package/ts/constants.d.ts +0 -11
  115. package/ts/describe.d.ts +0 -3
  116. package/ts/dispatch.d.ts +0 -8
  117. package/ts/either.d.ts +0 -61
  118. package/ts/fail.d.ts +0 -3
  119. package/ts/guard.d.ts +0 -7
  120. package/ts/index.d.ts +0 -38
  121. package/ts/instanceOf.d.ts +0 -3
  122. package/ts/lazy.d.ts +0 -3
  123. package/ts/mapping.d.ts +0 -4
  124. package/ts/optional.d.ts +0 -5
  125. package/ts/string.d.ts +0 -7
  126. package/ts/tuple.d.ts +0 -30
  127. package/ts/types.d.ts +0 -18
  128. package/ts/utils.d.ts +0 -13
package/lib/arrays.mjs ADDED
@@ -0,0 +1,123 @@
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
+ */
9
+ export var poja = define(function (blob, ok, err) {
10
+ if (!Array.isArray(blob)) {
11
+ return err('Must be an array');
12
+ }
13
+
14
+ return ok( // NOTE: Since Flow 0.98, Array.isArray() returns $ReadOnlyArray<mixed>
15
+ // instead of Array<mixed>. For rationale, see
16
+ // https://github.com/facebook/flow/issues/7684. In this case, we
17
+ // don't want to output read-only types because it's up to the user of
18
+ // decoders to determine what they want to do with the decoded output.
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 = [];
35
+
36
+ for (var index = 0; index < items.length; ++index) {
37
+ var result = items[index];
38
+
39
+ if (result.ok) {
40
+ results.push(result.value);
41
+ } else {
42
+ var ann = result.error; // Rewrite the annotation to include the index information, and inject it into the original blob
43
+
44
+ var clone = [].concat(blobs);
45
+ clone.splice(index, 1, annotate(ann, ann.text ? ann.text + " (at index " + index + ")" : "index " + index));
46
+ return err(annotate(clone));
47
+ }
48
+ }
49
+
50
+ return ok(results);
51
+ }
52
+ /**
53
+ * Accepts arrays of whatever the given decoder accepts.
54
+ */
55
+
56
+
57
+ export function array(decoder) {
58
+ return poja.then(function (blobs, ok, err) {
59
+ var results = blobs.map(decoder.decode);
60
+ return all(results, blobs, ok, err);
61
+ });
62
+ }
63
+ /**
64
+ * Like `array()`, but will reject arrays with 0 elements.
65
+ */
66
+
67
+ export function nonEmptyArray(decoder) {
68
+ return array(decoder).refine(function (arr) {
69
+ return arr.length > 0;
70
+ }, 'Must be non-empty array');
71
+ }
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
+
77
+ export function set(decoder) {
78
+ return array(decoder).transform(function (items) {
79
+ return new Set(items);
80
+ });
81
+ }
82
+
83
+ var ntuple = function ntuple(n) {
84
+ return poja.refine(function (arr) {
85
+ return arr.length === n;
86
+ }, "Must be a " + n + "-tuple");
87
+ }; // prettier-ignore
88
+
89
+
90
+ /**
91
+ * Accepts a tuple (an array with exactly _n_ items) of values accepted by the
92
+ * _n_ given decoders.
93
+ */
94
+ function _tuple() {
95
+ for (var _len = arguments.length, decoders = new Array(_len), _key = 0; _key < _len; _key++) {
96
+ decoders[_key] = arguments[_key];
97
+ }
98
+
99
+ return ntuple(decoders.length).then(function (blobs, ok, err) {
100
+ var allOk = true;
101
+ var rvs = decoders.map(function (decoder, i) {
102
+ var blob = blobs[i];
103
+ var result = decoder.decode(blob);
104
+
105
+ if (result.ok) {
106
+ return result.value;
107
+ } else {
108
+ allOk = false;
109
+ return result.error;
110
+ }
111
+ });
112
+
113
+ if (allOk) {
114
+ return ok(rvs);
115
+ } else {
116
+ // If a decoder error has happened while unwrapping all the
117
+ // results, try to construct a good error message
118
+ return err(annotate(rvs));
119
+ }
120
+ });
121
+ }
122
+
123
+ export var tuple = _tuple;
@@ -0,0 +1,14 @@
1
+ import { Decoder, Scalar } from '../Decoder';
2
+
3
+ export const null_: Decoder<null>;
4
+ export const undefined_: Decoder<undefined>;
5
+ export function optional<T>(decoder: Decoder<T>): Decoder<T | undefined>;
6
+ export function nullable<T>(decoder: Decoder<T>): Decoder<T | null>;
7
+ export function maybe<T>(decoder: Decoder<T>): Decoder<T | null | undefined>;
8
+ export function constant<T extends Scalar>(value: T): Decoder<T>;
9
+ export function always<T extends Scalar>(value: T): Decoder<T>;
10
+ export function always<T>(value: T): Decoder<T>;
11
+ export function hardcoded<T extends Scalar>(value: T): Decoder<T>;
12
+ export function hardcoded<T>(value: T): Decoder<T>;
13
+ export const mixed: Decoder<unknown>;
14
+ export const unknown: Decoder<unknown>;
package/lib/basics.js ADDED
@@ -0,0 +1,109 @@
1
+ "use strict";
2
+
3
+ exports.__esModule = true;
4
+ exports.always = always;
5
+ exports.constant = constant;
6
+ exports.hardcoded = void 0;
7
+ exports.maybe = maybe;
8
+ exports.null_ = exports.mixed = void 0;
9
+ exports.nullable = nullable;
10
+ exports.optional = optional;
11
+ exports.unknown = exports.undefined_ = void 0;
12
+
13
+ var _Decoder = require("../Decoder");
14
+
15
+ var _unions = require("./unions");
16
+
17
+ /**
18
+ * Accepts and returns only the literal `null` value.
19
+ */
20
+ var null_ = (0, _Decoder.define)(function (blob, ok, err) {
21
+ return blob === null ? ok(blob) : err('Must be null');
22
+ });
23
+ /**
24
+ * Accepts and returns only the literal `undefined` value.
25
+ */
26
+
27
+ exports.null_ = null_;
28
+ var undefined_ = (0, _Decoder.define)(function (blob, ok, err) {
29
+ return blob === undefined ? ok(blob) : err('Must be undefined');
30
+ });
31
+ /**
32
+ * Accepts whatever the given decoder accepts, or `undefined`.
33
+ */
34
+
35
+ exports.undefined_ = undefined_;
36
+
37
+ function optional(decoder) {
38
+ return (0, _unions.either)(undefined_, decoder);
39
+ }
40
+ /**
41
+ * Accepts whatever the given decoder accepts, or `null`.
42
+ */
43
+
44
+
45
+ function nullable(decoder) {
46
+ return (0, _unions.either)(null_, decoder);
47
+ }
48
+
49
+ var undefined_or_null = (0, _Decoder.define)(function (blob, ok, err) {
50
+ return blob === undefined || blob === null ? ok(blob) : // Combine error message into a single line for readability
51
+ err('Must be undefined or null');
52
+ });
53
+ /**
54
+ * Accepts whatever the given decoder accepts, or `null`, or `undefined`.
55
+ */
56
+
57
+ function maybe(decoder) {
58
+ return (0, _unions.either)(undefined_or_null, decoder);
59
+ }
60
+ /**
61
+ * Accepts only the given constant value.
62
+ */
63
+
64
+
65
+ function constant(value) {
66
+ return (0, _Decoder.define)(function (blob, ok, err) {
67
+ return blob === value ? ok(value) : err("Must be constant " + String(value));
68
+ });
69
+ }
70
+ /**
71
+ * Accepts anything, completely ignores it, and always returns the provided
72
+ * value instead.
73
+ *
74
+ * This is useful to manually add extra fields to object decoders.
75
+ */
76
+
77
+
78
+ function always(value) {
79
+ return (0, _Decoder.define)(function (blob
80
+ /* ignored */
81
+ , ok, _) {
82
+ return ok(value);
83
+ });
84
+ }
85
+ /**
86
+ * Alias of always.
87
+ */
88
+
89
+
90
+ var hardcoded = always;
91
+ /**
92
+ * Accepts anything and returns it unchanged.
93
+ *
94
+ * Useful for situation in which you don't know or expect a specific type. Of
95
+ * course, the downside is that you won't know the type of the value statically
96
+ * and you'll have to further refine it yourself.
97
+ */
98
+
99
+ exports.hardcoded = hardcoded;
100
+ var unknown = (0, _Decoder.define)(function (blob, ok, _) {
101
+ return ok(blob);
102
+ });
103
+ /**
104
+ * Alias of unknown.
105
+ */
106
+
107
+ exports.unknown = unknown;
108
+ var mixed = unknown;
109
+ exports.mixed = mixed;
@@ -0,0 +1,85 @@
1
+ // @flow strict
2
+
3
+ import { define } from '../Decoder';
4
+ import { either } from './unions';
5
+ import type { Decoder, Scalar } from '../Decoder';
6
+
7
+ /**
8
+ * Accepts and returns only the literal `null` value.
9
+ */
10
+ export const null_: Decoder<null> = define((blob, ok, err) =>
11
+ blob === null ? ok(blob) : err('Must be null'),
12
+ );
13
+
14
+ /**
15
+ * Accepts and returns only the literal `undefined` value.
16
+ */
17
+ export const undefined_: Decoder<void> = define((blob, ok, err) =>
18
+ blob === undefined ? ok(blob) : err('Must be undefined'),
19
+ );
20
+
21
+ /**
22
+ * Accepts whatever the given decoder accepts, or `undefined`.
23
+ */
24
+ export function optional<T>(decoder: Decoder<T>): Decoder<void | T> {
25
+ return either(undefined_, decoder);
26
+ }
27
+
28
+ /**
29
+ * Accepts whatever the given decoder accepts, or `null`.
30
+ */
31
+ export function nullable<T>(decoder: Decoder<T>): Decoder<null | T> {
32
+ return either(null_, decoder);
33
+ }
34
+
35
+ const undefined_or_null: Decoder<null | void> = define((blob, ok, err) =>
36
+ blob === undefined || blob === null
37
+ ? ok(blob)
38
+ : // Combine error message into a single line for readability
39
+ err('Must be undefined or null'),
40
+ );
41
+
42
+ /**
43
+ * Accepts whatever the given decoder accepts, or `null`, or `undefined`.
44
+ */
45
+ export function maybe<T>(decoder: Decoder<T>): Decoder<T | null | void> {
46
+ return either(undefined_or_null, decoder);
47
+ }
48
+
49
+ /**
50
+ * Accepts only the given constant value.
51
+ */
52
+ export function constant<T: Scalar>(value: T): Decoder<T> {
53
+ return define((blob, ok, err) =>
54
+ blob === value ? ok(value) : err(`Must be constant ${String(value)}`),
55
+ );
56
+ }
57
+
58
+ /**
59
+ * Accepts anything, completely ignores it, and always returns the provided
60
+ * value instead.
61
+ *
62
+ * This is useful to manually add extra fields to object decoders.
63
+ */
64
+ export function always<T>(value: T): Decoder<T> {
65
+ return define((blob /* ignored */, ok, _) => ok(value));
66
+ }
67
+
68
+ /**
69
+ * Alias of always.
70
+ */
71
+ export const hardcoded: <T>(T) => Decoder<T> = always;
72
+
73
+ /**
74
+ * Accepts anything and returns it unchanged.
75
+ *
76
+ * Useful for situation in which you don't know or expect a specific type. Of
77
+ * course, the downside is that you won't know the type of the value statically
78
+ * and you'll have to further refine it yourself.
79
+ */
80
+ export const unknown: Decoder<mixed> = define((blob, ok, _) => ok(blob));
81
+
82
+ /**
83
+ * Alias of unknown.
84
+ */
85
+ export const mixed: Decoder<mixed> = unknown;
package/lib/basics.mjs ADDED
@@ -0,0 +1,85 @@
1
+ import { define } from '../Decoder.mjs';
2
+ import { either } from './unions.mjs';
3
+
4
+ /**
5
+ * Accepts and returns only the literal `null` value.
6
+ */
7
+ 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
+ */
13
+
14
+ export var undefined_ = define(function (blob, ok, err) {
15
+ return blob === undefined ? ok(blob) : err('Must be undefined');
16
+ });
17
+ /**
18
+ * Accepts whatever the given decoder accepts, or `undefined`.
19
+ */
20
+
21
+ export function optional(decoder) {
22
+ return either(undefined_, decoder);
23
+ }
24
+ /**
25
+ * Accepts whatever the given decoder accepts, or `null`.
26
+ */
27
+
28
+ export function nullable(decoder) {
29
+ return either(null_, decoder);
30
+ }
31
+ var undefined_or_null = define(function (blob, ok, err) {
32
+ return blob === undefined || blob === null ? ok(blob) : // Combine error message into a single line for readability
33
+ err('Must be undefined or null');
34
+ });
35
+ /**
36
+ * Accepts whatever the given decoder accepts, or `null`, or `undefined`.
37
+ */
38
+
39
+ export function maybe(decoder) {
40
+ return either(undefined_or_null, decoder);
41
+ }
42
+ /**
43
+ * Accepts only the given constant value.
44
+ */
45
+
46
+ export function constant(value) {
47
+ return define(function (blob, ok, err) {
48
+ return blob === value ? ok(value) : err("Must be constant " + String(value));
49
+ });
50
+ }
51
+ /**
52
+ * Accepts anything, completely ignores it, and always returns the provided
53
+ * value instead.
54
+ *
55
+ * This is useful to manually add extra fields to object decoders.
56
+ */
57
+
58
+ export function always(value) {
59
+ return define(function (blob
60
+ /* ignored */
61
+ , ok, _) {
62
+ return ok(value);
63
+ });
64
+ }
65
+ /**
66
+ * Alias of always.
67
+ */
68
+
69
+ export var hardcoded = always;
70
+ /**
71
+ * Accepts anything and returns it unchanged.
72
+ *
73
+ * Useful for situation in which you don't know or expect a specific type. Of
74
+ * course, the downside is that you won't know the type of the value statically
75
+ * and you'll have to further refine it yourself.
76
+ */
77
+
78
+ export var unknown = define(function (blob, ok, _) {
79
+ return ok(blob);
80
+ });
81
+ /**
82
+ * Alias of unknown.
83
+ */
84
+
85
+ export var mixed = unknown;
@@ -1,4 +1,4 @@
1
- import { Decoder } from './types';
1
+ import { Decoder } from '../Decoder';
2
2
 
3
3
  export const boolean: Decoder<boolean>;
4
4
  export const truthy: Decoder<boolean>;
@@ -0,0 +1,35 @@
1
+ "use strict";
2
+
3
+ exports.__esModule = true;
4
+ exports.truthy = exports.numericBoolean = exports["boolean"] = void 0;
5
+
6
+ var _Decoder = require("../Decoder");
7
+
8
+ var _numbers = require("./numbers");
9
+
10
+ /**
11
+ * Accepts and returns booleans.
12
+ */
13
+ 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
+ */
19
+
20
+
21
+ exports["boolean"] = _boolean;
22
+ var truthy = (0, _Decoder.define)(function (blob, ok, _) {
23
+ return ok(!!blob);
24
+ });
25
+ /**
26
+ * Accepts numbers, but return their boolean representation.
27
+ */
28
+
29
+ exports.truthy = truthy;
30
+
31
+ var numericBoolean = _numbers.number.transform(function (n) {
32
+ return !!n;
33
+ });
34
+
35
+ exports.numericBoolean = numericBoolean;
@@ -0,0 +1,22 @@
1
+ // @flow strict
2
+
3
+ import { define } from '../Decoder';
4
+ import { number } from './numbers';
5
+ import type { Decoder } from '../Decoder';
6
+
7
+ /**
8
+ * Accepts and returns booleans.
9
+ */
10
+ export const boolean: Decoder<boolean> = define((blob, ok, err) => {
11
+ return typeof blob === 'boolean' ? ok(blob) : err('Must be boolean');
12
+ });
13
+
14
+ /**
15
+ * Accepts anything and will return its “truth” value. Will never reject.
16
+ */
17
+ export const truthy: Decoder<boolean> = define((blob, ok, _) => ok(!!blob));
18
+
19
+ /**
20
+ * Accepts numbers, but return their boolean representation.
21
+ */
22
+ export const numericBoolean: Decoder<boolean> = number.transform((n) => !!n);
@@ -0,0 +1,25 @@
1
+ import { define } from '../Decoder.mjs';
2
+ import { number } from './numbers.mjs';
3
+
4
+ /**
5
+ * Accepts and returns booleans.
6
+ */
7
+ 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
+ */
13
+
14
+
15
+ export { _boolean as boolean };
16
+ export var truthy = define(function (blob, ok, _) {
17
+ return ok(!!blob);
18
+ });
19
+ /**
20
+ * Accepts numbers, but return their boolean representation.
21
+ */
22
+
23
+ export var numericBoolean = number.transform(function (n) {
24
+ return !!n;
25
+ });
@@ -1,4 +1,4 @@
1
- import { Decoder } from './types';
1
+ import { Decoder } from '../Decoder';
2
2
 
3
3
  export const date: Decoder<Date>;
4
4
  export const iso8601: Decoder<Date>;
package/lib/dates.js ADDED
@@ -0,0 +1,44 @@
1
+ "use strict";
2
+
3
+ exports.__esModule = true;
4
+ exports.iso8601 = exports.date = void 0;
5
+
6
+ var _utils = require("../_utils");
7
+
8
+ var _Decoder = require("../Decoder");
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
+ */
19
+
20
+ 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 then 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);
37
+
38
+ if (isNaN(date.getTime())) {
39
+ throw new Error('Must be valid date/time value');
40
+ }
41
+
42
+ return date;
43
+ });
44
+ exports.iso8601 = iso8601;
@@ -0,0 +1,40 @@
1
+ // @flow strict
2
+
3
+ import { asDate } from '../_utils';
4
+ import { define } from '../Decoder';
5
+ import { regex } from './strings';
6
+ import type { Decoder } from '../Decoder';
7
+
8
+ // Only matches the shape. This "over-matches" some values that still aren't
9
+ // valid dates (like 9999-99-99), but those will be caught by JS Date's
10
+ // internal validations
11
+ const iso8601_re =
12
+ /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(?:[.]\d+)?(?:Z|[+-]\d{2}:?\d{2})$/;
13
+
14
+ /**
15
+ * Accepts and returns `Date` instances.
16
+ */
17
+ export const date: Decoder<Date> = define((blob, ok, err) => {
18
+ const date = asDate(blob);
19
+ return date !== null ? ok(date) : err('Must be a Date');
20
+ });
21
+
22
+ /**
23
+ * Accepts [ISO8601](https://en.wikipedia.org/wiki/ISO_8601)-formatted strings,
24
+ * returns then as `Date` instances.
25
+ *
26
+ * This is very useful for working with dates in APIs: serialize them as
27
+ * `.toISOString()` when sending, decode them with `iso8601` when receiving.
28
+ */
29
+ export const iso8601: Decoder<Date> =
30
+ // Input itself needs to match the ISO8601 regex...
31
+ regex(iso8601_re, 'Must be ISO8601 format').transform(
32
+ // Make sure it is a _valid_ date
33
+ (value: string) => {
34
+ const date = new Date(value);
35
+ if (isNaN(date.getTime())) {
36
+ throw new Error('Must be valid date/time value');
37
+ }
38
+ return date;
39
+ },
40
+ );
package/lib/dates.mjs ADDED
@@ -0,0 +1,34 @@
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
+ */
11
+
12
+ 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 then 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
+ */
23
+
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);
28
+
29
+ if (isNaN(date.getTime())) {
30
+ throw new Error('Must be valid date/time value');
31
+ }
32
+
33
+ return date;
34
+ });
@@ -1,4 +1,4 @@
1
- import { Decoder } from './types';
1
+ import { Decoder } from '../Decoder';
2
2
 
3
3
  export type JSONValue = null | string | number | boolean | JSONObject | JSONArray;
4
4
  export interface JSONObject {