decoders 2.0.0-beta1 → 2.0.0-beta2

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 (135) hide show
  1. package/{es → _esm}/_guard.js +0 -0
  2. package/{cjs → _esm}/_guard.js.flow +0 -0
  3. package/{es → _esm}/_types.js +0 -0
  4. package/{cjs → _esm}/_types.js.flow +0 -0
  5. package/{es → _esm}/_utils.js +0 -0
  6. package/{cjs → _esm}/_utils.js.flow +0 -0
  7. package/{es → _esm}/annotate.js +0 -0
  8. package/{cjs → _esm}/annotate.js.flow +0 -0
  9. package/{es/stdlib → _esm/core}/array.js +0 -0
  10. package/{cjs/stdlib → _esm/core}/array.js.flow +0 -0
  11. package/{es/stdlib → _esm/core}/boolean.js +0 -0
  12. package/{cjs/stdlib → _esm/core}/boolean.js.flow +0 -0
  13. package/{es/stdlib → _esm/core}/composition.js +0 -0
  14. package/{cjs/stdlib → _esm/core}/composition.js.flow +0 -0
  15. package/{es/stdlib → _esm/core}/constants.js +0 -0
  16. package/{cjs/stdlib → _esm/core}/constants.js.flow +0 -0
  17. package/{es/stdlib → _esm/core}/date.js +0 -0
  18. package/{cjs/stdlib → _esm/core}/date.js.flow +0 -0
  19. package/{es/stdlib → _esm/core}/describe.js +0 -0
  20. package/{cjs/stdlib → _esm/core}/describe.js.flow +0 -0
  21. package/{es/stdlib → _esm/core}/dispatch.js +0 -0
  22. package/{cjs/stdlib → _esm/core}/dispatch.js.flow +0 -0
  23. package/{es/stdlib → _esm/core}/either.js +0 -0
  24. package/{cjs/stdlib → _esm/core}/either.js.flow +0 -0
  25. package/{es/stdlib → _esm/core}/fail.js +0 -0
  26. package/{cjs/stdlib → _esm/core}/fail.js.flow +0 -0
  27. package/{es/stdlib → _esm/core}/instanceOf.js +0 -0
  28. package/{cjs/stdlib → _esm/core}/instanceOf.js.flow +0 -0
  29. package/{es/stdlib → _esm/core}/json.js +0 -0
  30. package/{cjs/stdlib → _esm/core}/json.js.flow +0 -0
  31. package/{es/stdlib → _esm/core}/lazy.js +0 -0
  32. package/{cjs/stdlib → _esm/core}/lazy.js.flow +0 -0
  33. package/{es/stdlib → _esm/core}/mapping.js +0 -0
  34. package/{cjs/stdlib → _esm/core}/mapping.js.flow +0 -0
  35. package/{es/stdlib → _esm/core}/number.js +0 -0
  36. package/{cjs/stdlib → _esm/core}/number.js.flow +0 -0
  37. package/{es/stdlib → _esm/core}/object.js +0 -0
  38. package/{cjs/stdlib → _esm/core}/object.js.flow +0 -0
  39. package/{es/stdlib → _esm/core}/optional.js +0 -0
  40. package/{cjs/stdlib → _esm/core}/optional.js.flow +0 -0
  41. package/{es/stdlib → _esm/core}/string.js +0 -0
  42. package/{cjs/stdlib → _esm/core}/string.js.flow +0 -0
  43. package/{es/stdlib → _esm/core}/tuple.js +0 -0
  44. package/{cjs/stdlib → _esm/core}/tuple.js.flow +0 -0
  45. package/{es → _esm}/format/index.js +0 -0
  46. package/{cjs → _esm}/format/index.js.flow +0 -0
  47. package/{es → _esm}/format/inline.js +0 -0
  48. package/{cjs → _esm}/format/inline.js.flow +0 -0
  49. package/{es → _esm}/format/short.js +0 -0
  50. package/{cjs → _esm}/format/short.js.flow +0 -0
  51. package/{es → _esm}/index.js +18 -18
  52. package/{cjs → _esm}/index.js.flow +19 -26
  53. package/{es → _esm}/result.js +0 -0
  54. package/{cjs → _esm}/result.js.flow +0 -0
  55. package/{cjs/_guard.js → _guard.js} +0 -0
  56. package/_guard.js.flow +20 -0
  57. package/{cjs/_types.js → _types.js} +0 -0
  58. package/_types.js.flow +20 -0
  59. package/{cjs/_utils.js → _utils.js} +0 -0
  60. package/_utils.js.flow +97 -0
  61. package/{cjs/annotate.js → annotate.js} +0 -0
  62. package/annotate.js.flow +218 -0
  63. package/{cjs/stdlib → core}/array.js +0 -0
  64. package/core/array.js.flow +103 -0
  65. package/{cjs/stdlib → core}/boolean.js +0 -0
  66. package/core/boolean.js.flow +29 -0
  67. package/{cjs/stdlib → core}/composition.js +0 -0
  68. package/core/composition.js.flow +43 -0
  69. package/{cjs/stdlib → core}/constants.js +0 -0
  70. package/core/constants.js.flow +46 -0
  71. package/{cjs/stdlib → core}/date.js +0 -0
  72. package/core/date.js.flow +40 -0
  73. package/{cjs/stdlib → core}/describe.js +0 -0
  74. package/core/describe.js.flow +17 -0
  75. package/{cjs/stdlib → core}/dispatch.js +0 -0
  76. package/core/dispatch.js.flow +58 -0
  77. package/{cjs/stdlib → core}/either.js +0 -0
  78. package/core/either.js.flow +151 -0
  79. package/{cjs/stdlib → core}/fail.js +0 -0
  80. package/core/fail.js.flow +12 -0
  81. package/{cjs/stdlib → core}/instanceOf.js +0 -0
  82. package/core/instanceOf.js.flow +20 -0
  83. package/{cjs/stdlib → core}/json.js +0 -0
  84. package/core/json.js.flow +28 -0
  85. package/{cjs/stdlib → core}/lazy.js +0 -0
  86. package/core/lazy.js.flow +15 -0
  87. package/{cjs/stdlib → core}/mapping.js +0 -0
  88. package/core/mapping.js.flow +54 -0
  89. package/{cjs/stdlib → core}/number.js +0 -0
  90. package/core/number.js.flow +34 -0
  91. package/{cjs/stdlib → core}/object.js +0 -0
  92. package/core/object.js.flow +203 -0
  93. package/{cjs/stdlib → core}/optional.js +0 -0
  94. package/core/optional.js.flow +41 -0
  95. package/{cjs/stdlib → core}/string.js +0 -0
  96. package/core/string.js.flow +82 -0
  97. package/{cjs/stdlib → core}/tuple.js +0 -0
  98. package/core/tuple.js.flow +220 -0
  99. package/{cjs/format → format}/index.js +0 -0
  100. package/format/index.js.flow +4 -0
  101. package/{cjs/format → format}/inline.js +0 -0
  102. package/format/inline.js.flow +122 -0
  103. package/{cjs/format → format}/short.js +0 -0
  104. package/format/short.js.flow +8 -0
  105. package/{cjs/index.js → index.js} +18 -18
  106. package/index.js.flow +56 -0
  107. package/package.json +3 -12
  108. package/{cjs/result.js → result.js} +0 -0
  109. package/result.js.flow +166 -0
  110. package/ts/_guard.d.ts +0 -7
  111. package/ts/_helpers.d.ts +0 -79
  112. package/ts/_types.d.ts +0 -16
  113. package/ts/_utils.d.ts +0 -13
  114. package/ts/annotate.d.ts +0 -58
  115. package/ts/array.d.ts +0 -5
  116. package/ts/boolean.d.ts +0 -5
  117. package/ts/constants.d.ts +0 -11
  118. package/ts/date.d.ts +0 -4
  119. package/ts/describe.d.ts +0 -3
  120. package/ts/dispatch.d.ts +0 -8
  121. package/ts/either.d.ts +0 -61
  122. package/ts/fail.d.ts +0 -3
  123. package/ts/index.d.ts +0 -42
  124. package/ts/inline.d.ts +0 -3
  125. package/ts/instanceOf.d.ts +0 -3
  126. package/ts/json.d.ts +0 -11
  127. package/ts/lazy.d.ts +0 -3
  128. package/ts/mapping.d.ts +0 -4
  129. package/ts/number.d.ts +0 -6
  130. package/ts/object.d.ts +0 -33
  131. package/ts/optional.d.ts +0 -5
  132. package/ts/result.d.ts +0 -39
  133. package/ts/short.d.ts +0 -3
  134. package/ts/string.d.ts +0 -7
  135. package/ts/tuple.d.ts +0 -30
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
@@ -17,21 +17,21 @@
17
17
  *
