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

[console] Use constructor.name to print out function type (#664)

This commit is contained in:
Kevin (Kun) "Kassimo" Qian 2018-09-24 07:06:48 -07:00 committed by Ryan Dahl
parent 9203e983d1
commit c124db4701
2 changed files with 13 additions and 3 deletions

View file

@ -23,11 +23,13 @@ function stringify(ctx: ConsoleContext, value: any): string {
case "symbol":
return String(value);
case "function":
// Might be Function/AsyncFunction/GeneratorFunction
const cstrName = value.__proto__.constructor.name;
if (value.name && value.name !== "anonymous") {
// from MDN spec
return `[Function: ${value.name}]`;
return `[${cstrName}: ${value.name}]`;
}
return "[Function]";
return `[${cstrName}]`;
case "object":
if (value === null) {
return "null";

View file

@ -43,6 +43,8 @@ test(function consoleTestStringifyCircular() {
bool: true,
str: "a",
method() {},
async asyncMethod() {},
*generatorMethod() {},
un: undefined,
nu: null,
arrowFunc: () => {},
@ -66,7 +68,7 @@ test(function consoleTestStringifyCircular() {
nestedObj.o = circularObj;
const nestedObjExpected = `{ num: 1, bool: true, str: "a", method: [Function: method], un: undefined, nu: null, arrowFunc: [Function: arrowFunc], extendedClass: Extended { a: 1, b: 2 }, nFunc: [Function], extendedCstr: [Function: Extended], o: { num: 2, bool: false, str: "b", method: [Function: method], un: undefined, nu: null, nested: [Circular], emptyObj: {}, arr: [ 1, "s", false, null, [Circular] ], baseClass: Base { a: 1 } } }`;
const nestedObjExpected = `{ num: 1, bool: true, str: "a", method: [Function: method], asyncMethod: [AsyncFunction: asyncMethod], generatorMethod: [GeneratorFunction: generatorMethod], un: undefined, nu: null, arrowFunc: [Function: arrowFunc], extendedClass: Extended { a: 1, b: 2 }, nFunc: [Function], extendedCstr: [Function: Extended], o: { num: 2, bool: false, str: "b", method: [Function: method], un: undefined, nu: null, nested: [Circular], emptyObj: {}, arr: [ 1, "s", false, null, [Circular] ], baseClass: Base { a: 1 } } }`;
assertEqual(stringify(1), "1");
assertEqual(stringify("s"), "s");
@ -76,6 +78,12 @@ test(function consoleTestStringifyCircular() {
assertEqual(stringify(undefined), "undefined");
assertEqual(stringify(new Extended()), "Extended { a: 1, b: 2 }");
assertEqual(stringify(function f() {}), "[Function: f]");
assertEqual(stringify(async function af() {}), "[AsyncFunction: af]");
assertEqual(stringify(function* gf() {}), "[GeneratorFunction: gf]");
assertEqual(
stringify(async function* agf() {}),
"[AsyncGeneratorFunction: agf]"
);
assertEqual(stringify(nestedObj), nestedObjExpected);
assertEqual(stringify(JSON), "{}");
assertEqual(