@logtape/pretty 2.4.0-dev.904 → 2.4.0-dev.918

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.
@@ -0,0 +1,76 @@
1
+
2
+ //#region src/circular.ts
3
+ /**
4
+ * Creates a {@link JSON.stringify} replacer which renders circular references
5
+ * as the string `"[Circular]"` instead of letting {@link JSON.stringify} throw
6
+ * a `TypeError`.
7
+ *
8
+ * The returned replacer keeps a stack of the values which are currently being
9
+ * serialized, so a value that merely appears more than once, such as the same
10
+ * object held by two sibling properties, is still serialized in full. Only a
11
+ * value which contains itself is replaced.
12
+ *
13
+ * The replacer is stateful; create a new one for every {@link JSON.stringify}
14
+ * call.
15
+ *
16
+ * Cycles are detected as the replacer sees them, which is *after* a `toJSON()`
17
+ * method has run. A `toJSON()` method which allocates a fresh object referring
18
+ * back to its owner on every call therefore still recurses without bound, just
19
+ * as it does with a plain {@link JSON.stringify} call.
20
+ *
21
+ * @param inner An optional replacer to compose with. It runs first, and the
22
+ * value it returns is what gets serialized. Cycles are detected
23
+ * in both the original and the returned values, so a replacer
24
+ * which allocates a new object for every visit (as the error
25
+ * serializer does) cannot hide a cycle.
26
+ * @returns A replacer function to pass to {@link JSON.stringify}.
27
+ */
28
+ function createCircularReplacer(inner) {
29
+ const sources = [];
30
+ const results = [];
31
+ return function(key, value) {
32
+ const result = inner === void 0 ? value : inner.call(this, key, value);
33
+ if (typeof result !== "object" || result === null) return result;
34
+ while (results.length > 0 && results[results.length - 1] !== this) {
35
+ results.pop();
36
+ sources.pop();
37
+ }
38
+ for (let i = 0; i < results.length; i++) if (results[i] === result || sources[i] === value) return "[Circular]";
39
+ sources.push(value);
40
+ results.push(result);
41
+ return result;
42
+ };
43
+ }
44
+ /**
45
+ * Serializes a value the way {@link JSON.stringify} does, except that circular
46
+ * references are rendered as the string `"[Circular]"` instead of throwing
47
+ * a `TypeError`.
48
+ *
49
+ * The plain {@link JSON.stringify} call is attempted first and the cycle-safe
50
+ * replacer is installed only if it throws, so values without cycles (by far the
51
+ * common case) cost exactly what they cost before. The price is that a value
52
+ * which does contain a cycle is traversed twice, so its getters and `toJSON()`
53
+ * methods run twice. If the second attempt fails as well, its error is thrown;
54
+ * values {@link JSON.stringify} cannot serialize at all, such as `BigInt`s,
55
+ * therefore keep failing as before.
56
+ *
57
+ * Like {@link JSON.stringify}, this returns `undefined` rather than a string
58
+ * for `undefined`, functions, and symbols. The return type says so, unlike
59
+ * the built-in's own declaration, so that every caller has to pick its own
60
+ * fallback for those values.
61
+ *
62
+ * @param value The value to serialize.
63
+ * @param replacer An optional replacer to apply, as in {@link JSON.stringify}.
64
+ * @param space An optional indentation, as in {@link JSON.stringify}.
65
+ * @returns The JSON representation of the value.
66
+ */
67
+ function stringifyWithoutCycles(value, replacer, space) {
68
+ try {
69
+ return JSON.stringify(value, replacer, space);
70
+ } catch {
71
+ return JSON.stringify(value, createCircularReplacer(replacer), space);
72
+ }
73
+ }
74
+
75
+ //#endregion
76
+ exports.stringifyWithoutCycles = stringifyWithoutCycles;
@@ -0,0 +1,76 @@
1
+ //#region src/circular.ts
2
+ /**
3
+ * Creates a {@link JSON.stringify} replacer which renders circular references
4
+ * as the string `"[Circular]"` instead of letting {@link JSON.stringify} throw
5
+ * a `TypeError`.
6
+ *
7
+ * The returned replacer keeps a stack of the values which are currently being
8
+ * serialized, so a value that merely appears more than once, such as the same
9
+ * object held by two sibling properties, is still serialized in full. Only a
10
+ * value which contains itself is replaced.
11
+ *
12
+ * The replacer is stateful; create a new one for every {@link JSON.stringify}
13
+ * call.
14
+ *
15
+ * Cycles are detected as the replacer sees them, which is *after* a `toJSON()`
16
+ * method has run. A `toJSON()` method which allocates a fresh object referring
17
+ * back to its owner on every call therefore still recurses without bound, just
18
+ * as it does with a plain {@link JSON.stringify} call.
19
+ *
20
+ * @param inner An optional replacer to compose with. It runs first, and the
21
+ * value it returns is what gets serialized. Cycles are detected
22
+ * in both the original and the returned values, so a replacer
23
+ * which allocates a new object for every visit (as the error
24
+ * serializer does) cannot hide a cycle.
25
+ * @returns A replacer function to pass to {@link JSON.stringify}.
26
+ */
27
+ function createCircularReplacer(inner) {
28
+ const sources = [];
29
+ const results = [];
30
+ return function(key, value) {
31
+ const result = inner === void 0 ? value : inner.call(this, key, value);
32
+ if (typeof result !== "object" || result === null) return result;
33
+ while (results.length > 0 && results[results.length - 1] !== this) {
34
+ results.pop();
35
+ sources.pop();
36
+ }
37
+ for (let i = 0; i < results.length; i++) if (results[i] === result || sources[i] === value) return "[Circular]";
38
+ sources.push(value);
39
+ results.push(result);
40
+ return result;
41
+ };
42
+ }
43
+ /**
44
+ * Serializes a value the way {@link JSON.stringify} does, except that circular
45
+ * references are rendered as the string `"[Circular]"` instead of throwing
46
+ * a `TypeError`.
47
+ *
48
+ * The plain {@link JSON.stringify} call is attempted first and the cycle-safe
49
+ * replacer is installed only if it throws, so values without cycles (by far the
50
+ * common case) cost exactly what they cost before. The price is that a value
51
+ * which does contain a cycle is traversed twice, so its getters and `toJSON()`
52
+ * methods run twice. If the second attempt fails as well, its error is thrown;
53
+ * values {@link JSON.stringify} cannot serialize at all, such as `BigInt`s,
54
+ * therefore keep failing as before.
55
+ *
56
+ * Like {@link JSON.stringify}, this returns `undefined` rather than a string
57
+ * for `undefined`, functions, and symbols. The return type says so, unlike
58
+ * the built-in's own declaration, so that every caller has to pick its own
59
+ * fallback for those values.
60
+ *
61
+ * @param value The value to serialize.
62
+ * @param replacer An optional replacer to apply, as in {@link JSON.stringify}.
63
+ * @param space An optional indentation, as in {@link JSON.stringify}.
64
+ * @returns The JSON representation of the value.
65
+ */
66
+ function stringifyWithoutCycles(value, replacer, space) {
67
+ try {
68
+ return JSON.stringify(value, replacer, space);
69
+ } catch {
70
+ return JSON.stringify(value, createCircularReplacer(replacer), space);
71
+ }
72
+ }
73
+
74
+ //#endregion
75
+ export { stringifyWithoutCycles };
76
+ //# sourceMappingURL=circular.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"circular.js","names":["inner?: JsonReplacer","sources: unknown[]","results: unknown[]","key: string","value: unknown","replacer?: JsonReplacer","space?: string | number"],"sources":["../src/circular.ts"],"sourcesContent":["/**\n * A replacer function in the shape {@link JSON.stringify} accepts.\n */\nexport type JsonReplacer = (\n this: unknown,\n key: string,\n value: unknown,\n) => unknown;\n\n/**\n * Creates a {@link JSON.stringify} replacer which renders circular references\n * as the string `\"[Circular]\"` instead of letting {@link JSON.stringify} throw\n * a `TypeError`.\n *\n * The returned replacer keeps a stack of the values which are currently being\n * serialized, so a value that merely appears more than once, such as the same\n * object held by two sibling properties, is still serialized in full. Only a\n * value which contains itself is replaced.\n *\n * The replacer is stateful; create a new one for every {@link JSON.stringify}\n * call.\n *\n * Cycles are detected as the replacer sees them, which is *after* a `toJSON()`\n * method has run. A `toJSON()` method which allocates a fresh object referring\n * back to its owner on every call therefore still recurses without bound, just\n * as it does with a plain {@link JSON.stringify} call.\n *\n * @param inner An optional replacer to compose with. It runs first, and the\n * value it returns is what gets serialized. Cycles are detected\n * in both the original and the returned values, so a replacer\n * which allocates a new object for every visit (as the error\n * serializer does) cannot hide a cycle.\n * @returns A replacer function to pass to {@link JSON.stringify}.\n */\nexport function createCircularReplacer(inner?: JsonReplacer): JsonReplacer {\n const sources: unknown[] = [];\n const results: unknown[] = [];\n return function (this: unknown, key: string, value: unknown): unknown {\n const result = inner === undefined ? value : inner.call(this, key, value);\n if (typeof result !== \"object\" || result === null) return result;\n // Unwind to the value which holds the one being serialized now; whatever\n // remains on the stack is an ancestor of it.\n while (results.length > 0 && results[results.length - 1] !== this) {\n results.pop();\n sources.pop();\n }\n for (let i = 0; i < results.length; i++) {\n if (results[i] === result || sources[i] === value) return \"[Circular]\";\n }\n sources.push(value);\n results.push(result);\n return result;\n };\n}\n\n/**\n * Serializes a value the way {@link JSON.stringify} does, except that circular\n * references are rendered as the string `\"[Circular]\"` instead of throwing\n * a `TypeError`.\n *\n * The plain {@link JSON.stringify} call is attempted first and the cycle-safe\n * replacer is installed only if it throws, so values without cycles (by far the\n * common case) cost exactly what they cost before. The price is that a value\n * which does contain a cycle is traversed twice, so its getters and `toJSON()`\n * methods run twice. If the second attempt fails as well, its error is thrown;\n * values {@link JSON.stringify} cannot serialize at all, such as `BigInt`s,\n * therefore keep failing as before.\n *\n * Like {@link JSON.stringify}, this returns `undefined` rather than a string\n * for `undefined`, functions, and symbols. The return type says so, unlike\n * the built-in's own declaration, so that every caller has to pick its own\n * fallback for those values.\n *\n * @param value The value to serialize.\n * @param replacer An optional replacer to apply, as in {@link JSON.stringify}.\n * @param space An optional indentation, as in {@link JSON.stringify}.\n * @returns The JSON representation of the value.\n */\nexport function stringifyWithoutCycles(\n value: unknown,\n replacer?: JsonReplacer,\n space?: string | number,\n): string | undefined {\n try {\n return JSON.stringify(value, replacer, space);\n } catch {\n return JSON.stringify(value, createCircularReplacer(replacer), space);\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;AAkCA,SAAgB,uBAAuBA,OAAoC;CACzE,MAAMC,UAAqB,CAAE;CAC7B,MAAMC,UAAqB,CAAE;AAC7B,QAAO,SAAyBC,KAAaC,OAAyB;EACpE,MAAM,SAAS,mBAAsB,QAAQ,MAAM,KAAK,MAAM,KAAK,MAAM;AACzE,aAAW,WAAW,YAAY,WAAW,KAAM,QAAO;AAG1D,SAAO,QAAQ,SAAS,KAAK,QAAQ,QAAQ,SAAS,OAAO,MAAM;AACjE,WAAQ,KAAK;AACb,WAAQ,KAAK;EACd;AACD,OAAK,IAAI,IAAI,GAAG,IAAI,QAAQ,QAAQ,IAClC,KAAI,QAAQ,OAAO,UAAU,QAAQ,OAAO,MAAO,QAAO;AAE5D,UAAQ,KAAK,MAAM;AACnB,UAAQ,KAAK,OAAO;AACpB,SAAO;CACR;AACF;;;;;;;;;;;;;;;;;;;;;;;;AAyBD,SAAgB,uBACdA,OACAC,UACAC,OACoB;AACpB,KAAI;AACF,SAAO,KAAK,UAAU,OAAO,UAAU,MAAM;CAC9C,QAAO;AACN,SAAO,KAAK,UAAU,OAAO,uBAAuB,SAAS,EAAE,MAAM;CACtE;AACF"}
package/dist/util.cjs CHANGED
@@ -1,8 +1,9 @@
1
+ const require_circular = require('./circular.cjs');
1
2
 
