Files
freeCodeCamp/client/src/utils/format.js
Ismail Tlemcani 2846df2b66 fix: fix util import statement (#45104)
Co-authored-by: IsmailTlemcani <ismail.tlemcani@gmail.com>
2022-02-24 08:16:47 -08:00

20 lines
583 B
JavaScript

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);
}