@joshuahhh/pretty-print 0.0.3 → 0.0.4
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,6 +4,10 @@ export interface PrettyPrintOptions {
|
|
|
4
4
|
useColor?: boolean;
|
|
5
5
|
niceId?: boolean;
|
|
6
6
|
niceType?: boolean;
|
|
7
|
+
/** Number of digits after the decimal point for floating-point numbers (uses toFixed). */
|
|
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,0FAA0F;IAC1F,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 = 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);
|
|
@@ -154,6 +154,59 @@ describe("prettyPrintToString", () => {
|
|
|
154
154
|
});
|
|
155
155
|
expect(result).toBe('{type: "user", id: 1, name: "Alice"}');
|
|
156
156
|
});
|
|
157
|
+
it("should format numbers with precision (toFixed)", () => {
|
|
158
|
+
const result = prettyPrintToString(3.14159, { useColor: false, precision: 2 });
|
|
159
|
+
expect(result).toBe("3.14");
|
|
160
|
+
});
|
|
161
|
+
it("should format numbers with precision 0", () => {
|
|
162
|
+
const result = prettyPrintToString(3.7, { useColor: false, precision: 0 });
|
|
163
|
+
expect(result).toBe("4");
|
|
164
|
+
});
|
|
165
|
+
it("should pad with zeros when precision exceeds decimal places", () => {
|
|
166
|
+
const result = prettyPrintToString(1.5, { useColor: false, precision: 4 });
|
|
167
|
+
expect(result).toBe("1.5000");
|
|
168
|
+
});
|
|
169
|
+
it("should format numbers with significantDigits (toPrecision)", () => {
|
|
170
|
+
const result = prettyPrintToString(3.14159, { useColor: false, significantDigits: 4 });
|
|
171
|
+
expect(result).toBe("3.142");
|
|
172
|
+
});
|
|
173
|
+
it("should format large numbers with significantDigits", () => {
|
|
174
|
+
const result = prettyPrintToString(123456, { useColor: false, significantDigits: 3 });
|
|
175
|
+
expect(result).toBe("1.23e+5");
|
|
176
|
+
});
|
|
177
|
+
it("precision should take precedence over significantDigits", () => {
|
|
178
|
+
const result = prettyPrintToString(3.14159, {
|
|
179
|
+
useColor: false,
|
|
180
|
+
precision: 1,
|
|
181
|
+
significantDigits: 5,
|
|
182
|
+
});
|
|
183
|
+
expect(result).toBe("3.1");
|
|
184
|
+
});
|
|
185
|
+
it("should not apply precision to Infinity", () => {
|
|
186
|
+
const result = prettyPrintToString(Infinity, { useColor: false, precision: 2 });
|
|
187
|
+
expect(result).toBe("Infinity");
|
|
188
|
+
});
|
|
189
|
+
it("should not apply precision to NaN", () => {
|
|
190
|
+
const result = prettyPrintToString(NaN, { useColor: false, precision: 2 });
|
|
191
|
+
expect(result).toBe("NaN");
|
|
192
|
+
});
|
|
193
|
+
it("should not apply significantDigits to -Infinity", () => {
|
|
194
|
+
const result = prettyPrintToString(-Infinity, { useColor: false, significantDigits: 3 });
|
|
195
|
+
expect(result).toBe("-Infinity");
|
|
196
|
+
});
|
|
197
|
+
it("should apply precision to numbers inside objects and arrays", () => {
|
|
198
|
+
const data = { values: [1.111, 2.222, 3.333] };
|
|
199
|
+
const result = prettyPrintToString(data, { width: 80, useColor: false, precision: 1 });
|
|
200
|
+
expect(result).toBe("{values: [1.1, 2.2, 3.3]}");
|
|
201
|
+
});
|
|
202
|
+
it("should format integers with precision", () => {
|
|
203
|
+
const result = prettyPrintToString(42, { useColor: false, precision: 2 });
|
|
204
|
+
expect(result).toBe("42.00");
|
|
205
|
+
});
|
|
206
|
+
it("should leave numbers unchanged when neither option is set", () => {
|
|
207
|
+
const result = prettyPrintToString(3.14159, { useColor: false });
|
|
208
|
+
expect(result).toBe("3.14159");
|
|
209
|
+
});
|
|
157
210
|
it("should keep opening tag together when it fits", () => {
|
|
158
211
|
const element = React.createElement("g", { "data-path": "/" }, React.createElement("circle", { cx: 0, cy: 0 }));
|
|
159
212
|
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.4",
|
|
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"
|