mirror of
https://github.com/denoland/deno.git
synced 2025-01-22 15:10:44 -05:00
Fixed printing strings in arrays & objects without quotes
This commit is contained in:
parent
224cfc8c74
commit
0f1db89aa6
2 changed files with 23 additions and 3 deletions
|
@ -13,7 +13,7 @@ function getClassInstanceName(instance: any): string {
|
||||||
}
|
}
|
||||||
|
|
||||||
// tslint:disable-next-line:no-any
|
// tslint:disable-next-line:no-any
|
||||||
function stringify(ctx: ConsoleContext, value: any): string {
|
export function stringify(ctx: ConsoleContext, value: any): string {
|
||||||
switch (typeof value) {
|
switch (typeof value) {
|
||||||
case "string":
|
case "string":
|
||||||
return value;
|
return value;
|
||||||
|
@ -42,7 +42,7 @@ function stringify(ctx: ConsoleContext, value: any): string {
|
||||||
|
|
||||||
if (Array.isArray(value)) {
|
if (Array.isArray(value)) {
|
||||||
for (const el of value) {
|
for (const el of value) {
|
||||||
entries.push(stringify(ctx, el));
|
entries.push(stringifyWithQuotes(ctx, el));
|
||||||
}
|
}
|
||||||
|
|
||||||
ctx.delete(value);
|
ctx.delete(value);
|
||||||
|
@ -61,7 +61,7 @@ function stringify(ctx: ConsoleContext, value: any): string {
|
||||||
}
|
}
|
||||||
|
|
||||||
for (const key of Object.keys(value)) {
|
for (const key of Object.keys(value)) {
|
||||||
entries.push(`${key}: ${stringify(ctx, value[key])}`);
|
entries.push(`${key}: ${stringifyWithQuotes(ctx, value[key])}`);
|
||||||
}
|
}
|
||||||
|
|
||||||
ctx.delete(value);
|
ctx.delete(value);
|
||||||
|
@ -83,6 +83,17 @@ function stringify(ctx: ConsoleContext, value: any): string {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Print strings when they are inside of arrays or objects with quotes
|
||||||
|
// tslint:disable-next-line:no-any
|
||||||
|
function stringifyWithQuotes(ctx: ConsoleContext, value: any): string {
|
||||||
|
switch (typeof value) {
|
||||||
|
case "string":
|
||||||
|
return `"${value}"`;
|
||||||
|
default:
|
||||||
|
return stringify(ctx, value);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// tslint:disable-next-line:no-any
|
// tslint:disable-next-line:no-any
|
||||||
function stringifyArgs(args: any[]): string {
|
function stringifyArgs(args: any[]): string {
|
||||||
const out: string[] = [];
|
const out: string[] = [];
|
||||||
|
|
|
@ -5,6 +5,7 @@
|
||||||
|
|
||||||
import { test, assert, assertEqual } from "./testing/testing.ts";
|
import { test, assert, assertEqual } from "./testing/testing.ts";
|
||||||
import { readFileSync } from "deno";
|
import { readFileSync } from "deno";
|
||||||
|
import { stringify } from "./console.ts";
|
||||||
import * as deno from "deno";
|
import * as deno from "deno";
|
||||||
|
|
||||||
import "./compiler_test.ts";
|
import "./compiler_test.ts";
|
||||||
|
@ -25,6 +26,14 @@ test(function tests_console_assert() {
|
||||||
assertEqual(hasThrown, true);
|
assertEqual(hasThrown, true);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
test(function tests_console_stringify_complex_objects() {
|
||||||
|
// tslint:disable:no-any
|
||||||
|
assertEqual("foo", stringify(new Set<any>(), "foo"));
|
||||||
|
assertEqual(`[ "foo", "bar" ]`, stringify(new Set<any>(), ["foo", "bar"]));
|
||||||
|
assertEqual(`{ foo: "bar" }`, stringify(new Set<any>(), { foo: "bar" }));
|
||||||
|
// tslint:enable:no-any
|
||||||
|
});
|
||||||
|
|
||||||
test(function tests_console_stringify_circular() {
|
test(function tests_console_stringify_circular() {
|
||||||
class Base {
|
class Base {
|
||||||
a = 1;
|
a = 1;
|
||||||
|
|
Loading…
Add table
Reference in a new issue