Skip to content

Commit

Permalink
Expose deno.inspect (denoland#1378)
Browse files Browse the repository at this point in the history
  • Loading branch information
kevinkassimo authored and ry committed Dec 20, 2018
1 parent 1cd18a9 commit 419000d
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 1 deletion.
23 changes: 23 additions & 0 deletions js/console.ts
Original file line number Diff line number Diff line change
Expand Up @@ -474,3 +474,26 @@ export class Console {
this.info(`${label}: ${duration}ms`);
};
}

/**
* inspect() converts input into string that has the same format
* as printed by console.log(...);
*/
export function inspect(
value: any, // tslint:disable-line:no-any
options?: ConsoleOptions
) {
const opts = options || {};
if (typeof value === "string") {
return value;
} else {
return stringify(
value,
// tslint:disable-next-line:no-any
new Set<any>(),
0,
// tslint:disable-next-line:triple-equals
opts.depth != undefined ? opts.depth : DEFAULT_MAX_DEPTH
);
}
}
9 changes: 8 additions & 1 deletion js/console_test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Copyright 2018 the Deno authors. All rights reserved. MIT license.
import { test, assert, assertEqual } from "./test_util.ts";
import { stringifyArgs } from "./console.ts";
import { stringifyArgs, inspect } from "./console.ts";

import { Console } from "./console.ts";
import { libdeno } from "./libdeno";
Expand Down Expand Up @@ -120,6 +120,8 @@ test(function consoleTestStringifyCircular() {
// tslint:disable-next-line:max-line-length
"Console { printFunc: [Function], log: [Function], debug: [Function], info: [Function], dir: [Function], warn: [Function], error: [Function], assert: [Function], count: [Function], countReset: [Function], time: [Function], timeLog: [Function], timeEnd: [Function] }"
);
// test inspect is working the same
assertEqual(inspect(nestedObj), nestedObjExpected);
});

test(function consoleTestStringifyWithDepth() {
Expand All @@ -138,6 +140,11 @@ test(function consoleTestStringifyWithDepth() {
stringifyArgs([nestedObj], { depth: null }),
"{ a: { b: { c: { d: [Object] } } } }"
);
// test inspect is working the same way
assertEqual(
inspect(nestedObj, { depth: 4 }),
"{ a: { b: { c: { d: [Object] } } } }"
);
});

test(function consoleTestCallToStringOnLabel() {
Expand Down
1 change: 1 addition & 0 deletions js/deno.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ export { connect, dial, listen, Listener, Conn } from "./net";
export { metrics } from "./metrics";
export { resources } from "./resources";
export { run, RunOptions, Process, ProcessStatus } from "./process";
export { inspect } from "./console";
export const args: string[] = [];

// Provide the compiler API in an obfuscated way
Expand Down

0 comments on commit 419000d

Please sign in to comment.