MCPcopy Create free account
hub / github.com/effect-app/libs / format

Function format

repos/effect/packages/effect/src/Formatter.ts:116–199  ·  view source on GitHub ↗
(input: unknown, options?: {
  readonly space?: number | string | undefined
  readonly ignoreToString?: boolean | undefined
})

Source from the content-addressed store, hash-verified

114 const ind = (d: number) => gap.repeat(d)
115
116 const wrap = (v: unknown, body: string): string => {
117 const ctor = (v as any)?.constructor
118 return ctor && ctor !== Object.prototype.constructor && ctor.name ? `${ctor.name}(${body})` : body
119 }
120
121 const ownKeys = (o: object): Array<PropertyKey> => {
122 try {
123 return Reflect.ownKeys(o)
124 } catch {
125 return ["[ownKeys threw]"]
126 }
127 }
128
129 function recur(v: unknown, d = 0): string {
130 if (Array.isArray(v)) {
131 if (ancestors.has(v)) return CIRCULAR
132 ancestors.add(v)
133 const output = !gap || v.length <= 1
134 ? `[${v.map((x) => recur(x, d)).join(",")}]`
135 : `[\n${ind(d + 1)}${v.map((x) => recur(x, d + 1)).join(",\n" + ind(d + 1))}\n${ind(d)}]`
136 ancestors.delete(v)
137 return output
138 }
139
140 if (v instanceof Date) return formatDate(v)
141
142 if (
143 !options?.ignoreToString &&
144 Predicate.hasProperty(v, "toString") &&
145 typeof v["toString"] === "function" &&
146 v["toString"] !== Object.prototype.toString &&
147 v["toString"] !== Array.prototype.toString
148 ) {
149 const s = safeToString(v)
150 if (v instanceof Error && v.cause) {
151 return `${s} (cause: ${recur(v.cause, d)})`
152 }
153 return s
154 }
155
156 if (typeof v === "string") return JSON.stringify(v)
157
158 if (
159 typeof v === "number" ||
160 v == null ||
161 typeof v === "boolean" ||
162 typeof v === "symbol"
163 ) return String(v)
164
165 if (typeof v === "bigint") return String(v) + "n"
166
167 if (typeof v === "object" || typeof v === "function") {
168 if (ancestors.has(v)) return CIRCULAR
169 ancestors.add(v)
170
171 let output: string
172 if (symbolRedactable in v) {
173 output = format(getRedacted(v as any))

Callers 2

Formatter.test.tsFile · 0.90
recurFunction · 0.70

Calls 1

recurFunction · 0.70

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…