1 | "use strict";
|
2 |
|
3 | Object.defineProperty(exports, "__esModule", {
|
4 | value: true
|
5 | });
|
6 | exports.typeToString = typeToString;
|
7 | exports.getTypeFromName = getTypeFromName;
|
8 | exports.describeValue = describeValue;
|
9 | exports.jsonToDisplayString = jsonToDisplayString;
|
10 | exports.verboseToDisplayJson = verboseToDisplayJson;
|
11 |
|
12 | var _index = require("./values/index.js");
|
13 |
|
14 | var _invariant = _interopRequireDefault(require("./invariant.js"));
|
15 |
|
16 | function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 |
|
23 |
|
24 |
|
25 |
|
26 |
|
27 |
|
28 | function typeToString(type) {
|
29 | function isInstance(proto, Constructor) {
|
30 | return proto instanceof Constructor || proto === Constructor.prototype;
|
31 | }
|
32 |
|
33 | let proto = type.prototype;
|
34 |
|
35 | if (isInstance(proto, _index.UndefinedValue)) {
|
36 | return "undefined";
|
37 | } else if (isInstance(proto, _index.NullValue)) {
|
38 | return "object";
|
39 | } else if (isInstance(proto, _index.StringValue)) {
|
40 | return "string";
|
41 | } else if (isInstance(proto, _index.BooleanValue)) {
|
42 | return "boolean";
|
43 | } else if (isInstance(proto, _index.NumberValue)) {
|
44 | return "number";
|
45 | } else if (isInstance(proto, _index.SymbolValue)) {
|
46 | return "symbol";
|
47 | } else if (isInstance(proto, _index.ObjectValue)) {
|
48 | if (_index.Value.isTypeCompatibleWith(type, _index.FunctionValue)) {
|
49 | return "function";
|
50 | }
|
51 |
|
52 | return "object";
|
53 | } else {
|
54 | return undefined;
|
55 | }
|
56 | }
|
57 |
|
58 | function getTypeFromName(typeName) {
|
59 | switch (typeName) {
|
60 | case "empty":
|
61 | return _index.EmptyValue;
|
62 |
|
63 | case "void":
|
64 | return _index.UndefinedValue;
|
65 |
|
66 | case "null":
|
67 | return _index.NullValue;
|
68 |
|
69 | case "boolean":
|
70 | return _index.BooleanValue;
|
71 |
|
72 | case "string":
|
73 | return _index.StringValue;
|
74 |
|
75 | case "symbol":
|
76 | return _index.SymbolValue;
|
77 |
|
78 | case "number":
|
79 | return _index.NumberValue;
|
80 |
|
81 | case "object":
|
82 | return _index.ObjectValue;
|
83 |
|
84 | case "array":
|
85 | return _index.ArrayValue;
|
86 |
|
87 | case "function":
|
88 | return _index.FunctionValue;
|
89 |
|
90 | case "integral":
|
91 | return _index.IntegralValue;
|
92 |
|
93 | default:
|
94 | return undefined;
|
95 | }
|
96 | }
|
97 |
|
98 | function describeValue(value) {
|
99 | let title;
|
100 | let suffix = "";
|
101 | if (value instanceof _index.PrimitiveValue) title = value.toDisplayString();else if (value instanceof _index.ObjectValue) title = "[object]";else {
|
102 | (0, _invariant.default)(value instanceof _index.AbstractValue, value.constructor.name);
|
103 | title = "[abstract]";
|
104 | if (value.kind !== undefined) title += `, kind: ${value.kind}`;
|
105 |
|
106 | for (let arg of value.args) {
|
107 | let t = describeValue(arg);
|
108 | suffix += t.split("\n").map(u => " " + u).join("\n") + "\n";
|
109 | }
|
110 | }
|
111 | title += `, hash: ${value.getHash()}`;
|
112 | if (value.intrinsicName !== undefined) title += `, intrinsic name: ${value.intrinsicName}`;
|
113 | if (value.__originalName !== undefined) title += `, original name: ${value.__originalName}`;
|
114 | return suffix ? `${title}\n${suffix}` : title;
|
115 | }
|
116 |
|
117 | function jsonToDisplayString(instance, depth) {
|
118 | let result = instance.toDisplayJson(depth);
|
119 | return typeof result === "string" ? result : JSON.stringify(result, null, 2).replace(/\"/g, "");
|
120 | }
|
121 |
|
122 | function verboseToDisplayJson(obj, depth) {
|
123 | let result = {};
|
124 |
|
125 | function valueOfProp(prop) {
|
126 | if (typeof prop === "function") return undefined;
|
127 |
|
128 | if (Array.isArray(prop)) {
|
129 |
|
130 | if (prop.length === 0) return "[]";
|
131 | let valuesArray = prop.map(x => valueOfProp(x));
|
132 |
|
133 | if (valuesArray.length < 5) {
|
134 | let string = "[" + valuesArray.reduce((acc, x) => `${acc}, ${x instanceof Object ? JSON.stringify(x) : x}`) + "]";
|
135 | string = string.replace(/\"/g, "");
|
136 | if (string.length < 60) return string;
|
137 | }
|
138 |
|
139 | return valuesArray;
|
140 | }
|
141 |
|
142 | if (prop instanceof Set || prop instanceof Map) return `${prop.constructor.name}(${prop.size})`;
|
143 | if (prop.toDisplayJson) return prop.toDisplayJson(depth - 1);
|
144 | if (prop.toDisplayString) return prop.toDisplayString();
|
145 | if (prop.toJSON) return prop.toJSON();
|
146 | return prop.toString();
|
147 | }
|
148 |
|
149 | for (let key in obj) {
|
150 | let prop = obj[key];
|
151 | if (!prop) continue;
|
152 | let value = valueOfProp(prop);
|
153 | if (value && value !== "[object Object]") result[key] = value;
|
154 | }
|
155 |
|
156 | return result;
|
157 | }
|
158 |
|
\ | No newline at end of file |