Files
freeCodeCamp/tools/client-plugins/browser-scripts/utils/format.js
Oliver Eyton-Williams 69d6ee32bf feat: python in the browser (#50913)
Co-authored-by: Beau Carnes <1513130+beaucarnes@users.noreply.github.com>
2023-07-28 07:36:25 +02:00

23 lines
693 B
JavaScript

// TODO: this is a straight up copy of the format function from the client.
// Figure out a way to share it.
import { inspect } from 'util/util';
export function format(x) {
// we're trying to mimic console.log, so we avoid wrapping strings in quotes:
if (typeof x === 'string') return x;
else if (x instanceof Set) {
return `Set(${x.size}) {${Array.from(x).join(', ')}}`;
} else if (x instanceof Map) {
return `Map(${x.size}) {${Array.from(
x.entries(),
([k, v]) => `${k} => ${v}`
).join(', ')}})`;
} else if (typeof x === 'bigint') {
return x.toString() + 'n';
} else if (typeof x === 'symbol') {
return x.toString();
}
return inspect(x);
}