-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmod.js
More file actions
328 lines (304 loc) · 9.94 KB
/
Copy pathmod.js
File metadata and controls
328 lines (304 loc) · 9.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
/* @ts-self-types="./mod.d.ts" */
/**
* Render a detailed HTML representation of any JavaScript value.
*/
export function dump(
obj,
{
depth = 6,
enumerable = true,
symbols = false,
inherited = false,
order = false,
callGetters = false,
customRender = false,
} = {},
) {
const style = "<style>" +
"@scope (.nuxHtmlDump) {" +
" :scope {" +
" background:#f8f8f8;" +
" width:max-content;" +
" font-size:12px;" +
" color:black;" +
" padding:4px;" +
" border:1px solid;" +
" }" +
" table {" +
" border-collapse:collapse;" +
" display:inline-table;" +
" &:target { background:#ff0; }" +
" }" +
" td {" +
" vertical-align:top;" +
" padding:1px 4px;" +
" border:1px solid #0004;" +
" }" +
" number, bool { color:green; }" +
" string { color:#800; }" +
" null { color:#888; }" +
" function { color:#88f; }" +
" symbol { color:#f48; }" +
" accessor { color:#888; }" +
" :is(date, promise) { color:#1b8; }" +
" thead { font-weight:bold; }" +
"}" +
"</style>";
const objects = new WeakMap();
return `<div class=nuxHtmlDump>${style + valueToHtml(obj, 0)}</div>`;
function valueToHtml(obj, level) {
++level;
if (customRender) {
const val = customRender(obj);
if (val != null) return val;
}
switch (typeof obj) {
case "string": return `<string>"${encode(obj)}"</string>`;
case "number": return `<number>${encode(obj)}</number>`;
case "boolean": return `<bool>${encode(obj)}</bool>`;
case "symbol": return `<symbol>${encode(obj.toString())}</symbol>`;
}
if (typeof obj === "function" && !isConstructor(obj)) {
const asStr = obj.toString();
const arrow = asStr[0] === "f" ||
asStr.replace(/\s+/g, " ").startsWith("async function") ? "" : "arrow";
const async = obj[Symbol.toStringTag] === "AsyncFunction" ? "async" : "";
const generator = obj[Symbol.toStringTag] === "GeneratorFunction" ? "*" : "";
return "<function>" +
(async + " " + arrow + " " + generator + "function <b>").trim() +
encode(obj.name) + "</b>(" + obj.length + ")</function>";
}
if (obj == null) return "<null>" + obj + "</null>";
if (obj instanceof Date) return "<date>" + encode(Date.prototype.toString.call(obj)) + "</date>";
if (obj instanceof Promise) return "<promise>[object Promise]</promise>";
if (objects.has(obj)) return '<a href="#' + objects.get(obj) + '">(circular)</a>';
// its an object / array
if (level > depth) return "...";
const id = ("x" + Math.random()).replace(".", "");
try { objects.set(obj, id); }
catch { return "? error ?"; }
if (obj instanceof Map) obj = Object.fromEntries(obj);
if (obj instanceof Set) obj = [...obj];
const keys = propertiesOf(obj, { enumerable, inherited, symbols, order });
const isArray = Array.isArray(obj) && obj !== Array.prototype;
// table
const cols = objectIsTable(obj);
if (cols) {
let str = '<table id="' + id + '">';
str += "<thead>";
str += "<tr>";
str += "<td><small>" + (isArray ? "(items)" : "(index)") + "</small>";
for (const col in cols) str += "<td>" + encode(col);
str += "<tbody>";
for (const [name, behavoir] of keys) {
//if (isArray && name==='length') continue;
const value = propertyValue(obj, name, behavoir);
if (
name === "length" && value.valueType === "value" &&
typeof value.value === "number"
) continue;
str += "<tr>";
str += "<td>";
str += encode(name) + propertyBadge(value) + (isArray ? " = { " : " : { ");
for (const col in cols) {
str += "<td>";
const cell = value.valueType === "value"
? propertyValue(value.value, col) : { valueType: "none" };
str += cell.valueType !== "none"
? propertyValueToHtml(cell, level) : "<null>(not set)</null>";
}
}
return str += "</table>";
}
if (isArray) {
return "[" + obj.map((item) => valueToHtml(item, level)).join(" , ") + "]";
} else { // object
let str = '<table id="' + id + '">';
for (const [name, behavoir] of keys) {
const value = propertyValue(obj, name, behavoir);
str += "<tr>";
str += "<td>" + encode(name);
str += propertyBadge(value);
if (behavoir.inherited) str += " <small title=inherited>👨👦</small>";
if (!behavoir.enumerable) {
str += ' <small title="non-enumerable">🚫</small>';
}
str += "<td>" + propertyValueToHtml(value, level);
}
return str += "</table>";
}
}
function objectIsTable(obj) {
const keys = {}; // cols
let numProps = 0;
for (const prop in obj) {
try {
const value = propertyValue(obj, prop, undefined, false);
if (value.valueType !== "value") return;
if (typeof value.value === "string") return;
if (
value.value == null ||
(typeof value.value !== "object" && typeof value.value !== "function")
) return;
++numProps;
for (const key in value.value) {
if (keys[key] === undefined) keys[key] = 0;
keys[key]++;
}
} catch {
//console.log(obj, e)
}
}
if (numProps < 3) return; // just two rows
if (Object.values(keys).length < 2) return; // not enough cols
for (const keyNum of Object.values(keys)) {
if (keyNum < numProps / 2) return; // not minimum half the keys in sub obj
}
return keys;
}
function propertyValue(obj, key, behavoir, callAccessor = callGetters) {
const descriptor = behavoir?.descriptor || propertyDescriptor(obj, key);
if (!descriptor) return { valueType: "none" };
if ("value" in descriptor) {
return { valueType: "value", value: descriptor.value };
}
if (callAccessor && descriptor.get) {
try {
return {
valueType: "value",
value: descriptor.get.call(obj),
getter: true,
setter: Boolean(descriptor.set),
};
} catch (error) {
return {
valueType: "error",
error,
getter: true,
setter: Boolean(descriptor.set),
};
}
}
return {
valueType: "accessor",
get: Boolean(descriptor.get),
set: Boolean(descriptor.set),
};
}
function propertyValueToHtml(value, level) {
if (value.valueType === "accessor") {
const parts = [];
if (value.get) parts.push("Getter");
if (value.set) parts.push("Setter");
return "<accessor>[" + parts.join("/") + "]</accessor>";
}
if (value.valueType === "none") return "<null>(not set)</null>";
if (value.valueType === "error") return "<accessor>[Thrown: " + encode(value.error) + "]</accessor>";
return valueToHtml(value.value, level);
}
function propertyBadge(value) {
const parts = [];
if (value.getter || value.get) parts.push("get");
if (value.setter || value.set) parts.push("set");
if (parts.length) return ' <small title="accessor">(' + parts.join(",") + ")</small>";
return "";
}
function propertyDescriptor(obj, key) {
if (obj == null || (typeof obj !== "object" && typeof obj !== "function")) return;
let proto = obj;
while (proto) {
const descriptor = Object.getOwnPropertyDescriptor(proto, key);
if (descriptor) return descriptor;
proto = Object.getPrototypeOf(proto);
}
}
}
/**
* @param {unknown} str
* @returns {string}
*/
export function encode(str) { // TODO: does not escape " and '
// check if str is a Symbol
if (typeof str === "symbol") return "<symbol>" + encode(str.toString()) + "</symbol>";
return (str + "").replace(/[\u00A0-\u9999<>\&]/gim, function (i) {
return "&#" + i.charCodeAt(0) + ";";
});
}
function isConstructor(f) {
if (typeof f !== "function") return false;
if (f.prototype === undefined) return false;
if (Reflect.ownKeys(f.prototype).length < 2) return false;
// try {
// Reflect.construct(String, [], f);
// } catch {
// return false;
// }
return true;
}
function propertiesOf(obj, { enumerable, symbols, inherited, order }) {
let keys = new Map();
if (obj instanceof Map) {
for (const [k, _v] of obj) {
keys.set(k, {
own: true,
enumerable: true,
});
}
return keys;
}
Reflect.ownKeys(obj).forEach((k) => {
if (!symbols && typeof k === "symbol") return;
const descriptor = Object.getOwnPropertyDescriptor(obj, k);
const isEnumerable = descriptor?.enumerable ?? false;
if (!enumerable && !isEnumerable) return;
keys.set(k, {
own: true,
enumerable: isEnumerable,
descriptor,
});
});
if (inherited) {
const proto = Object.getPrototypeOf(obj);
if (
proto && proto !== Object.prototype && proto !== Function.prototype &&
proto !== Array.prototype
) {
const inheritedKeys = propertiesOf(proto, {
enumerable,
symbols,
inherited,
order,
});
for (const [k, v] of inheritedKeys) {
if (k === "constructor") continue;
if (!keys.has(k)) {
v.inherited = true;
keys.set(k, v);
}
}
}
}
if (order) {
const nKeys = new Map();
const tmp = Array.from(keys.keys()).sort();
for (const k of tmp) nKeys.set(k, keys.get(k));
keys = nKeys;
}
return keys;
}
/**
* @param {unknown} obj
* @returns {string | undefined}
*/
export function domRender(obj) {
let isElement = false;
let isText = false;
try {
isElement = obj instanceof globalThis.Element && obj.tagName;
isText = obj instanceof globalThis.Text;
} catch {
// The DOM globals may not exist in every runtime.
}
if (isElement) return encode(obj.outerHTML).substring(0, 70).trim() + "...";
if (isText) return encode(obj.textContent).substring(0, 60).trim() + "... [TextNode]";
}