From 9a0400d3dffa778b874e972dd30fec0bd950a7c4 Mon Sep 17 00:00:00 2001 From: catloversg <152669316+catloversg@users.noreply.github.com> Date: Thu, 24 Oct 2024 06:01:58 +0700 Subject: [PATCH] API: Improve built-in print APIs when printing objects containing Map or Set (#1723) --- src/Netscript/NetscriptHelpers.tsx | 30 ++++++++++++++++++++++-------- 1 file changed, 22 insertions(+), 8 deletions(-) diff --git a/src/Netscript/NetscriptHelpers.tsx b/src/Netscript/NetscriptHelpers.tsx index 341319da5..5cfc925dc 100644 --- a/src/Netscript/NetscriptHelpers.tsx +++ b/src/Netscript/NetscriptHelpers.tsx @@ -229,6 +229,19 @@ function spawnOptions(ctx: NetscriptContext, threadOrOption: unknown): CompleteS return result; } +function mapToString(map: Map): string { + const formattedMap = [...map] + .map((m) => { + return `${m[0]} => ${m[1]}`; + }) + .join("; "); + return `< Map: ${formattedMap} >`; +} + +function setToString(set: Set): string { + return `< Set: ${[...set].join("; ")} >`; +} + /** Convert multiple arguments for tprint or print into a single string. */ function argsToString(args: unknown[]): string { // Reduce array of args into a single output string @@ -243,17 +256,12 @@ function argsToString(args: unknown[]): string { // Handle Map formatting, since it does not JSON stringify or toString in a helpful way // output is "< Map: key1 => value1; key2 => value2 >" - if (nativeArg instanceof Map && [...nativeArg].length) { - const formattedMap = [...nativeArg] - .map((m) => { - return `${m[0]} => ${m[1]}`; - }) - .join("; "); - return (out += `< Map: ${formattedMap} >`); + if (nativeArg instanceof Map) { + return (out += mapToString(nativeArg)); } // Handle Set formatting, since it does not JSON stringify or toString in a helpful way if (nativeArg instanceof Set) { - return (out += `< Set: ${[...nativeArg].join("; ")} >`); + return (out += setToString(nativeArg)); } if (typeof nativeArg === "object") { return (out += JSON.stringify(nativeArg, (_, value) => { @@ -264,6 +272,12 @@ function argsToString(args: unknown[]): string { if (value instanceof Promise) { return value.toString(); } + if (value instanceof Map) { + return mapToString(value); + } + if (value instanceof Set) { + return setToString(value); + } return value; })); }