@joshuahhh/pretty-print 0.0.4 → 0.0.6
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/dist/pretty-print.d.ts
CHANGED
|
@@ -4,7 +4,7 @@ export interface PrettyPrintOptions {
|
|
|
4
4
|
useColor?: boolean;
|
|
5
5
|
niceId?: boolean;
|
|
6
6
|
niceType?: boolean;
|
|
7
|
-
/**
|
|
7
|
+
/** Maximum number of digits after the decimal point for floating-point numbers. */
|
|
8
8
|
precision?: number;
|
|
9
9
|
/** Number of significant digits for floating-point numbers (uses toPrecision). Ignored if precision is set. */
|
|
10
10
|
significantDigits?: number;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"pretty-print.d.ts","sourceRoot":"","sources":["../src/pretty-print.tsx"],"names":[],"mappings":"AACA,OAAO,KAAK,MAAM,OAAO,CAAC;AAO1B,MAAM,WAAW,kBAAkB;IACjC,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,QAAQ,CAAC,EAAE,OAAO,CAAC;IACnB,MAAM,CAAC,EAAE,OAAO,CAAC;IACjB,QAAQ,CAAC,EAAE,OAAO,CAAC;IACnB,
|
|
1
|
+
{"version":3,"file":"pretty-print.d.ts","sourceRoot":"","sources":["../src/pretty-print.tsx"],"names":[],"mappings":"AACA,OAAO,KAAK,MAAM,OAAO,CAAC;AAO1B,MAAM,WAAW,kBAAkB;IACjC,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,QAAQ,CAAC,EAAE,OAAO,CAAC;IACnB,MAAM,CAAC,EAAE,OAAO,CAAC;IACjB,QAAQ,CAAC,EAAE,OAAO,CAAC;IACnB,mFAAmF;IACnF,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB,+GAA+G;IAC/G,iBAAiB,CAAC,EAAE,MAAM,CAAC;CAC5B;AA2BD;;GAEG;AACH,wBAAgB,qBAAqB,CACnC,KAAK,EAAE,OAAO,EACd,OAAO,GAAE,kBAAuB,GAC/B,CAAC,MAAM,EAAE,GAAG,MAAM,EAAE,CAAC,CA2BvB;AAED;;;GAGG;AACH,wBAAgB,SAAS,CACvB,KAAK,EAAE,OAAO,EACd,EAAE,KAAK,EAAE,GAAG,OAAO,EAAE,GAAE,kBAAkB,GAAG;IAAE,KAAK,CAAC,EAAE,MAAM,CAAA;CAAO,GAClE,IAAI,CAQN;AAED;;;GAGG;AACH,wBAAgB,WAAW,CAAC,EAC1B,KAAK,EACL,KAAK,EACL,SAAS,EACT,GAAG,OAAO,EACX,EAAE,kBAAkB,GAAG;IACtB,KAAK,EAAE,OAAO,CAAC;IACf,KAAK,CAAC,EAAE,KAAK,CAAC,aAAa,CAAC;IAC5B,SAAS,CAAC,EAAE,MAAM,CAAC;CACpB,2CAyFA;AAmUD,wBAAgB,mBAAmB,CACjC,KAAK,EAAE,OAAO,EACd,OAAO,GAAE,kBAAuB,GAC/B,MAAM,CAaR;AAGD,eAAO,MAAM,QAAQ;;;;;;;;;;;;;;;;;;;;;;;;CAYpB,CAAC"}
|
package/dist/pretty-print.js
CHANGED
|
@@ -141,8 +141,6 @@ function prettyPrintToDoc(value, tagger, options, visited = new Set(), path = []
|
|
|
141
141
|
const props = element.props;
|
|
142
142
|
const { children, ...otherProps } = props;
|
|
143
143
|
const openTag = colorize(`<${type}`, "keyword", [...path, 0]);
|
|
144
|
-
const closeTag = colorize(`</${type}>`, "keyword", [...path, 4]);
|
|
145
|
-
const selfCloseTag = colorize("/>", "keyword", [...path, 2]);
|
|
146
144
|
// Format props
|
|
147
145
|
const propEntries = Object.entries(otherProps);
|
|
148
146
|
const propDocs = [];
|
|
@@ -166,9 +164,11 @@ function prettyPrintToDoc(value, tagger, options, visited = new Set(), path = []
|
|
|
166
164
|
const childrenArray = React.Children.toArray(children);
|
|
167
165
|
const hasChildren = childrenArray.length > 0;
|
|
168
166
|
if (!hasChildren && propEntries.length === 0) {
|
|
167
|
+
const selfCloseTag = colorize("/>", "keyword", [...path, 2]);
|
|
169
168
|
return [openTag, " ", selfCloseTag];
|
|
170
169
|
}
|
|
171
170
|
if (!hasChildren) {
|
|
171
|
+
const selfCloseTag = colorize("/>", "keyword", [...path, 2]);
|
|
172
172
|
return group([
|
|
173
173
|
openTag,
|
|
174
174
|
indent(propDocs),
|
|
@@ -176,6 +176,7 @@ function prettyPrintToDoc(value, tagger, options, visited = new Set(), path = []
|
|
|
176
176
|
selfCloseTag,
|
|
177
177
|
]);
|
|
178
178
|
}
|
|
179
|
+
const closeTag = colorize(`</${type}>`, "keyword", [...path, 4]);
|
|
179
180
|
const childDocs = childrenArray.map((child, i) => typeof child === "string" || typeof child === "number"
|
|
180
181
|
? String(child)
|
|
181
182
|
: prettyPrintToDoc(child, tagger, options, visited, [...path, 3, i]));
|
|
@@ -211,7 +212,7 @@ function prettyPrintToDoc(value, tagger, options, visited = new Set(), path = []
|
|
|
211
212
|
if (typeof value === "number") {
|
|
212
213
|
let numStr;
|
|
213
214
|
if (Number.isFinite(value) && options.precision != null) {
|
|
214
|
-
numStr = value.toFixed(options.precision);
|
|
215
|
+
numStr = String(parseFloat(value.toFixed(options.precision)));
|
|
215
216
|
}
|
|
216
217
|
else if (Number.isFinite(value) && options.significantDigits != null) {
|
|
217
218
|
numStr = value.toPrecision(options.significantDigits);
|
|
@@ -381,6 +382,7 @@ function prettyPrintToDoc(value, tagger, options, visited = new Set(), path = []
|
|
|
381
382
|
return "[Unknown]";
|
|
382
383
|
}
|
|
383
384
|
export function prettyPrintToString(value, options = {}) {
|
|
385
|
+
console.log("value", value);
|
|
384
386
|
const { width = 80, useColor = true } = options;
|
|
385
387
|
const tagger = useColor ? new StringTagger() : null;
|
|
386
388
|
const doc = prettyPrintToDoc(value, tagger, options);
|
|
@@ -4,8 +4,14 @@ import { prettyPrintToString } from "./pretty-print.js";
|
|
|
4
4
|
describe("prettyPrintToString", () => {
|
|
5
5
|
it("should format with and without ANSI codes", () => {
|
|
6
6
|
const longArray = Array.from({ length: 20 }, (_, i) => i);
|
|
7
|
-
const withoutAnsi = prettyPrintToString(longArray, {
|
|
8
|
-
|
|
7
|
+
const withoutAnsi = prettyPrintToString(longArray, {
|
|
8
|
+
width: 200,
|
|
9
|
+
useColor: false,
|
|
10
|
+
});
|
|
11
|
+
const withAnsi = prettyPrintToString(longArray, {
|
|
12
|
+
width: 200,
|
|
13
|
+
useColor: true,
|
|
14
|
+
});
|
|
9
15
|
// Count ANSI escape sequences
|
|
10
16
|
const ansiMatches = withAnsi.match(/\x1b\[\d+m/g);
|
|
11
17
|
expect(ansiMatches).toBeTruthy();
|
|
@@ -13,13 +19,19 @@ describe("prettyPrintToString", () => {
|
|
|
13
19
|
});
|
|
14
20
|
it("should format long arrays inline with wide printWidth", () => {
|
|
15
21
|
const longArray = Array.from({ length: 20 }, (_, i) => i);
|
|
16
|
-
const result = prettyPrintToString(longArray, {
|
|
22
|
+
const result = prettyPrintToString(longArray, {
|
|
23
|
+
width: 200,
|
|
24
|
+
useColor: false,
|
|
25
|
+
});
|
|
17
26
|
// With width 200, this should be all on one line
|
|
18
27
|
expect(result).not.toContain("\n");
|
|
19
28
|
});
|
|
20
29
|
it("should format long arrays with line breaks when narrow", () => {
|
|
21
30
|
const longArray = Array.from({ length: 20 }, (_, i) => i);
|
|
22
|
-
const result = prettyPrintToString(longArray, {
|
|
31
|
+
const result = prettyPrintToString(longArray, {
|
|
32
|
+
width: 40,
|
|
33
|
+
useColor: false,
|
|
34
|
+
});
|
|
23
35
|
// With width 40, this should break across multiple lines
|
|
24
36
|
expect(result).toContain("\n");
|
|
25
37
|
});
|
|
@@ -53,8 +65,14 @@ describe("prettyPrintToString", () => {
|
|
|
53
65
|
});
|
|
54
66
|
it("should handle very long arrays", () => {
|
|
55
67
|
const veryLongArray = Array.from({ length: 50 }, (_, i) => i + 1);
|
|
56
|
-
const wide = prettyPrintToString(veryLongArray, {
|
|
57
|
-
|
|
68
|
+
const wide = prettyPrintToString(veryLongArray, {
|
|
69
|
+
width: 300,
|
|
70
|
+
useColor: false,
|
|
71
|
+
});
|
|
72
|
+
const narrow = prettyPrintToString(veryLongArray, {
|
|
73
|
+
width: 60,
|
|
74
|
+
useColor: false,
|
|
75
|
+
});
|
|
58
76
|
// Both should contain all elements
|
|
59
77
|
expect(wide).toContain("49");
|
|
60
78
|
expect(narrow).toContain("49");
|
|
@@ -126,7 +144,11 @@ describe("prettyPrintToString", () => {
|
|
|
126
144
|
});
|
|
127
145
|
it("should keep type as normal property when niceType is false", () => {
|
|
128
146
|
const obj = { type: "person", name: "Alice" };
|
|
129
|
-
const result = prettyPrintToString(obj, {
|
|
147
|
+
const result = prettyPrintToString(obj, {
|
|
148
|
+
width: 80,
|
|
149
|
+
useColor: false,
|
|
150
|
+
niceType: false,
|
|
151
|
+
});
|
|
130
152
|
expect(result).toBe('{type: "person", name: "Alice"}');
|
|
131
153
|
});
|
|
132
154
|
it("should extract id field as prefix by default", () => {
|
|
@@ -136,7 +158,11 @@ describe("prettyPrintToString", () => {
|
|
|
136
158
|
});
|
|
137
159
|
it("should keep id as normal property when niceId is false", () => {
|
|
138
160
|
const obj = { id: 42, name: "Alice" };
|
|
139
|
-
const result = prettyPrintToString(obj, {
|
|
161
|
+
const result = prettyPrintToString(obj, {
|
|
162
|
+
width: 80,
|
|
163
|
+
useColor: false,
|
|
164
|
+
niceId: false,
|
|
165
|
+
});
|
|
140
166
|
expect(result).toBe('{id: 42, name: "Alice"}');
|
|
141
167
|
});
|
|
142
168
|
it("should extract both type and id as prefixes by default", () => {
|
|
@@ -154,24 +180,33 @@ describe("prettyPrintToString", () => {
|
|
|
154
180
|
});
|
|
155
181
|
expect(result).toBe('{type: "user", id: 1, name: "Alice"}');
|
|
156
182
|
});
|
|
157
|
-
it("should
|
|
158
|
-
const result = prettyPrintToString(3.14159, {
|
|
183
|
+
it("should limit decimal places with precision", () => {
|
|
184
|
+
const result = prettyPrintToString(3.14159, {
|
|
185
|
+
useColor: false,
|
|
186
|
+
precision: 2,
|
|
187
|
+
});
|
|
159
188
|
expect(result).toBe("3.14");
|
|
160
189
|
});
|
|
161
190
|
it("should format numbers with precision 0", () => {
|
|
162
191
|
const result = prettyPrintToString(3.7, { useColor: false, precision: 0 });
|
|
163
192
|
expect(result).toBe("4");
|
|
164
193
|
});
|
|
165
|
-
it("should pad with zeros when precision exceeds decimal places", () => {
|
|
194
|
+
it("should not pad with zeros when precision exceeds decimal places", () => {
|
|
166
195
|
const result = prettyPrintToString(1.5, { useColor: false, precision: 4 });
|
|
167
|
-
expect(result).toBe("1.
|
|
196
|
+
expect(result).toBe("1.5");
|
|
168
197
|
});
|
|
169
198
|
it("should format numbers with significantDigits (toPrecision)", () => {
|
|
170
|
-
const result = prettyPrintToString(3.14159, {
|
|
199
|
+
const result = prettyPrintToString(3.14159, {
|
|
200
|
+
useColor: false,
|
|
201
|
+
significantDigits: 4,
|
|
202
|
+
});
|
|
171
203
|
expect(result).toBe("3.142");
|
|
172
204
|
});
|
|
173
205
|
it("should format large numbers with significantDigits", () => {
|
|
174
|
-
const result = prettyPrintToString(123456, {
|
|
206
|
+
const result = prettyPrintToString(123456, {
|
|
207
|
+
useColor: false,
|
|
208
|
+
significantDigits: 3,
|
|
209
|
+
});
|
|
175
210
|
expect(result).toBe("1.23e+5");
|
|
176
211
|
});
|
|
177
212
|
it("precision should take precedence over significantDigits", () => {
|
|
@@ -183,7 +218,10 @@ describe("prettyPrintToString", () => {
|
|
|
183
218
|
expect(result).toBe("3.1");
|
|
184
219
|
});
|
|
185
220
|
it("should not apply precision to Infinity", () => {
|
|
186
|
-
const result = prettyPrintToString(Infinity, {
|
|
221
|
+
const result = prettyPrintToString(Infinity, {
|
|
222
|
+
useColor: false,
|
|
223
|
+
precision: 2,
|
|
224
|
+
});
|
|
187
225
|
expect(result).toBe("Infinity");
|
|
188
226
|
});
|
|
189
227
|
it("should not apply precision to NaN", () => {
|
|
@@ -191,17 +229,24 @@ describe("prettyPrintToString", () => {
|
|
|
191
229
|
expect(result).toBe("NaN");
|
|
192
230
|
});
|
|
193
231
|
it("should not apply significantDigits to -Infinity", () => {
|
|
194
|
-
const result = prettyPrintToString(-Infinity, {
|
|
232
|
+
const result = prettyPrintToString(-Infinity, {
|
|
233
|
+
useColor: false,
|
|
234
|
+
significantDigits: 3,
|
|
235
|
+
});
|
|
195
236
|
expect(result).toBe("-Infinity");
|
|
196
237
|
});
|
|
197
238
|
it("should apply precision to numbers inside objects and arrays", () => {
|
|
198
239
|
const data = { values: [1.111, 2.222, 3.333] };
|
|
199
|
-
const result = prettyPrintToString(data, {
|
|
240
|
+
const result = prettyPrintToString(data, {
|
|
241
|
+
width: 80,
|
|
242
|
+
useColor: false,
|
|
243
|
+
precision: 1,
|
|
244
|
+
});
|
|
200
245
|
expect(result).toBe("{values: [1.1, 2.2, 3.3]}");
|
|
201
246
|
});
|
|
202
|
-
it("should
|
|
247
|
+
it("should not pad integers with precision", () => {
|
|
203
248
|
const result = prettyPrintToString(42, { useColor: false, precision: 2 });
|
|
204
|
-
expect(result).toBe("42
|
|
249
|
+
expect(result).toBe("42");
|
|
205
250
|
});
|
|
206
251
|
it("should leave numbers unchanged when neither option is set", () => {
|
|
207
252
|
const result = prettyPrintToString(3.14159, { useColor: false });
|
|
@@ -218,4 +263,11 @@ describe("prettyPrintToString", () => {
|
|
|
218
263
|
/>
|
|
219
264
|
</g>`);
|
|
220
265
|
});
|
|
266
|
+
it("should print nested JSX elements with color enabled", () => {
|
|
267
|
+
// Nested JSX with children triggers the bug: phantom selfCloseTag/closeTag
|
|
268
|
+
// spans get registered in tagger.tag() but never appear in the formatted
|
|
269
|
+
// output, corrupting path-based marker matching in expand().
|
|
270
|
+
const element = React.createElement("g", { transform: "translate(0,0)" }, React.createElement("rect", { width: 10, height: 20 }), React.createElement("text", { x: 5 }, "hello"));
|
|
271
|
+
expect(() => prettyPrintToString(element, { width: 60, useColor: true })).not.toThrow();
|
|
272
|
+
});
|
|
221
273
|
});
|