mirror of
https://github.com/bitburner-official/bitburner-src.git
synced 2024-11-26 01:23:49 +01:00
API: Improve built-in print APIs when printing objects containing Map or Set (#1723)
This commit is contained in:
parent
d0022aac2b
commit
9a0400d3df
@ -229,6 +229,19 @@ function spawnOptions(ctx: NetscriptContext, threadOrOption: unknown): CompleteS
|
|||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function mapToString(map: Map<unknown, unknown>): string {
|
||||||
|
const formattedMap = [...map]
|
||||||
|
.map((m) => {
|
||||||
|
return `${m[0]} => ${m[1]}`;
|
||||||
|
})
|
||||||
|
.join("; ");
|
||||||
|
return `< Map: ${formattedMap} >`;
|
||||||
|
}
|
||||||
|
|
||||||
|
function setToString(set: Set<unknown>): string {
|
||||||
|
return `< Set: ${[...set].join("; ")} >`;
|
||||||
|
}
|
||||||
|
|
||||||
/** Convert multiple arguments for tprint or print into a single string. */
|
/** Convert multiple arguments for tprint or print into a single string. */
|
||||||
function argsToString(args: unknown[]): string {
|
function argsToString(args: unknown[]): string {
|
||||||
// Reduce array of args into a single output 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
|
// Handle Map formatting, since it does not JSON stringify or toString in a helpful way
|
||||||
// output is "< Map: key1 => value1; key2 => value2 >"
|
// output is "< Map: key1 => value1; key2 => value2 >"
|
||||||
if (nativeArg instanceof Map && [...nativeArg].length) {
|
if (nativeArg instanceof Map) {
|
||||||
const formattedMap = [...nativeArg]
|
return (out += mapToString(nativeArg));
|
||||||
.map((m) => {
|
|
||||||
return `${m[0]} => ${m[1]}`;
|
|
||||||
})
|
|
||||||
.join("; ");
|
|
||||||
return (out += `< Map: ${formattedMap} >`);
|
|
||||||
}
|
}
|
||||||
// Handle Set formatting, since it does not JSON stringify or toString in a helpful way
|
// Handle Set formatting, since it does not JSON stringify or toString in a helpful way
|
||||||
if (nativeArg instanceof Set) {
|
if (nativeArg instanceof Set) {
|
||||||
return (out += `< Set: ${[...nativeArg].join("; ")} >`);
|
return (out += setToString(nativeArg));
|
||||||
}
|
}
|
||||||
if (typeof nativeArg === "object") {
|
if (typeof nativeArg === "object") {
|
||||||
return (out += JSON.stringify(nativeArg, (_, value) => {
|
return (out += JSON.stringify(nativeArg, (_, value) => {
|
||||||
@ -264,6 +272,12 @@ function argsToString(args: unknown[]): string {
|
|||||||
if (value instanceof Promise) {
|
if (value instanceof Promise) {
|
||||||
return value.toString();
|
return value.toString();
|
||||||
}
|
}
|
||||||
|
if (value instanceof Map) {
|
||||||
|
return mapToString(value);
|
||||||
|
}
|
||||||
|
if (value instanceof Set) {
|
||||||
|
return setToString(value);
|
||||||
|
}
|
||||||
return value;
|
return value;
|
||||||
}));
|
}));
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user