mirror of
https://github.com/vercel-labs/scriptc.git
synced 2026-10-28 06:53:42 +08:00
- util.format %d and %i format bigint, symbol, null and undefined arguments - util.format %j prints [Circular] for circular arguments - util.types answers the remaining brand checks - util.inspect prints WeakMap, WeakSet, Date and numeric typed array values as Node does - util.inspect lowers the breakLength, compact and sorted options
30 lines
998 B
TypeScript
30 lines
998 B
TypeScript
// util.format and console.log %d / %i over arguments Node formats without a
|
|
// number conversion: a bigint prints with its n suffix, a symbol as NaN,
|
|
// null as 0 under %d and NaN under %i, and undefined as NaN. Arguments still
|
|
// evaluate in order.
|
|
import { format } from "node:util";
|
|
|
|
const calls: string[] = [];
|
|
function big(n: bigint): bigint {
|
|
calls.push(`big ${n}`);
|
|
return n;
|
|
}
|
|
function sym(): symbol {
|
|
calls.push("sym");
|
|
return Symbol("s");
|
|
}
|
|
function nothing(): undefined {
|
|
calls.push("nothing");
|
|
return undefined;
|
|
}
|
|
|
|
console.log(format("%d %i", 10n, -5n));
|
|
console.log(format("%d", 2n ** 70n), format("%i", -(2n ** 65n)));
|
|
console.log(format("%d|%i", 0n, 0n));
|
|
console.log(format("%d %i", Symbol("x"), Symbol.iterator));
|
|
console.log(format("%d %i", null, null));
|
|
console.log(format("%d %i", undefined, undefined));
|
|
console.log(format("%d-%i-%d", big(3n), sym(), nothing()), calls.join(", "));
|
|
console.log("count %d of %i", 42n, 7n);
|
|
console.log("missing %d", null, "extra");
|