18
18
  */
19
19
  export { guard } from './_guard';
20
- export { compose, map, predicate } from './stdlib/composition';
21
- export { array, nonEmptyArray, poja } from './stdlib/array';
22
- export { boolean, numericBoolean, truthy } from './stdlib/boolean';
23
- export { constant, hardcoded, mixed, null_, undefined_, unknown } from './stdlib/constants';
24
- export { date, iso8601 } from './stdlib/date';
25
- export { describe } from './stdlib/describe';
26
- export { dispatch } from './stdlib/dispatch';
27
- export { either, either3, either4, either5, either6, either7, either8, either9, oneOf } from './stdlib/either';
28
- export { fail } from './stdlib/fail';
29
- export { instanceOf } from './stdlib/instanceOf';
30
- export { json, jsonObject, jsonArray } from './stdlib/json';
31
- export { lazy } from './stdlib/lazy';
32
- export { mapping, dict } from './stdlib/mapping';
33
- export { integer, number, positiveInteger, positiveNumber } from './stdlib/number';
34
- export { exact, inexact, object, pojo } from './stdlib/object';
35
- export { maybe, nullable, optional } from './stdlib/optional';
36
- export { email, nonEmptyString, regex, string, url } from './stdlib/string';
37
- export { tuple1, tuple2, tuple3, tuple4, tuple5, tuple6 } from './stdlib/tuple';
20
+ export { compose, map, predicate } from './core/composition';
21
+ export { array, nonEmptyArray, poja } from './core/array';
22
+ export { boolean, numericBoolean, truthy } from './core/boolean';
23
+ export { constant, hardcoded, mixed, null_, undefined_, unknown } from './core/constants';
24
+ export { date, iso8601 } from './core/date';
25
+ export { describe } from './core/describe';
26
+ export { dispatch } from './core/dispatch';
27
+ export { either, either3, either4, either5, either6, either7, either8, either9, oneOf } from './core/either';
28
+ export { fail } from './core/fail';
29
+ export { instanceOf } from './core/instanceOf';
30
+ export { json, jsonObject, jsonArray } from './core/json';
31
+ export { lazy } from './core/lazy';
32
+ export { mapping, dict } from './core/mapping';
33
+ export { integer, number, positiveInteger, positiveNumber } from './core/number';
34
+ export { exact, inexact, object, pojo } from './core/object';
35
+ export { maybe, nullable, optional } from './core/optional';
36
+ export { email, nonEmptyString, regex, string, url } from './core/string';
37
+ export { tuple1, tuple2, tuple3, tuple4, tuple5, tuple6 } from './core/tuple';
@@ -21,25 +21,18 @@
21
21
  export type { Decoder, Guard } from './_types';
