0
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2025-03-03 09:31:22 -05:00

Expose deno.inspect (#1378)

This commit is contained in:
Kevin (Kun) "Kassimo" Qian 2018-12-19 22:08:49 -05:00 committed by Ryan Dahl
parent 1cd18a9ac6
commit 419000d556
3 changed files with 32 additions and 1 deletions

View file

@ -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
);
}
}

View file

@ -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";
@ -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() {
@ -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() {

View file

@ -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