mirror of
https://github.com/denoland/deno.git
synced 2025-03-03 17:34:47 -05:00
Expose deno.inspect (#1378)
This commit is contained in:
parent
1cd18a9ac6
commit
419000d556
3 changed files with 32 additions and 1 deletions
|
@ -474,3 +474,26 @@ export class Console {
|
||||||
this.info(`${label}: ${duration}ms`);
|
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
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
// Copyright 2018 the Deno authors. All rights reserved. MIT license.
|
// Copyright 2018 the Deno authors. All rights reserved. MIT license.
|
||||||
import { test, assert, assertEqual } from "./test_util.ts";
|
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 { Console } from "./console.ts";
|
||||||
import { libdeno } from "./libdeno";
|
import { libdeno } from "./libdeno";
|
||||||
|
@ -120,6 +120,8 @@ test(function consoleTestStringifyCircular() {
|
||||||
// tslint:disable-next-line:max-line-length
|
// 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] }"
|
"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() {
|
test(function consoleTestStringifyWithDepth() {
|
||||||
|
@ -138,6 +140,11 @@ test(function consoleTestStringifyWithDepth() {
|
||||||
stringifyArgs([nestedObj], { depth: null }),
|
stringifyArgs([nestedObj], { depth: null }),
|
||||||
"{ a: { b: { c: { d: [Object] } } } }"
|
"{ 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() {
|
test(function consoleTestCallToStringOnLabel() {
|
||||||
|
|
|
@ -52,6 +52,7 @@ export { connect, dial, listen, Listener, Conn } from "./net";
|
||||||
export { metrics } from "./metrics";
|
export { metrics } from "./metrics";
|
||||||
export { resources } from "./resources";
|
export { resources } from "./resources";
|
||||||
export { run, RunOptions, Process, ProcessStatus } from "./process";
|
export { run, RunOptions, Process, ProcessStatus } from "./process";
|
||||||
|
export { inspect } from "./console";
|
||||||
export const args: string[] = [];
|
export const args: string[] = [];
|
||||||
|
|
||||||
// Provide the compiler API in an obfuscated way
|
// Provide the compiler API in an obfuscated way
|
||||||
|
|
Loading…
Add table
Reference in a new issue