decoders 2.0.0-beta8 → 2.0.0-beta9

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 (56) hide show
  1. package/CHANGELOG.md +11 -2
  2. package/README.md +234 -72
  3. package/_utils.js +1 -1
  4. package/_utils.js.flow +3 -3
  5. package/_utils.mjs +1 -1
  6. package/core/array.d.ts +3 -0
  7. package/core/array.js +12 -1
  8. package/core/array.js.flow +9 -2
  9. package/core/array.mjs +11 -2
  10. package/core/boolean.js +3 -3
  11. package/core/boolean.js.flow +2 -2
  12. package/core/boolean.mjs +2 -2
  13. package/core/composition.d.ts +5 -1
  14. package/core/composition.js +33 -5
  15. package/core/composition.js.flow +31 -5
  16. package/core/composition.mjs +31 -5
  17. package/core/date.js +3 -3
  18. package/core/date.js.flow +2 -2
  19. package/core/date.mjs +2 -2
  20. package/core/dispatch.d.ts +1 -1
  21. package/core/dispatch.js +8 -6
  22. package/core/dispatch.js.flow +9 -8
  23. package/core/dispatch.mjs +6 -5
  24. package/core/either.d.ts +63 -58
  25. package/core/either.js +30 -42
  26. package/core/either.js.flow +38 -84
  27. package/core/either.mjs +34 -34
  28. package/core/instanceOf.d.ts +1 -1
  29. package/core/json.js +3 -3
  30. package/core/json.js.flow +3 -3
  31. package/core/json.mjs +3 -3
  32. package/core/object.d.ts +5 -0
  33. package/core/object.js +59 -2
  34. package/core/object.js.flow +77 -21
  35. package/core/object.mjs +56 -3
  36. package/core/string.d.ts +3 -0
  37. package/core/string.js +15 -3
  38. package/core/string.js.flow +15 -2
  39. package/core/string.mjs +12 -3
  40. package/core/tuple.d.ts +28 -28
  41. package/core/tuple.js +28 -151
  42. package/core/tuple.js.flow +31 -191
  43. package/core/tuple.mjs +27 -141
  44. package/index.d.ts +17 -18
  45. package/index.js +33 -43
  46. package/index.js.flow +17 -18
  47. package/index.mjs +8 -9
  48. package/package.json +1 -1
  49. package/result.d.ts +2 -2
  50. package/result.js +9 -9
  51. package/result.js.flow +11 -11
  52. package/result.mjs +9 -9
  53. package/core/mapping.d.ts +0 -6
  54. package/core/mapping.js +0 -67
  55. package/core/mapping.js.flow +0 -62
  56. package/core/mapping.mjs +0 -58
package/index.mjs CHANGED
@@ -17,21 +17,20 @@
17
17
  *
18
18
  */
19
19
  export { guard } from './_guard.mjs';
20
- export { compose, map, predicate } from './core/composition.mjs';
21
- export { array, nonEmptyArray, poja } from './core/array.mjs';
20
+ export { compose, predicate, prep, transform } from './core/composition.mjs';
21
+ export { array, nonEmptyArray, poja, set } from './core/array.mjs';
22
22
  export { boolean, numericBoolean, truthy } from './core/boolean.mjs';
23
23
  export { constant, hardcoded, mixed, null_, undefined_, unknown } from './core/constants.mjs';
24
24
  export { date, iso8601 } from './core/date.mjs';
25
25
  export { describe } from './core/describe.mjs';
26
- export { disjointUnion } from './core/dispatch.mjs';
27
- export { either, either3, either4, either5, either6, either7, either8, either9, oneOf } from './core/either.mjs';
26
+ export { dict, exact, inexact, mapping, object, pojo } from './core/object.mjs';
27
+ export { either, oneOf } from './core/either.mjs';
28
+ export { email, httpsUrl, nonEmptyString, regex, string, url, uuid, uuidv1, uuidv4 } from './core/string.mjs';
28
29
  export { fail } from './core/fail.mjs';
29
30
  export { instanceOf } from './core/instanceOf.mjs';
31
+ export { integer, number, positiveInteger, positiveNumber } from './core/number.mjs';
30
32
  export { json, jsonObject, jsonArray } from './core/json.mjs';
31
33
  export { lazy } from './core/lazy.mjs';
32
- export { mapping, dict } from './core/mapping.mjs';
33
- export { integer, number, positiveInteger, positiveNumber } from './core/number.mjs';
34
- export { exact, inexact, object, pojo } from './core/object.mjs';
35
34
  export { maybe, nullable, optional } from './core/optional.mjs';
