@joshuahhh/pretty-print 0.0.3 → 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 +4 -0
- package/dist/pretty-print.d.ts.map +1 -1
- package/dist/pretty-print.js +11 -1
- package/dist/pretty-print.test.js +106 -8
- package/package.json +3 -2
package/dist/pretty-print.d.ts
CHANGED
|
@@ -4,6 +4,10 @@ export interface PrettyPrintOptions {
|
|
|
4
4
|
useColor?: boolean;
|
|
5
5
|
niceId?: boolean;
|
|
6
6
|
niceType?: boolean;
|
|
7
|
+
/** Maximum number of digits after the decimal point for floating-point numbers. */
|
|
8
|
+
precision?: number;
|
|
9
|
+
/** Number of significant digits for floating-point numbers (uses toPrecision). Ignored if precision is set. */
|
|
10
|
+
significantDigits?: number;
|
|
7
11
|
}
|
|
8
12
|
/**
|
|
9
13
|
* Converts ANSI color codes to browser console %c format with CSS styles.
|
|
@@ -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;
|
|
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
|
@@ -209,7 +209,17 @@ function prettyPrintToDoc(value, tagger, options, visited = new Set(), path = []
|
|
|
209
209
|
return colorize(JSON.stringify(value), "string", path);
|
|
210
210
|
}
|
|
211
211
|
if (typeof value === "number") {
|
|
212
|
-
|
|
212
|
+
let numStr;
|
|
213
|
+
if (Number.isFinite(value) && options.precision != null) {
|
|
214
|
+
numStr = String(parseFloat(value.toFixed(options.precision)));
|
|
215
|
+
}
|
|
216
|
+
else if (Number.isFinite(value) && options.significantDigits != null) {
|
|
217
|
+
numStr = value.toPrecision(options.significantDigits);
|
|
218
|
+
}
|
|
219
|
+
else {
|
|
220
|
+
numStr = String(value);
|
|
221
|
+
}
|
|
222
|
+
return colorize(numStr, "number", path);
|
|
213
223
|
}
|
|
214
224
|
if (typeof value === "boolean") {
|
|
215
225
|
return colorize(String(value), "boolean", path);
|
|
@@ -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,6 +180,78 @@ describe("prettyPrintToString", () => {
|
|
|
154
180
|
});
|
|
155
181
|
expect(result).toBe('{type: "user", id: 1, name: "Alice"}');
|
|
156
182
|
});
|
|
183
|
+
it("should limit decimal places with precision", () => {
|
|
184
|
+
const result = prettyPrintToString(3.14159, {
|
|
185
|
+
useColor: false,
|
|
186
|
+
precision: 2,
|
|
187
|
+
});
|
|
188
|
+
expect(result).toBe("3.14");
|
|
189
|
+
});
|
|
190
|
+
it("should format numbers with precision 0", () => {
|
|
191
|
+
const result = prettyPrintToString(3.7, { useColor: false, precision: 0 });
|
|
192
|
+
expect(result).toBe("4");
|
|
193
|
+
});
|
|
194
|
+
it("should not pad with zeros when precision exceeds decimal places", () => {
|
|
195
|
+
const result = prettyPrintToString(1.5, { useColor: false, precision: 4 });
|
|
196
|
+
expect(result).toBe("1.5");
|
|
197
|
+
});
|
|
198
|
+
it("should format numbers with significantDigits (toPrecision)", () => {
|
|
199
|
+
const result = prettyPrintToString(3.14159, {
|
|
200
|
+
useColor: false,
|
|
201
|
+
significantDigits: 4,
|
|
202
|
+
});
|
|
203
|
+
expect(result).toBe("3.142");
|
|
204
|
+
});
|
|
205
|
+
it("should format large numbers with significantDigits", () => {
|
|
206
|
+
const result = prettyPrintToString(123456, {
|
|
207
|
+
useColor: false,
|
|
208
|
+
significantDigits: 3,
|
|
209
|
+
});
|
|
210
|
+
expect(result).toBe("1.23e+5");
|
|
211
|
+
});
|
|
212
|
+
it("precision should take precedence over significantDigits", () => {
|
|
213
|
+
const result = prettyPrintToString(3.14159, {
|
|
214
|
+
useColor: false,
|
|
215
|
+
precision: 1,
|
|
216
|
+
significantDigits: 5,
|
|
217
|
+
});
|
|
218
|
+
expect(result).toBe("3.1");
|
|
219
|
+
});
|
|
220
|
+
it("should not apply precision to Infinity", () => {
|
|
221
|
+
const result = prettyPrintToString(Infinity, {
|
|
222
|
+
useColor: false,
|
|
223
|
+
precision: 2,
|
|
224
|
+
});
|
|
225
|
+
expect(result).toBe("Infinity");
|
|
226
|
+
});
|
|
227
|
+
it("should not apply precision to NaN", () => {
|
|
228
|
+
const result = prettyPrintToString(NaN, { useColor: false, precision: 2 });
|
|
229
|
+
expect(result).toBe("NaN");
|
|
230
|
+
});
|
|
231
|
+
it("should not apply significantDigits to -Infinity", () => {
|
|
232
|
+
const result = prettyPrintToString(-Infinity, {
|
|
233
|
+
useColor: false,
|
|
234
|
+
significantDigits: 3,
|
|
235
|
+
});
|
|
236
|
+
expect(result).toBe("-Infinity");
|
|
237
|
+
});
|
|
238
|
+
it("should apply precision to numbers inside objects and arrays", () => {
|
|
239
|
+
const data = { values: [1.111, 2.222, 3.333] };
|
|
240
|
+
const result = prettyPrintToString(data, {
|
|
241
|
+
width: 80,
|
|
242
|
+
useColor: false,
|
|
243
|
+
precision: 1,
|
|
244
|
+
});
|
|
245
|
+
expect(result).toBe("{values: [1.1, 2.2, 3.3]}");
|
|
246
|
+
});
|
|
247
|
+
it("should not pad integers with precision", () => {
|
|
248
|
+
const result = prettyPrintToString(42, { useColor: false, precision: 2 });
|
|
249
|
+
expect(result).toBe("42");
|
|
250
|
+
});
|
|
251
|
+
it("should leave numbers unchanged when neither option is set", () => {
|
|
252
|
+
const result = prettyPrintToString(3.14159, { useColor: false });
|
|
253
|
+
expect(result).toBe("3.14159");
|
|
254
|
+
});
|
|
157
255
|
it("should keep opening tag together when it fits", () => {
|
|
158
256
|
const element = React.createElement("g", { "data-path": "/" }, React.createElement("circle", { cx: 0, cy: 0 }));
|
|
159
257
|
const result = prettyPrintToString(element, { width: 20, useColor: false });
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@joshuahhh/pretty-print",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.5",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"main": "./dist/index.js",
|
|
6
6
|
"types": "./dist/index.d.ts",
|
|
@@ -16,7 +16,8 @@
|
|
|
16
16
|
"scripts": {
|
|
17
17
|
"build": "tsc",
|
|
18
18
|
"test": "vitest run",
|
|
19
|
-
"prepublishOnly": "npm run build"
|
|
19
|
+
"prepublishOnly": "npm run build",
|
|
20
|
+
"release": "npm test && npm version patch && npm publish"
|
|
20
21
|
},
|
|
21
22
|
"dependencies": {
|
|
22
23
|
"prettier": "^3.8.1"
|