bitburner-src/utils/helpers/arrayToString.ts

22 lines
585 B
TypeScript
Raw Normal View History

2018-07-08 07:11:34 +02:00
/**
* Returns the input array as a comma separated string.
2019-02-09 03:46:30 +01:00
*
* Does several things that Array.toString() doesn't do
* - Adds brackets around the array
* - Adds quotation marks around strings
2018-07-08 07:11:34 +02:00
*/
2021-05-01 09:17:31 +02:00
export function arrayToString<T>(a: T[]): string {
2019-02-09 03:46:30 +01:00
const vals: any[] = [];
for (let i = 0; i < a.length; ++i) {
let elem: any = a[i];
if (Array.isArray(elem)) {
elem = arrayToString(elem);
} else if (typeof elem === "string") {
2019-02-09 03:46:30 +01:00
elem = `"${elem}"`;
}
vals.push(elem);
}
2019-02-09 03:46:30 +01:00
return `[${vals.join(", ")}]`;
}