@joshuahhh/pretty-print 0.0.4 → 0.0.5
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;AAiUD,wBAAgB,mBAAmB,CACjC,KAAK,EAAE,OAAO,EACd,OAAO,GAAE,kBAAuB,GAC/B,MAAM,CAWR;AAGD,eAAO,MAAM,QAAQ;;;;;;;;;;;;;;;;;;;;;;;;CAYpB,CAAC"}
|
package/dist/pretty-print.js
CHANGED
|
@@ -211,7 +211,7 @@ function prettyPrintToDoc(value, tagger, options, visited = new Set(), path = []
|
|
|
211
211
|
if (typeof value === "number") {
|
|
212
212
|
let numStr;
|
|
213
213
|
if (Number.isFinite(value) && options.precision != null) {
|
|
214
|
-
numStr = value.toFixed(options.precision);
|
|
214
|
+
numStr = String(parseFloat(value.toFixed(options.precision)));
|
|
215
215
|
}
|
|
216
216
|
else if (Number.isFinite(value) && options.significantDigits != null) {
|
|
217
217
|
numStr = value.toPrecision(options.significantDigits);
|
|
@@ -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 });
|