36
- export { email, httpsUrl, nonEmptyString, regex, string, url } from './core/string.mjs';
37
- export { tuple1, tuple2, tuple3, tuple4, tuple5, tuple6 } from './core/tuple.mjs';
35
+ export { taggedUnion } from './core/dispatch.mjs';
36
+ export { tuple } from './core/tuple.mjs';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "decoders",
3
- "version": "2.0.0-beta8",
3
+ "version": "2.0.0-beta9",
4
4
  "description": "Elegant and battle-tested validation library for type-safe input data (for TypeScript and Flow)",
5
5
  "license": "MIT",
6
6
  "repository": {
package/result.d.ts CHANGED
@@ -1,11 +1,11 @@
1
1
  export interface Ok<T> {
2
- type: 'ok';
2
+ ok: true;
3
3
  value: T;
4
4
  error: undefined;
5
5
  }
6
6
 
7
7
  export interface Err<E> {
8
- type: 'err';
8
+ ok: false;
9
9
  value: undefined;
10
10
  error: E;
11
11
  }
package/result.js CHANGED
@@ -22,7 +22,7 @@ exports.unwrap = unwrap;
22
22
  */
23
23
  function ok(value) {
24
24
  return {
25
- type: 'ok',
25
+ ok: true,
26
26
  value: value,
27
27
  error: undefined
28
28
  };
@@ -34,7 +34,7 @@ function ok(value) {
34
34
 
35
35
  function err(error) {
36
36
  return {
37
- type: 'err',
37
+ ok: false,
38
38
  value: undefined,
39
39
  error: error
40
40
  };
@@ -46,7 +46,7 @@ function err(error) {
46
46
 
47
47
 
48
48
  function unwrap(result) {
49
- if (result.type === 'ok') {
49
+ if (result.ok) {
50
50
  return result.value;
51
51
  } else {
52
52
  throw result.error;
@@ -54,7 +54,7 @@ function unwrap(result) {
54
54
  }
55
55
 
56
56
  function expect(result, message) {
57
- if (result.type === 'ok') {
57
+ if (result.ok) {
58
58
  return result.value;
59
59
  } else {
60
60
  throw message instanceof Error ? message : new Error(message);
@@ -62,7 +62,7 @@ function expect(result, message) {
62
62
  }
63
63
 
64
64
  function dispatch(result, okCallback, errCallback) {
65
- return result.type === 'ok' ? okCallback(result.value) : errCallback(result.error);
65
+ return result.ok ? okCallback(result.value) : errCallback(result.error);
66
66
  }
67
67
  /**
68
68
  * Like .and(), aka &&, but the second argument gets evaluated lazily only if
@@ -72,7 +72,7 @@ function dispatch(result, okCallback, errCallback) {
72
72
 
73
73
 
74
74
  function andThen(result1, lazyResult2) {
75
- return result1.type === 'ok' ? lazyResult2(result1.value) : result1;
75
+ return result1.ok ? lazyResult2(result1.value) : result1;
76
76
  }
77
77
  /**
78
78
  * Like .or(), aka ||, but the second argument gets evaluated lazily only if
@@ -82,7 +82,7 @@ function andThen(result1, lazyResult2) {
82
82
 
83
83
 
84
84
  function orElse(result1, lazyResult2) {
85
- return result1.type === 'ok' ? result1 : lazyResult2(result1.error);
85
+ return result1.ok ? result1 : lazyResult2(result1.error);
86
86
  }
87
87
  /**
88
88
  * Transform an Ok result. Will not touch Err results.
@@ -90,7 +90,7 @@ function orElse(result1, lazyResult2) {
90
90
 
91
91
 
92
92
  function mapOk(result, mapper) {
93
- return result.type === 'ok' ? ok(mapper(result.value)) : result;
93
+ return result.ok ? ok(mapper(result.value)) : result;
94
94
  }
95
95
  /**
96
96
  * Transform an Err value. Will not touch Ok results.
@@ -98,5 +98,5 @@ function mapOk(result, mapper) {
98
98
 
99
99
 
100
100
  function mapError(result, mapper) {
101
- return result.type === 'ok' ? result : err(mapper(result.error));
101
+ return result.ok ? result : err(mapper(result.error));
102
102
  }
package/result.js.flow CHANGED
@@ -6,8 +6,8 @@
6
6
  * | Err <error>
7
7
  */
8
8
 
9
- type Ok<+T> = {| +type: 'ok', +value: T, +error: void |};
10
- type Err<+E> = {| +type: 'err', +value: void, +error: E |};
9
+ type Ok<+T> = {| +ok: true, +value: T, +error: void |};
10
+ type Err<+E> = {| +ok: false, +value: void, +error: E |};
11
11
 
12
12
  export type Result<+T, +E> = Ok<T> | Err<E>;
13
13
 
@@ -15,14 +15,14 @@ export type Result<+T, +E> = Ok<T> | Err<E>;
15
15
  * Create a new Result instance representing a successful computation.
16
16
  */
17
17
  export function ok<T>(value: T): Ok<T> {
18
- return { type: 'ok', value, error: undefined };
18
+ return { ok: true, value, error: undefined };
19
19
  }
20
20
 
21
21
  /**
22
22
  * Create a new Result instance representing a failed computation.
23
23
  */
24
24
  export function err<E>(error: E): Err<E> {
25
- return { type: 'err', value: undefined, error };
25
+ return { ok: false, value: undefined, error };
26
26
  }
27
27
 
28
28
  /**
@@ -30,7 +30,7 @@ export function err<E>(error: E): Err<E> {
30
30
  * Otherwise, will throw the "Err" error via a runtime exception.
31
31
  */
32
32
  export function unwrap<T>(result: Result<T, mixed>): T {
33
- if (result.type === 'ok') {
33
+ if (result.ok) {
34
34
  return result.value;
35
35
  } else {
36
36
  throw result.error;
@@ -38,7 +38,7 @@ export function unwrap<T>(result: Result<T, mixed>): T {
38
38
  }
39
39
 
40
40
  export function expect<T>(result: Result<T, mixed>, message: string | Error): T {
41
- if (result.type === 'ok') {
41
+ if (result.ok) {
42
42
  return result.value;
43
43
  } else {
44
44
  throw message instanceof Error ? message : new Error(message);
@@ -50,7 +50,7 @@ export function dispatch<T, E, O>(
50
50
  okCallback: (value: T) => O,
51
51
  errCallback: (error: E) => O,
52
52
  ): O {
53
- return result.type === 'ok' ? okCallback(result.value) : errCallback(result.error);
53
+ return result.ok ? okCallback(result.value) : errCallback(result.error);
54
54
  }
55
55
 
56
56
  /**
@@ -62,7 +62,7 @@ export function andThen<T, E, T2>(
62
62
  result1: Result<T, E>,
63
63
  lazyResult2: (value: T) => Result<T2, E>,
64
64
  ): Result<T2, E> {
65
- return result1.type === 'ok' ? lazyResult2(result1.value) : result1;
65
+ return result1.ok ? lazyResult2(result1.value) : result1;
66
66
  }
67
67
 
68
68
  /**
@@ -74,7 +74,7 @@ export function orElse<T, E, E2>(
74
74
  result1: Result<T, E>,
75
75
  lazyResult2: (errValue: E) => Result<T, E2>,
76
76
  ): Result<T, E2> {
77
- return result1.type === 'ok' ? result1 : lazyResult2(result1.error);
77
+ return result1.ok ? result1 : lazyResult2(result1.error);
78
78
  }
79
79
 
80
80
  /**
@@ -84,7 +84,7 @@ export function mapOk<T, E, T2>(
84
84
  result: Result<T, E>,
85
85
  mapper: (value: T) => T2,
86
86
  ): Result<T2, E> {
87
- return result.type === 'ok' ? ok(mapper(result.value)) : result;
87
+ return result.ok ? ok(mapper(result.value)) : result;
88
88
  }
89
89
 
90
90
  /**
@@ -94,5 +94,5 @@ export function mapError<T, E, E2>(
94
94
  result: Result<T, E>,
95
95
  mapper: (error: E) => E2,
96
96
  ): Result<T, E2> {
97
- return result.type === 'ok' ? result : err(mapper(result.error));
97
+ return result.ok ? result : err(mapper(result.error));
98
98
  }
package/result.mjs CHANGED
@@ -9,7 +9,7 @@
9
9
  */
10
10
  export function ok(value) {
11
11
  return {
12
- type: 'ok',
12
+ ok: true,
13
13
  value: value,
14
14
  error: undefined
15
15
  };
@@ -20,7 +20,7 @@ export function ok(value) {
20
20
 
21
21
  export function err(error) {
22
22
  return {
23
- type: 'err',
23
+ ok: false,
24
24
  value: undefined,
25
25
  error: error
26
26
  };
@@ -31,21 +31,21 @@ export function err(error) {
31
31
  */
32
32
 
33
33
  export function unwrap(result) {
34
- if (result.type === 'ok') {
34
+ if (result.ok) {
35
35
  return result.value;
36
36
  } else {
37
37
  throw result.error;
38
38
  }
39
39
  }
40
40
  export function expect(result, message) {
41
- if (result.type === 'ok') {
41
+ if (result.ok) {
42
42
  return result.value;
43
43
  } else {
44
44
  throw message instanceof Error ? message : new Error(message);
45
45
  }
46
46
  }
47
47
  export function dispatch(result, okCallback, errCallback) {
48
- return result.type === 'ok' ? okCallback(result.value) : errCallback(result.error);
48
+ return result.ok ? okCallback(result.value) : errCallback(result.error);
49
49
  }
50
50
  /**
51
51
  * Like .and(), aka &&, but the second argument gets evaluated lazily only if
@@ -54,7 +54,7 @@ export function dispatch(result, okCallback, errCallback) {
54
54
  */
55
55
 
56
56
  export function andThen(result1, lazyResult2) {
57
- return result1.type === 'ok' ? lazyResult2(result1.value) : result1;
57
+ return result1.ok ? lazyResult2(result1.value) : result1;
58
58
  }
59
59
  /**
60
60
  * Like .or(), aka ||, but the second argument gets evaluated lazily only if
@@ -63,19 +63,19 @@ export function andThen(result1, lazyResult2) {
63
63
  */
64
64
 
65
65
  export function orElse(result1, lazyResult2) {
66
- return result1.type === 'ok' ? result1 : lazyResult2(result1.error);
66
+ return result1.ok ? result1 : lazyResult2(result1.error);
67
67
  }
68
68
  /**
69
69
  * Transform an Ok result. Will not touch Err results.
70
70
  */
71
71
 
72
72
  export function mapOk(result, mapper) {
73
- return result.type === 'ok' ? ok(mapper(result.value)) : result;
73
+ return result.ok ? ok(mapper(result.value)) : result;
74
74
  }
75
75
  /**
76
76
  * Transform an Err value. Will not touch Ok results.
77
77
  */
78
78
 
79
79
  export function mapError(result, mapper) {
80
- return result.type === 'ok' ? result : err(mapper(result.error));
80
+ return result.ok ? result : err(mapper(result.error));
81
81
  }
package/core/mapping.d.ts DELETED
@@ -1,6 +0,0 @@
1
- /// <reference lib="es6" />
2
-
3
- import { Decoder } from '../_types';
4
-
5
- export function mapping<T>(decoder: Decoder<T>): Decoder<Map<string, T>>;
6
- export function dict<T>(decoder: Decoder<T>): Decoder<{ [key: string]: T }>;
package/core/mapping.js DELETED
@@ -1,67 +0,0 @@
1
- "use strict";
2
-
3
- exports.__esModule = true;
4
- exports.dict = dict;
5
- exports.mapping = mapping;
6
-
7
- var _annotate = require("../annotate");
8
-
9
- var _composition = require("./composition");
10
-
11
- var _result = require("../result");
12
-
13
- var _object = require("./object");
14
-
15
- /**
16
- * Like mapping(), but returns an object rather than a Map instance.
17
- */
18
- function dict(decoder) {
19
- return (0, _composition.compose)(_object.pojo, function (blob) {
20
- var rv = {};
21
- var errors = null;
22
- Object.keys(blob).forEach(function (key) {
23
- var value = blob[key];
24
- var result = decoder(value);
25
-
26
- if (result.type === 'ok') {
27
- if (errors === null) {
28
- rv[key] = result.value;
29
- }
30
- } else {
31
- rv = {}; // Clear the success value so it can get garbage collected early
32
-
33
- if (errors === null) {
34
- errors = {};
35
- }
36
-
37
- errors[key] = result.error;
38
- }
39
- });
40
-
41
- if (errors !== null) {
42
- return (0, _result.err)((0, _annotate.merge)((0, _annotate.annotateObject)(blob), errors));
43
- } else {
44
- return (0, _result.ok)(rv);
45
- }
46
- });
47
- }
48
- /**
49
- * Given an object, will decode a Map of string keys to whatever values.
50
- *
51
- * For example, given a decoder for a Person, we can verify a Person lookup
52
- * table structure (of type Map<string, Person>) like so:
53
- *
54
- * mapping(person)
55
- *
56
- */
57
-
58
-
59
- function mapping(decoder) {
60
- return (0, _composition.map)(dict(decoder), function (obj) {
61
- return new Map( // This is effectively Object.entries(obj), but in a way that Flow
62
- // will know the types are okay
63
- Object.keys(obj).map(function (key) {
64
- return [key, obj[key]];
65
- }));
66
- });
67
- }
@@ -1,62 +0,0 @@
1
- // @flow strict
2
-
3
- import { annotateObject } from '../annotate';
4
- import { compose, map } from './composition';
5
- import { err, ok } from '../result';
6
- import { merge } from '../annotate';
7
- import { pojo } from './object';
8
- import type { Annotation } from '../annotate';
9
- import type { Decoder } from '../_types';
10
-
11
- /**
12
- * Like mapping(), but returns an object rather than a Map instance.
13
- */
14
- export function dict<T>(decoder: Decoder<T>): Decoder<{ [string]: T }> {
15
- return compose(pojo, (blob: { +[key: string]: mixed }) => {
16
- let rv: { [key: string]: T } = {};
17
- let errors: { [key: string]: Annotation } | null = null;
18
-
19
- Object.keys(blob).forEach((key: string) => {
20
- const value = blob[key];
21
- const result = decoder(value);
22
- if (result.type === 'ok') {
23
- if (errors === null) {
24
- rv[key] = result.value;
25
- }
26
- } else {
27
- rv = {}; // Clear the success value so it can get garbage collected early
28
- if (errors === null) {
29
- errors = {};
30
- }
31
- errors[key] = result.error;
32
- }
33
- });
34
-
35
- if (errors !== null) {
36
- return err(merge(annotateObject(blob), errors));
37
- } else {
38
- return ok(rv);
39
- }
40
- });
41
- }
42
-
43
- /**
44
- * Given an object, will decode a Map of string keys to whatever values.
45
- *
46
- * For example, given a decoder for a Person, we can verify a Person lookup
47
- * table structure (of type Map<string, Person>) like so:
48
- *
49
- * mapping(person)
50
- *
51
- */
52
- export function mapping<T>(decoder: Decoder<T>): Decoder<Map<string, T>> {
53
- return map(
54
- dict(decoder),
55
- (obj) =>
56
- new Map(
57
- // This is effectively Object.entries(obj), but in a way that Flow
58
- // will know the types are okay
59
- Object.keys(obj).map((key) => [key, obj[key]]),
60
- ),
61
- );
62
- }
package/core/mapping.mjs DELETED
@@ -1,58 +0,0 @@
1
- import { annotateObject } from '../annotate.mjs';
2
- import { compose, map } from './composition.mjs';
3
- import { err, ok } from '../result.mjs';
4
- import { merge } from '../annotate.mjs';
5
- import { pojo } from './object.mjs';
6
-
7
- /**
8
- * Like mapping(), but returns an object rather than a Map instance.
9
- */
10
- export function dict(decoder) {
11
- return compose(pojo, function (blob) {
12
- var rv = {};
13
- var errors = null;
14
- Object.keys(blob).forEach(function (key) {
15
- var value = blob[key];
16
- var result = decoder(value);
17
-
18
- if (result.type === 'ok') {
19
- if (errors === null) {
20
- rv[key] = result.value;
21
- }
22
- } else {
23
- rv = {}; // Clear the success value so it can get garbage collected early
24
-
25
- if (errors === null) {
26
- errors = {};
27
- }
28
-
29
- errors[key] = result.error;
30
- }
31
- });
32
-
33
- if (errors !== null) {
34
- return err(merge(annotateObject(blob), errors));
35
- } else {
36
- return ok(rv);
37
- }
38
- });
39
- }
40
- /**
41
- * Given an object, will decode a Map of string keys to whatever values.
42
- *
43
- * For example, given a decoder for a Person, we can verify a Person lookup
44
- * table structure (of type Map<string, Person>) like so:
45
- *
46
- * mapping(person)
47
- *
48
- */
49
-
50
- export function mapping(decoder) {
51
- return map(dict(decoder), function (obj) {
52
- return new Map( // This is effectively Object.entries(obj), but in a way that Flow
53
- // will know the types are okay
54
- Object.keys(obj).map(function (key) {
55
- return [key, obj[key]];
56
- }));
57
- });
58
- }