D
Deno

help

Custom console.log formatting for a class/object

NNeTT7/15/2023
Is there a way to change the way an object/class is printed in the console? For example, instead of
[1, 2, 3, 4]
[1, 2, 3, 4]
, I want the particular object to be printed as
[1, 2
3, 4]
[1, 2
3, 4]
Yybabts7/15/2023
you can specify some options for how you want the object to be displayed https://deno.land/api@v1.35.1?s=Deno.InspectOptions
Deno
Deno.InspectOptions | Runtime APIs | Deno
Option which can be specified when performing {@linkcode Deno.inspect}.
Yybabts7/15/2023
if you want to have a more specific way of displaying your object, you can use the Deno.customInspect symbol to implement your own way of inspecting the object https://deno.land/manual@v1.35.1/basics/testing/snapshot_testing#serialization-with--denocustominspect
Deno
Snapshots | Manual | Deno
The Deno standard library comes with a snapshot module, which enables developers to write tests which assert a value against a reference snap
Yybabts7/15/2023
Here's a little example it showing off
const myarray = [1, 2, 3, 4, 5, 6];

console.log(myarray);
console.log(Deno.inspect(myarray, {
breakLength: 28,
}));
Object.defineProperty(myarray, Symbol.for("Deno.customInspect"), {
value() {
const splits = String(this).split(",");
let str = "";
for (let i = 0; i < splits.length; i += 2) {
str += (i !== 0 ? " " : "") + (splits[i] + ", " + splits[i + 1] ?? "") +
(i > splits.length / 2 ? "" : "\n");
}
return `[${str}]`;
},
});
console.log(myarray);
const myarray = [1, 2, 3, 4, 5, 6];

console.log(myarray);
console.log(Deno.inspect(myarray, {
breakLength: 28,
}));
Object.defineProperty(myarray, Symbol.for("Deno.customInspect"), {
value() {
const splits = String(this).split(",");
let str = "";
for (let i = 0; i < splits.length; i += 2) {
str += (i !== 0 ? " " : "") + (splits[i] + ", " + splits[i + 1] ?? "") +
(i > splits.length / 2 ? "" : "\n");
}
return `[${str}]`;
},
});
console.log(myarray);
stdout
[ 1, 2, 3, 4, 5, 6 ]
[
1,
2,
3,
4,
5,
6
]
[1, 2
3, 4
5, 6]
[ 1, 2, 3, 4, 5, 6 ]
[
1,
2,
3,
4,
5,
6
]
[1, 2
3, 4
5, 6]
you can also define it for specific object classes as well if you want, or implement the symbol in your own class
Object.defineProperty(Array.prototype, Symbol.for("Deno.customInspect"), { ... })
Object.defineProperty(Array.prototype, Symbol.for("Deno.customInspect"), { ... })
NNeTT7/15/2023
Ooh thank you I'll take a look into it

Looking for more? Join the community!

Recommended Posts
Is there a way to write to a file by replacing bytes?Like "I am a looong file" > write('Test') > 'Test a looong file'std/http/server: URI too long —how to avoid loading those?Using https://deno.land/std@0.194.0/http/server.ts, is there a way to reject long URIs before they aWorker: TS2304 [ERROR]: Cannot find name 'postMessage'When type-checking a worker script that uses the global `postMessage()` method, `deno check` gives aHow to recover from worker death? It terminates my main program…In the error message handler, I replace the dead worker with a new one, but it kills my program eithIs there anyone using kv with pentagon ORM and Zod ?I've tried using pentagon for Deno kv in a Fresh project, and it seems like it's not working.Prefix Kv keys with a base partCurrently I add `base()` to all keys: ``` kv.set([base(), "foo"], value) ``` Instead of manually adCaching old versionsWhy is deno caching old versions even tho the version is specifieddeno check: Module '"internal:///missing_dependency.d.ts"' has no exported member...The sequence of events: - Upgraded to Deno 1.35.0 - ran `deno check` on my code - got an error like cargo compile size 160MWhen using `cargo install deno` the result is 160M, if there's info on shrinking this somewhere pleaDeno type enforcementHey, I have a question about the type checking. ```ts function logging(message : number) { consoDNS Records and Denoso I am confused I'm building an application that uses Steam's web API and am looking at why the resFresh pattern helpCould anyone possibly help guide me to understand the 'freshest' pattern for the following, please? Deno seems to assume wrong return type (Puppeteer)With the following code, I get an type error which I do not expect: ```ts const element: ElementHanCreating my first Deno moduleHave never published to NPM or Denoland before, so I'm having some problems figuring shit out. I'm gSanitize multiple test stepsIs it possible to sanitize multiple steps inside of one test. I want to test my DB with somthing likFFI string corrupted?Is there anything i'm doing wrong here? I'm confused on why it's crashing on setVerisonUE5 in partic