22
22
  export type { DecoderType, GuardType } from './_types';
23
23
 
24
- export type { JSONValue, JSONObject, JSONArray } from './stdlib/json';
24
+ export type { JSONValue, JSONObject, JSONArray } from './core/json';
25
25
 
26
26
  export { guard } from './_guard';
27
27
 
28
- export { compose, map, predicate } from './stdlib/composition';
28
+ export { compose, map, predicate } from './core/composition';
29
29
 
30
- export { array, nonEmptyArray, poja } from './stdlib/array';
31
- export { boolean, numericBoolean, truthy } from './stdlib/boolean';
32
- export {
33
- constant,
34
- hardcoded,
35
- mixed,
36
- null_,
37
- undefined_,
38
- unknown,
39
- } from './stdlib/constants';
40
- export { date, iso8601 } from './stdlib/date';
41
- export { describe } from './stdlib/describe';
42
- export { dispatch } from './stdlib/dispatch';
30
+ export { array, nonEmptyArray, poja } from './core/array';
31
+ export { boolean, numericBoolean, truthy } from './core/boolean';
32
+ export { constant, hardcoded, mixed, null_, undefined_, unknown } from './core/constants';
33
+ export { date, iso8601 } from './core/date';
34
+ export { describe } from './core/describe';
35
+ export { dispatch } from './core/dispatch';
43
36
  export {
44
37
  either,
45
38
  either3,
@@ -50,14 +43,14 @@ export {
50
43
  either8,
51
44
  either9,
52
45
  oneOf,
53
- } from './stdlib/either';
54
- export { fail } from './stdlib/fail';
55
- export { instanceOf } from './stdlib/instanceOf';
56
- export { json, jsonObject, jsonArray } from './stdlib/json';
57
- export { lazy } from './stdlib/lazy';
58
- export { mapping, dict } from './stdlib/mapping';
59
- export { integer, number, positiveInteger, positiveNumber } from './stdlib/number';
60
- export { exact, inexact, object, pojo } from './stdlib/object';
61
- export { maybe, nullable, optional } from './stdlib/optional';
62
- export { email, nonEmptyString, regex, string, url } from './stdlib/string';
63
- export { tuple1, tuple2, tuple3, tuple4, tuple5, tuple6 } from './stdlib/tuple';
46
+ } from './core/either';
47
+ export { fail } from './core/fail';
48
+ export { instanceOf } from './core/instanceOf';
49
+ export { json, jsonObject, jsonArray } from './core/json';
50
+ export { lazy } from './core/lazy';
51
+ export { mapping, dict } from './core/mapping';
52
+ export { integer, number, positiveInteger, positiveNumber } from './core/number';
53
+ export { exact, inexact, object, pojo } from './core/object';
54
+ export { maybe, nullable, optional } from './core/optional';
55
+ export { email, nonEmptyString, regex, string, url } from './core/string';
56
+ export { tuple1, tuple2, tuple3, tuple4, tuple5, tuple6 } from './core/tuple';
File without changes
File without changes
File without changes
package/_guard.js.flow ADDED
@@ -0,0 +1,20 @@
1
+ // @flow strict
2
+
3
+ import * as Result from './result';
4
+ import { formatInline } from './format';
5
+ import type { Annotation } from './annotate';
6
+ import type { Decoder, Guard } from './_types';
7
+
8
+ export function guard<T>(
9
+ decoder: Decoder<T>,
10
+ formatter: (Annotation) => string = formatInline,
11
+ ): Guard<T> {
12
+ return (blob: mixed) =>
13
+ Result.unwrap(
14
+ Result.mapError(decoder(blob), (annotation) => {
15
+ const err = new Error('\n' + formatter(annotation));
16
+ err.name = 'Decoding error';
17
+ return err;
18
+ }),
19
+ );
20
+ }
File without changes
package/_types.js.flow ADDED
@@ -0,0 +1,20 @@
1
+ // @flow strict
2
+
3
+ import type { Annotation } from './annotate';
4
+ import type { Result } from './result';
5
+
6
+ export type Scalar = string | number | boolean | symbol | void | null;
7
+
8
+ export type Predicate<T> = (T) => boolean;
9
+ export type DecodeResult<T> = Result<T, Annotation>;
10
+
11
+ export type Decoder<T, F = mixed> = (F) => DecodeResult<T>;
12
+ export type Guard<T> = (mixed) => T;
13
+
14
+ /**
15
+ * A "type function" which informs Flow about how a type will be modified at runtime.
16
+ * Read this as "given a Guard of type T, I can produce a value of type T". This
17
+ * definition helps construct $ObjMap types.
18
+ */
19
+ export type DecoderType = <T>(Decoder<T>) => T;
20
+ export type GuardType = <T>(Guard<T>) => T;
File without changes
package/_utils.js.flow ADDED
@@ -0,0 +1,97 @@
1
+ // @flow strict
2
+
3
+ import type { Annotation } from './annotate';
4
+
5
+ // $FlowFixMe[unclear-type] - deliberate casting
6
+ type cast = any;
7
+
8
+ // Two spaces of indentation
9
+ export const INDENT = ' ';
10
+
11
+ /**
12
+ * `x instanceof Date` checks are unreliable across stack frames (that information
13
+ * might get lost by the JS runtime), so we'll have to reside to more runtime
14
+ * inspection checks.
15
+ *
16
+ * Taken from https://stackoverflow.com/a/44198641
17
+ */
18
+ export function isDate(value: mixed): boolean {
19
+ return (
20
+ !!value &&
21
+ // $FlowFixMe[method-unbinding]
22
+ Object.prototype.toString.call(value) === '[object Date]' &&
23
+ !isNaN(value)
24
+ );
25
+ }
26
+
27
+ /**
28
+ * Is value is a valid Date instance, then return that. If not, then return
29
+ * null.
30
+ */
31
+ export function asDate(value: mixed): Date | null {
32
+ return isDate(value) ? ((value: cast): Date) : null;
33
+ }
34
+
35
+ export function isMultiline(s: string): boolean {
36
+ return s.indexOf('\n') >= 0;
37
+ }
38
+
39
+ export function indent(s: string, prefix: string = INDENT): string {
40
+ if (isMultiline(s)) {
41
+ return s
42
+ .split('\n')
43
+ .map((line) => prefix + line)
44
+ .join('\n');
45
+ } else {
46
+ return prefix + s;
47
+ }
48
+ }
49
+
50
+ /**
51
+ * Walks the annotation tree and emits the annotation's key path within the
52
+ * object tree, and the message as a series of messages (array of strings).
53
+ */
54
+ export function summarize(
55
+ ann: Annotation,
56
+ keypath: $ReadOnlyArray<number | string> = [],
57
+ ): Array<string> {
58
+ const result: Array<string> = [];
59
+
60
+ if (ann.type === 'array') {
61
+ const items = ann.items;
62
+ let index = 0;
63
+ items.forEach((ann) => {
64
+ summarize(ann, [...keypath, index++]).forEach((item) =>
65
+ // Collect to results
66
+ result.push(item),
67
+ );
68
+ });
69
+ } else if (ann.type === 'object') {
70
+ const fields = ann.fields;
71
+ Object.keys(fields).forEach((key) => {
72
+ const value = fields[key];
73
+ summarize(value, [...keypath, key]).forEach((item) =>
74
+ // Collect to results
75
+ result.push(item),
76
+ );
77
+ });
78
+ }
79
+
80
+ const text = ann.text;
81
+ if (!text) {
82
+ return result;
83
+ }
84
+
85
+ let prefix: string;
86
+ if (keypath.length === 0) {
87
+ prefix = '';
88
+ } else if (keypath.length === 1) {
89
+ prefix =
90
+ typeof keypath[0] === 'number'
91
+ ? `Value at index ${keypath[0]}: `
92
+ : `Value at key ${JSON.stringify(keypath[0])}: `;
93
+ } else {
94
+ prefix = `Value at keypath ${keypath.map((x) => x.toString()).join('.')}: `;
95
+ }
96
+ return [...result, prefix + text];
97
+ }
File without changes
@@ -0,0 +1,218 @@
1
+ // @flow strict
2
+
3
+ type cast = $FlowFixMe;
4
+
5
+ const _register: WeakSet<{ ... }> = new WeakSet();
6
+
7
+ export type ObjectAnnotation = {|
8
+ +type: 'object',
9
+ +fields: { +[key: string]: Annotation },
10
+ +text?: string,
11
+ |};
12
+
13
+ export type ArrayAnnotation = {|
14
+ +type: 'array',
15
+ +items: $ReadOnlyArray<Annotation>,
16
+ +text?: string,
17
+ |};
18
+
19
+ export type ScalarAnnotation = {|
20
+ +type: 'scalar',
21
+ +value: mixed,
22
+ +text?: string,
23
+ |};
24
+
25
+ export type FunctionAnnotation = {|
26
+ +type: 'function',
27
+ +text?: string,
28
+ |};
29
+
30
+ export type CircularRefAnnotation = {|
31
+ +type: 'circular-ref',
32
+ +text?: string,
33
+ |};
34
+
35
+ export type UnknownAnnotation = {|
36
+ +type: 'unknown',
37
+ +value: mixed,
38
+ +text?: string,
39
+ |};
40
+
41
+ export type Annotation =
42
+ | ObjectAnnotation
43
+ | ArrayAnnotation
44
+ | ScalarAnnotation
45
+ | FunctionAnnotation
46
+ | CircularRefAnnotation
47
+ | UnknownAnnotation;
48
+
49
+ function brand<A: Annotation>(ann: A): A {
50
+ _register.add(ann);
51
+ return ann;
52
+ }
53
+
54
+ export function object(
55
+ fields: { +[key: string]: Annotation },
56
+ text?: string,
57
+ ): ObjectAnnotation {
58
+ return brand({ type: 'object', fields, text });
59
+ }
60
+
61
+ export function array(items: $ReadOnlyArray<Annotation>, text?: string): ArrayAnnotation {
62
+ return brand({
63
+ type: 'array',
64
+ items,
65
+ text,
66
+ });
67
+ }
68
+
69
+ export function func(text?: string): FunctionAnnotation {
70
+ return brand({
71
+ type: 'function',
72
+ text,
73
+ });
74
+ }
75
+
76
+ export function unknown(value: mixed, text?: string): UnknownAnnotation {
77
+ return brand({
78
+ type: 'unknown',
79
+ value,
80
+ text,
81
+ });
82
+ }
83
+
84
+ export function scalar(value: mixed, text?: string): ScalarAnnotation {
85
+ return brand({
86
+ type: 'scalar',
87
+ value,
88
+ text,
89
+ });
90
+ }
91
+
92
+ export function circularRef(text?: string): CircularRefAnnotation {
93
+ return brand({
94
+ type: 'circular-ref',
95
+ text,
96
+ });
97
+ }
98
+
99
+ /**
100
+ * Given an existing Annotation, set the annotation's text to a new value.
101
+ */
102
+ export function updateText<A: Annotation>(annotation: A, text?: string): A {
103
+ if (text !== undefined) {
104
+ return brand({ ...annotation, text });
105
+ } else {
106
+ return annotation;
107
+ }
108
+ }
109
+
110
+ /**
111
+ * Given an existing ObjectAnnotation, merges new Annotations in there.
112
+ */
113
+ export function merge(
114
+ objAnnotation: ObjectAnnotation,
115
+ fields: { +[key: string]: Annotation },
116
+ ): ObjectAnnotation {
117
+ const newFields = { ...objAnnotation.fields, ...fields };
118
+ return object(newFields, objAnnotation.text);
119
+ }
120
+
121
+ export function asAnnotation(thing: mixed): Annotation | void {
122
+ return typeof thing === 'object' && thing !== null && _register.has(thing)
123
+ ? ((thing: cast): Annotation)
124
+ : undefined;
125
+ }
126
+
127
+ type RefSet = WeakSet<{ ... } | $ReadOnlyArray<mixed>>;
128
+
129
+ function annotateArray(
130
+ value: $ReadOnlyArray<mixed>,
131
+ text?: string,
132
+ seen: RefSet,
133
+ ): ArrayAnnotation | CircularRefAnnotation {
134
+ seen.add(value);
135
+
136
+ const items = value.map((v) => annotate(v, undefined, seen));
137
+ return array(items, text);
138
+ }
139
+
140
+ function annotateObject(
141
+ obj: { +[string]: mixed },
142
+ text?: string,
143
+ seen: RefSet,
144
+ ): ObjectAnnotation {
145
+ seen.add(obj);
146
+
147
+ const fields = {};
148
+ Object.keys(obj).forEach((key) => {
149
+ const value = obj[key];
150
+ fields[key] = annotate(value, undefined, seen);
151
+ });
152
+ return object(fields, text);
153
+ }
154
+
155
+ function annotate(value: mixed, text?: string, seen: RefSet): Annotation {
156
+ if (
157
+ value === null ||
158
+ value === undefined ||
159
+ typeof value === 'string' ||
160
+ typeof value === 'number' ||
161
+ typeof value === 'boolean' ||
162
+ typeof value === 'symbol' ||
163
+ typeof value.getMonth === 'function'
164
+ ) {
165
+ return scalar(value, text);
166
+ }
167
+
168
+ const ann = asAnnotation(value);
169
+ if (ann) {
170
+ return updateText(ann, text);
171
+ }
172
+
173
+ if (Array.isArray(value)) {
174
+ // "Circular references" can only exist in objects or arrays
175
+ if (seen.has(value)) {
176
+ return circularRef(text);
177
+ } else {
178
+ return annotateArray(value, text, seen);
179
+ }
180
+ }
181
+
182
+ if (typeof value === 'object') {
183
+ // "Circular references" can only exist in objects or arrays
184
+ if (seen.has(value)) {
185
+ return circularRef(text);
186
+ } else {
187
+ return annotateObject(value, text, seen);
188
+ }
189
+ }
190
+
191
+ if (typeof value === 'function') {
192
+ return func(text);
193
+ }
194
+
195
+ return unknown(value, text);
196
+ }
197
+
198
+ function public_annotate(value: mixed, text?: string): Annotation {
199
+ return annotate(value, text, new WeakSet());
200
+ }
201
+
202
+ function public_annotateObject(
203
+ obj: { +[string]: mixed },
204
+ text?: string,
205
+ ): ObjectAnnotation {
206
+ return annotateObject(obj, text, new WeakSet());
207
+ }
208
+
209
+ export {
210
+ // This construct just ensures the "seen" weakmap (used for circular
211
+ // reference detection) isn't made part of the public API.
212
+ public_annotate as annotate,
213
+ public_annotateObject as annotateObject,
214
+ //
215
+ // NOTE: Don't acces theses private APIs directly. They are only exported here
216
+ // to better enable unit testing.
217
+ annotate as __private_annotate,
218
+ };
File without changes