@oclif/core 4.0.27 → 4.0.28
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.
- package/lib/ux/colorize-json.d.ts +1 -0
- package/lib/ux/colorize-json.js +33 -1
- package/package.json +1 -1
|
@@ -2,6 +2,7 @@ type Options = {
|
|
|
2
2
|
pretty?: boolean | undefined;
|
|
3
3
|
theme?: Record<string, string> | undefined;
|
|
4
4
|
};
|
|
5
|
+
export declare function removeCycles(object: unknown): unknown;
|
|
5
6
|
export declare function tokenize(json?: unknown, options?: Options): {
|
|
6
7
|
type: string;
|
|
7
8
|
value: string;
|
package/lib/ux/colorize-json.js
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.removeCycles = removeCycles;
|
|
3
4
|
exports.tokenize = tokenize;
|
|
4
5
|
exports.default = colorizeJson;
|
|
5
6
|
const theme_1 = require("./theme");
|
|
@@ -15,6 +16,37 @@ const tokenTypes = [
|
|
|
15
16
|
{ regex: /^true|^false/, tokenType: 'boolean' },
|
|
16
17
|
{ regex: /^null/, tokenType: 'null' },
|
|
17
18
|
];
|
|
19
|
+
function removeCycles(object) {
|
|
20
|
+
// Keep track of seen objects.
|
|
21
|
+
const seenObjects = new WeakMap();
|
|
22
|
+
const _removeCycles = (obj) => {
|
|
23
|
+
// Use object prototype to get around type and null checks
|
|
24
|
+
if (Object.prototype.toString.call(obj) === '[object Object]') {
|
|
25
|
+
// We know it is a "Record<string, unknown>" because of the conditional
|
|
26
|
+
const dictionary = obj;
|
|
27
|
+
// Seen, return undefined to remove.
|
|
28
|
+
if (seenObjects.has(dictionary))
|
|
29
|
+
return;
|
|
30
|
+
seenObjects.set(dictionary, undefined);
|
|
31
|
+
for (const key in dictionary) {
|
|
32
|
+
// Delete the duplicate object if cycle found.
|
|
33
|
+
if (_removeCycles(dictionary[key]) === undefined) {
|
|
34
|
+
delete dictionary[key];
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
else if (Array.isArray(obj)) {
|
|
39
|
+
for (const i in obj) {
|
|
40
|
+
if (_removeCycles(obj[i]) === undefined) {
|
|
41
|
+
// We don't want to delete the array, but we can replace the element with null.
|
|
42
|
+
obj[i] = null;
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
return obj;
|
|
47
|
+
};
|
|
48
|
+
return _removeCycles(object);
|
|
49
|
+
}
|
|
18
50
|
function formatInput(json, options) {
|
|
19
51
|
return options?.pretty
|
|
20
52
|
? JSON.stringify(typeof json === 'string' ? JSON.parse(json) : json, null, 2)
|
|
@@ -23,7 +55,7 @@ function formatInput(json, options) {
|
|
|
23
55
|
: JSON.stringify(json);
|
|
24
56
|
}
|
|
25
57
|
function tokenize(json, options) {
|
|
26
|
-
let input = formatInput(json, options);
|
|
58
|
+
let input = formatInput(removeCycles(json), options);
|
|
27
59
|
const tokens = [];
|
|
28
60
|
let foundToken = false;
|
|
29
61
|
do {
|