2
3
  //#region src/util.ts
3
4
  function inspect(obj, options) {
4
5
  const indent = options?.compact === true ? void 0 : 2;
5
- return JSON.stringify(obj, null, indent);
6
+ return require_circular.stringifyWithoutCycles(obj, void 0, indent) ?? "undefined";
6
7
  }
7
8
 
8
9
  //#endregion
@@ -1 +1 @@
1
- {"version":3,"file":"util.d.cts","names":[],"sources":["../src/util.ts"],"sourcesContent":[],"mappings":";UAAiB,cAAA;EAAA,MAAA,CAAA,EAAA,OAAA;EASD,KAAA,CAAA,EAAA,MAAO,GAAA,IAAyB;;;;;;iBAAhC,OAAA,yBAAgC"}
1
+ {"version":3,"file":"util.d.cts","names":[],"sources":["../src/util.ts"],"sourcesContent":[],"mappings":";UAEiB,cAAA;EAAA,MAAA,CAAA,EAAA,OAAA;EASD,KAAA,CAAA,EAAA,MAAO,GAAA,IAAyB;;;;;;iBAAhC,OAAA,yBAAgC"}
@@ -1 +1 @@
1
- {"version":3,"file":"util.d.ts","names":[],"sources":["../src/util.ts"],"sourcesContent":[],"mappings":";UAAiB,cAAA;EAAA,MAAA,CAAA,EAAA,OAAA;EASD,KAAA,CAAA,EAAA,MAAO,GAAA,IAAyB;;;;;;iBAAhC,OAAA,yBAAgC"}
1
+ {"version":3,"file":"util.d.ts","names":[],"sources":["../src/util.ts"],"sourcesContent":[],"mappings":";UAEiB,cAAA;EAAA,MAAA,CAAA,EAAA,OAAA;EASD,KAAA,CAAA,EAAA,MAAO,GAAA,IAAyB;;;;;;iBAAhC,OAAA,yBAAgC"}
@@ -1,3 +1,4 @@
1
+ const require_circular = require('./circular.cjs');
1
2
 
