@devvit/shared-types 0.10.15-next-2024-01-29-98f6082be.0 → 0.10.15-next-2024-01-29-c13a1c9de.0
Sign up to get free protection for your applications and to get access to all the features.
- package/inspect.d.ts +12 -0
- package/inspect.d.ts.map +1 -0
- package/inspect.js +125 -0
- package/inspect.spec.d.ts.map +1 -0
- package/package.json +4 -4
package/inspect.d.ts
ADDED
@@ -0,0 +1,12 @@
|
|
1
|
+
export type Options = {
|
2
|
+
depth: number | null;
|
3
|
+
};
|
4
|
+
/**
|
5
|
+
* Similar to node's standard util.inspect, with the following differences:
|
6
|
+
* - Strings are not escaped (for example, when they have quote marks in them)
|
7
|
+
* - Symbol keys in objects are dropped and not printed
|
8
|
+
* - Prints arrays with more items per line sometimes
|
9
|
+
* - There's no color output
|
10
|
+
*/
|
11
|
+
export declare function inspect(val: unknown, options?: Partial<Options>): string;
|
12
|
+
//# sourceMappingURL=inspect.d.ts.map
|
package/inspect.d.ts.map
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"inspect.d.ts","sourceRoot":"","sources":["../src/inspect.ts"],"names":[],"mappings":"AAGA,MAAM,MAAM,OAAO,GAAG;IACpB,KAAK,EAAE,MAAM,GAAG,IAAI,CAAC;CACtB,CAAC;AAkBF;;;;;;GAMG;AACH,wBAAgB,OAAO,CAAC,GAAG,EAAE,OAAO,EAAE,OAAO,CAAC,EAAE,OAAO,CAAC,OAAO,CAAC,GAAG,MAAM,CAiBxE"}
|
package/inspect.js
ADDED
@@ -0,0 +1,125 @@
|
|
1
|
+
const indentPerLevel = ' '; // 2 spaces
|
2
|
+
const smallThreashold = 55;
|
3
|
+
const defaultOptions = {
|
4
|
+
depth: 2,
|
5
|
+
};
|
6
|
+
/**
|
7
|
+
* Similar to node's standard util.inspect, with the following differences:
|
8
|
+
* - Strings are not escaped (for example, when they have quote marks in them)
|
9
|
+
* - Symbol keys in objects are dropped and not printed
|
10
|
+
* - Prints arrays with more items per line sometimes
|
11
|
+
* - There's no color output
|
12
|
+
*/
|
13
|
+
export function inspect(val, options) {
|
14
|
+
if (val instanceof Error) {
|
15
|
+
return `${val.name}: ${val.message}`;
|
16
|
+
}
|
17
|
+
const indent = '';
|
18
|
+
const refs = {
|
19
|
+
visited: new Map(),
|
20
|
+
currentNumber: 0,
|
21
|
+
};
|
22
|
+
const completeOptions = {
|
23
|
+
...defaultOptions,
|
24
|
+
...(options ?? {}),
|
25
|
+
};
|
26
|
+
return inspectRoot(val, completeOptions, refs, indent);
|
27
|
+
}
|
28
|
+
function inspectRoot(val, args, refs, indent) {
|
29
|
+
switch (typeof val) {
|
30
|
+
case 'object':
|
31
|
+
if (val === null) {
|
32
|
+
return 'null';
|
33
|
+
}
|
34
|
+
return inspectObject(val, args, refs, indent);
|
35
|
+
case 'undefined':
|
36
|
+
return 'undefined';
|
37
|
+
case 'function':
|
38
|
+
return `[Function: ${val.name}]`;
|
39
|
+
case 'string':
|
40
|
+
return `'${val}'`;
|
41
|
+
case 'number': // fallthrough
|
42
|
+
case 'bigint': // fallthrough
|
43
|
+
case 'boolean': // fallthrough
|
44
|
+
case 'symbol': // fallthrough
|
45
|
+
return val.toString();
|
46
|
+
default:
|
47
|
+
return ''; // this should never happen
|
48
|
+
}
|
49
|
+
}
|
50
|
+
function inspectObject(obj, args, refs, indent) {
|
51
|
+
let refConfig = refs.visited.get(obj);
|
52
|
+
if (refConfig === undefined) {
|
53
|
+
refConfig = { isBeingPrinted: false };
|
54
|
+
refs.visited.set(obj, refConfig);
|
55
|
+
}
|
56
|
+
if (refConfig.isBeingPrinted) {
|
57
|
+
if (refConfig.refNumber === undefined) {
|
58
|
+
refConfig.refNumber = ++refs.currentNumber;
|
59
|
+
}
|
60
|
+
return `[Circular *${refConfig.refNumber}]`;
|
61
|
+
}
|
62
|
+
refConfig.isBeingPrinted = true;
|
63
|
+
let depthRemaining = args.depth;
|
64
|
+
if (depthRemaining !== null && depthRemaining < 0) {
|
65
|
+
refConfig.isBeingPrinted = false;
|
66
|
+
return Array.isArray(obj) ? '[Array]' : '[Object]';
|
67
|
+
}
|
68
|
+
if (depthRemaining !== null) {
|
69
|
+
depthRemaining -= 1;
|
70
|
+
}
|
71
|
+
const entries = [];
|
72
|
+
let totalLength = 0;
|
73
|
+
const subObjArgs = {
|
74
|
+
...args,
|
75
|
+
depth: depthRemaining,
|
76
|
+
};
|
77
|
+
// collect all of the entries of the object or array
|
78
|
+
if (Array.isArray(obj)) {
|
79
|
+
for (const val of obj) {
|
80
|
+
const valString = inspectRoot(val, subObjArgs, refs, indent + indentPerLevel);
|
81
|
+
entries.push(valString);
|
82
|
+
totalLength += valString.length;
|
83
|
+
}
|
84
|
+
}
|
85
|
+
else {
|
86
|
+
for (const key in obj) {
|
87
|
+
const valString = inspectRoot(obj[key], subObjArgs, refs, indent + indentPerLevel);
|
88
|
+
entries.push([key, valString]);
|
89
|
+
totalLength += key.length;
|
90
|
+
totalLength += valString.length;
|
91
|
+
totalLength += ': '.length;
|
92
|
+
}
|
93
|
+
}
|
94
|
+
// now that we've visited all the children, it's safe to mark as not being printed
|
95
|
+
refConfig.isBeingPrinted = false;
|
96
|
+
// if any children referenced this node, we've got to add a ref label
|
97
|
+
const refLabel = refConfig.refNumber ? `⟨ref *${refConfig.refNumber}⟩ ` : '';
|
98
|
+
// handle small objects in a single line
|
99
|
+
if (totalLength < smallThreashold) {
|
100
|
+
if (Array.isArray(obj)) {
|
101
|
+
return `${refLabel}[ ${entries.join(', ')} ]`;
|
102
|
+
}
|
103
|
+
else {
|
104
|
+
return `${refLabel}{${entries.map(([key, val]) => `${key}: ${val}`).join(', ')}}`;
|
105
|
+
}
|
106
|
+
}
|
107
|
+
// handle large objects by indenting/formatting
|
108
|
+
let result = `${refLabel}${Array.isArray(obj) ? '[' : '{'}`;
|
109
|
+
for (let i = 0; i < entries.length; i++) {
|
110
|
+
const isLast = i === entries.length - 1;
|
111
|
+
const entry = entries[i];
|
112
|
+
if (Array.isArray(obj)) {
|
113
|
+
result += `\n${indent}${indentPerLevel}${entry}`;
|
114
|
+
}
|
115
|
+
else {
|
116
|
+
const [key, val] = entry;
|
117
|
+
result += `\n${indent}${indentPerLevel}${key}: ${val}`;
|
118
|
+
}
|
119
|
+
if (!isLast) {
|
120
|
+
result += ',';
|
121
|
+
}
|
122
|
+
}
|
123
|
+
result += `\n${indent}${Array.isArray(obj) ? ']' : '}'}`;
|
124
|
+
return result;
|
125
|
+
}
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"inspect.spec.d.ts","sourceRoot":"","sources":["../src/inspect.spec.ts"],"names":[],"mappings":""}
|
package/package.json
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
{
|
2
2
|
"name": "@devvit/shared-types",
|
3
|
-
"version": "0.10.15-next-2024-01-29-
|
3
|
+
"version": "0.10.15-next-2024-01-29-c13a1c9de.0",
|
4
4
|
"license": "BSD-3-Clause",
|
5
5
|
"repository": {
|
6
6
|
"type": "git",
|
@@ -23,12 +23,12 @@
|
|
23
23
|
},
|
24
24
|
"types": "./index.d.ts",
|
25
25
|
"dependencies": {
|
26
|
-
"@devvit/protos": "0.10.15-next-2024-01-29-
|
26
|
+
"@devvit/protos": "0.10.15-next-2024-01-29-c13a1c9de.0"
|
27
27
|
},
|
28
28
|
"devDependencies": {
|
29
29
|
"@devvit/eslint-config": "0.10.14",
|
30
30
|
"@devvit/repo-tools": "0.10.14",
|
31
|
-
"@devvit/tsconfig": "0.10.15-next-2024-01-29-
|
31
|
+
"@devvit/tsconfig": "0.10.15-next-2024-01-29-c13a1c9de.0",
|
32
32
|
"eslint": "8.9.0",
|
33
33
|
"typescript": "5.3.2",
|
34
34
|
"vitest": "0.31.0"
|
@@ -37,5 +37,5 @@
|
|
37
37
|
"directory": "dist"
|
38
38
|
},
|
39
39
|
"source": "./src/index.ts",
|
40
|
-
"gitHead": "
|
40
|
+
"gitHead": "9393eaa2706d3f23669c82beddff72eef62ec41a"
|
41
41
|
}
|