Package: effect
Module: Formatter
Converts any JavaScript value into a human-readable string.
When to use
Use when you need to format arbitrary JavaScript values for debugging, logging, or error messages.
Details
formatJson when you need
parseable JSON.BigInt, Symbol, Set, Map, Date, RegExp, and class
instances that JSON.stringify cannot represent."[Circular]" instead of throwing.null, undefined, 123, true).
Strings are JSON-quoted.toString (not Object.prototype.toString):
toString() is called unless ignoreToString is true.cause: formatted as "<message> (cause: <cause>)".Set, Map, etc.): formatted as
ClassName([...elements]).ClassName({...}).Redactable values are automatically redacted.space is set.space — indentation unit (number of spaces, or a string like
"\t"). Defaults to 0 (compact).ignoreToString — skip calling toString(). Defaults to false.Example (Formatting compact output)
import { Formatter } from "effect"
console.log(Formatter.format({ a: 1, b: [2, 3] }))
// {"a":1,"b":[2,3]}
Example (Pretty-printed output)
import { Formatter } from "effect"
console.log(Formatter.format({ a: 1, b: [2, 3] }, { space: 2 }))
// {
// "a": 1,
// "b": [
// 2,
// 3
// ]
// }
Example (Handling circular references)
import { Formatter } from "effect"
const obj: any = { name: "loop" }
obj.self = obj
console.log(Formatter.format(obj))
// {"name":"loop","self":[Circular]}
See
formatJsonFormatterSignature
declare const format: (input: unknown, options?: { readonly space?: number | string | undefined; readonly ignoreToString?: boolean | undefined; }) => string
Since v2.0.0