Skip to content

Commit dcab693

Browse files
authored
Cleanup format (#2959)
* for some reason this fixes the test * cleanup custom format * fixup
1 parent 07019d0 commit dcab693

File tree

6 files changed

+30
-17
lines changed

6 files changed

+30
-17
lines changed

lib/web/fetch/formdata.js

+20-5
Original file line numberDiff line numberDiff line change
@@ -157,12 +157,27 @@ class FormData {
157157
}
158158

159159
[nodeUtil.inspect.custom] (depth, options) {
160-
let output = 'FormData:\n'
161-
this[kState].forEach(entry => {
162-
output += `${entry.name}: ${entry.value}\n`
163-
})
160+
const state = this[kState].reduce((a, b) => {
161+
if (a[b.name]) {
162+
if (Array.isArray(a[b.name])) {
163+
a[b.name].push(b.value)
164+
} else {
165+
a[b.name] = [a[b.name], b.value]
166+
}
167+
} else {
168+
a[b.name] = b.value
169+
}
170+
171+
return a
172+
}, { __proto__: null })
173+
174+
options.depth ??= depth
175+
options.colors ??= true
176+
177+
const output = nodeUtil.formatWithOptions(options, state)
164178

165-
return output
179+
// remove [Object null prototype]
180+
return `FormData ${output.slice(output.indexOf(']') + 2)}`
166181
}
167182
}
168183

lib/web/fetch/headers.js

+2-8
Original file line numberDiff line numberDiff line change
@@ -572,16 +572,10 @@ class Headers {
572572
return (this[kHeadersList][kHeadersSortedMap] = headers)
573573
}
574574

575-
[Symbol.for('nodejs.util.inspect.custom')] () {
576-
webidl.brandCheck(this, Headers)
577-
578-
return this[kHeadersList]
579-
}
580-
581575
[util.inspect.custom] (depth, options) {
582-
const inspected = util.inspect(this[kHeadersList].entries)
576+
options.depth ??= depth
583577

584-
return `Headers ${inspected}`
578+
return `Headers ${util.formatWithOptions(options, this[kHeadersList].entries)}`
585579
}
586580
}
587581

lib/web/fetch/request.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -778,6 +778,8 @@ class Request {
778778
options.depth = 2
779779
}
780780

781+
options.colors ??= true
782+
781783
const properties = {
782784
method: this.method,
783785
url: this.url,
@@ -796,7 +798,7 @@ class Request {
796798
signal: this.signal
797799
}
798800

799-
return nodeUtil.formatWithOptions(options, { ...properties })
801+
return `Request ${nodeUtil.formatWithOptions(options, properties)}`
800802
}
801803
}
802804

lib/web/fetch/response.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,8 @@ class Response {
259259
options.depth = 2
260260
}
261261

262+
options.colors ??= true
263+
262264
const properties = {
263265
status: this.status,
264266
statusText: this.statusText,
@@ -271,7 +273,7 @@ class Response {
271273
url: this.url
272274
}
273275

274-
return nodeUtil.formatWithOptions(options, `Response ${nodeUtil.inspect(properties)}`)
276+
return `Response ${nodeUtil.formatWithOptions(options, properties)}`
275277
}
276278
}
277279

test/fetch/formdata-inspect-custom.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ test('FormData class custom inspection', () => {
1010
formData.append('username', 'john_doe')
1111
formData.append('email', 'john@example.com')
1212

13-
const expectedOutput = 'FormData:\nusername: john_doe\nemail: john@example.com\n'
13+
const expectedOutput = "FormData {\n username: 'john_doe',\n email: 'john@example.com'\n}"
1414

1515
assert.deepStrictEqual(inspect(formData), expectedOutput)
1616
})

test/fetch/request-inspect-custom.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ describe('Request custom inspection', () => {
1616

1717
const inspectedOutput = util.inspect(request)
1818

19-
const expectedOutput = '{\n method: \'POST\',\n url: \'https://example.com/api\',\n headers: Headers { \'Content-Type\': \'application/json\' },\n destination: \'\',\n referrer: \'about:client\',\n referrerPolicy: \'\',\n mode: \'cors\',\n credentials: \'same-origin\',\n cache: \'default\',\n redirect: \'follow\',\n integrity: \'\',\n keepalive: false,\n isReloadNavigation: false,\n isHistoryNavigation: false,\n signal: AbortSignal { aborted: false }\n}'
19+
const expectedOutput = "Request {\n method: 'POST',\n url: 'https://example.com/api',\n headers: Headers { 'Content-Type': 'application/json' },\n destination: '',\n referrer: 'about:client',\n referrerPolicy: '',\n mode: 'cors',\n credentials: 'same-origin',\n cache: 'default',\n redirect: 'follow',\n integrity: '',\n keepalive: false,\n isReloadNavigation: false,\n isHistoryNavigation: false,\n signal: AbortSignal { aborted: false }\n}"
2020
assert.strictEqual(inspectedOutput, expectedOutput)
2121
})
2222
})

0 commit comments

Comments
 (0)