2
3
  //#region src/util.deno.ts
3
4
  function inspect(obj, options) {
@@ -10,7 +11,7 @@ function inspect(obj, options) {
10
11
  });
11
12
  else {
12
13
  const indent = options?.compact === true ? void 0 : 2;
13
- return JSON.stringify(obj, null, indent);
14
+ return require_circular.stringifyWithoutCycles(obj, void 0, indent) ?? "undefined";
14
15
  }
15
16
  }
16
17
 
@@ -1 +1 @@
1
- {"version":3,"file":"util.deno.d.cts","names":[],"sources":["../src/util.deno.ts"],"sourcesContent":[],"mappings":";UAAiB,cAAA;EAAA,MAAA,CAAA,EAAA,OAAA;EASD,KAAA,CAAA,EAAA,MAAO,GAAA,IAAyB;;;;;;iBAAhC,OAAA,yBAAgC"}
1
+ {"version":3,"file":"util.deno.d.cts","names":[],"sources":["../src/util.deno.ts"],"sourcesContent":[],"mappings":";UAEiB,cAAA;EAAA,MAAA,CAAA,EAAA,OAAA;EASD,KAAA,CAAA,EAAA,MAAO,GAAA,IAAyB;;;;;;iBAAhC,OAAA,yBAAgC"}
@@ -1 +1 @@
1
- {"version":3,"file":"util.deno.d.ts","names":[],"sources":["../src/util.deno.ts"],"sourcesContent":[],"mappings":";UAAiB,cAAA;EAAA,MAAA,CAAA,EAAA,OAAA;EASD,KAAA,CAAA,EAAA,MAAO,GAAA,IAAyB;;;;;;iBAAhC,OAAA,yBAAgC"}
1
+ {"version":3,"file":"util.deno.d.ts","names":[],"sources":["../src/util.deno.ts"],"sourcesContent":[],"mappings":";UAEiB,cAAA;EAAA,MAAA,CAAA,EAAA,OAAA;EASD,KAAA,CAAA,EAAA,MAAO,GAAA,IAAyB;;;;;;iBAAhC,OAAA,yBAAgC"}
package/dist/util.deno.js CHANGED
@@ -1,3 +1,5 @@
1
+ import { stringifyWithoutCycles } from "./circular.js";
2
+
1
3
  //#region src/util.deno.ts
2
4
  function inspect(obj, options) {
3
5
  if ("Deno" in globalThis) return Deno.inspect(obj, {
@@ -9,7 +11,7 @@ function inspect(obj, options) {
9
11
  });
10
12
  else {
11
13
  const indent = options?.compact === true ? void 0 : 2;
12
- return JSON.stringify(obj, null, indent);
14
+ return stringifyWithoutCycles(obj, void 0, indent) ?? "undefined";
13
15
  }
14
16
  }
15
17
 
@@ -1 +1 @@
1
- {"version":3,"file":"util.deno.js","names":["obj: unknown","options?: InspectOptions"],"sources":["../src/util.deno.ts"],"sourcesContent":["export interface InspectOptions {\n colors?: boolean;\n depth?: number | null;\n compact?: boolean;\n getters?: boolean;\n showProxy?: boolean;\n [key: string]: unknown;\n}\n\nexport function inspect(obj: unknown, options?: InspectOptions): string {\n if (\"Deno\" in globalThis) {\n return Deno.inspect(obj, {\n colors: options?.colors,\n depth: options?.depth ?? undefined,\n compact: options?.compact ?? true,\n getters: options?.getters,\n showProxy: options?.showProxy,\n });\n } else {\n const indent = options?.compact === true ? undefined : 2;\n return JSON.stringify(obj, null, indent);\n }\n}\n"],"mappings":";AASA,SAAgB,QAAQA,KAAcC,SAAkC;AACtE,KAAI,UAAU,WACZ,QAAO,KAAK,QAAQ,KAAK;EACvB,QAAQ,SAAS;EACjB,OAAO,SAAS;EAChB,SAAS,SAAS,WAAW;EAC7B,SAAS,SAAS;EAClB,WAAW,SAAS;CACrB,EAAC;MACG;EACL,MAAM,SAAS,SAAS,YAAY,gBAAmB;AACvD,SAAO,KAAK,UAAU,KAAK,MAAM,OAAO;CACzC;AACF"}
1
+ {"version":3,"file":"util.deno.js","names":["obj: unknown","options?: InspectOptions"],"sources":["../src/util.deno.ts"],"sourcesContent":["import { stringifyWithoutCycles } from \"./circular.ts\";\n\nexport interface InspectOptions {\n colors?: boolean;\n depth?: number | null;\n compact?: boolean;\n getters?: boolean;\n showProxy?: boolean;\n [key: string]: unknown;\n}\n\nexport function inspect(obj: unknown, options?: InspectOptions): string {\n if (\"Deno\" in globalThis) {\n return Deno.inspect(obj, {\n colors: options?.colors,\n depth: options?.depth ?? undefined,\n compact: options?.compact ?? true,\n getters: options?.getters,\n showProxy: options?.showProxy,\n });\n } else {\n const indent = options?.compact === true ? undefined : 2;\n return stringifyWithoutCycles(obj, undefined, indent) ??\n \"undefined\";\n }\n}\n"],"mappings":";;;AAWA,SAAgB,QAAQA,KAAcC,SAAkC;AACtE,KAAI,UAAU,WACZ,QAAO,KAAK,QAAQ,KAAK;EACvB,QAAQ,SAAS;EACjB,OAAO,SAAS;EAChB,SAAS,SAAS,WAAW;EAC7B,SAAS,SAAS;EAClB,WAAW,SAAS;CACrB,EAAC;MACG;EACL,MAAM,SAAS,SAAS,YAAY,gBAAmB;AACvD,SAAO,uBAAuB,aAAgB,OAAO,IACnD;CACH;AACF"}
package/dist/util.js CHANGED
@@ -1,7 +1,9 @@
1
+ import { stringifyWithoutCycles } from "./circular.js";
2
+
1
3
  //#region src/util.ts
2
4
  function inspect(obj, options) {
3
5
  const indent = options?.compact === true ? void 0 : 2;
4
- return JSON.stringify(obj, null, indent);
6
+ return stringifyWithoutCycles(obj, void 0, indent) ?? "undefined";
5
7
  }
6
8
 
7
9
  //#endregion
package/dist/util.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"util.js","names":["obj: unknown","options?: InspectOptions"],"sources":["../src/util.ts"],"sourcesContent":["export interface InspectOptions {\n colors?: boolean;\n depth?: number | null;\n compact?: boolean;\n getters?: boolean;\n showProxy?: boolean;\n [key: string]: unknown;\n}\n\nexport function inspect(obj: unknown, options?: InspectOptions): string {\n const indent = options?.compact === true ? undefined : 2;\n return JSON.stringify(obj, null, indent);\n}\n"],"mappings":";AASA,SAAgB,QAAQA,KAAcC,SAAkC;CACtE,MAAM,SAAS,SAAS,YAAY,gBAAmB;AACvD,QAAO,KAAK,UAAU,KAAK,MAAM,OAAO;AACzC"}
1
+ {"version":3,"file":"util.js","names":["obj: unknown","options?: InspectOptions"],"sources":["../src/util.ts"],"sourcesContent":["import { stringifyWithoutCycles } from \"./circular.ts\";\n\nexport interface InspectOptions {\n colors?: boolean;\n depth?: number | null;\n compact?: boolean;\n getters?: boolean;\n showProxy?: boolean;\n [key: string]: unknown;\n}\n\nexport function inspect(obj: unknown, options?: InspectOptions): string {\n const indent = options?.compact === true ? undefined : 2;\n return stringifyWithoutCycles(obj, undefined, indent) ?? \"undefined\";\n}\n"],"mappings":";;;AAWA,SAAgB,QAAQA,KAAcC,SAAkC;CACtE,MAAM,SAAS,SAAS,YAAY,gBAAmB;AACvD,QAAO,uBAAuB,aAAgB,OAAO,IAAI;AAC1D"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@logtape/pretty",
3
- "version": "2.4.0-dev.904+b25f1929",
3
+ "version": "2.4.0-dev.918+96f14d97",
4
4
  "description": "Beautiful text formatter for LogTape—perfect for local development",
5
5
  "keywords": [
6
6
  "logging",
@@ -78,7 +78,7 @@
78
78
  "dist/"
79
79
  ],
80
80
  "peerDependencies": {
81
- "@logtape/logtape": "^2.4.0-dev.904+b25f1929"
81
+ "@logtape/logtape": "^2.4.0-dev.918+96f14d97"
82
82
  },
83
83
  "devDependencies": {
84
84
  "@alinea/suite": "^0.6.3",