Skip to content

Commit

Permalink
Cleanup format (#2959)
Browse files Browse the repository at this point in the history
* for some reason this fixes the test

* cleanup custom format

* fixup
  • Loading branch information
KhafraDev committed Mar 15, 2024
1 parent 07019d0 commit dcab693
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 17 deletions.
25 changes: 20 additions & 5 deletions lib/web/fetch/formdata.js
Original file line number Diff line number Diff line change
Expand Up @@ -157,12 +157,27 @@ class FormData {
}

[nodeUtil.inspect.custom] (depth, options) {
let output = 'FormData:\n'
this[kState].forEach(entry => {
output += `${entry.name}: ${entry.value}\n`
})
const state = this[kState].reduce((a, b) => {
if (a[b.name]) {
if (Array.isArray(a[b.name])) {
a[b.name].push(b.value)
} else {
a[b.name] = [a[b.name], b.value]
}
} else {
a[b.name] = b.value
}

return a
}, { __proto__: null })

options.depth ??= depth
options.colors ??= true

const output = nodeUtil.formatWithOptions(options, state)

return output
// remove [Object null prototype]
return `FormData ${output.slice(output.indexOf(']') + 2)}`
}
}

Expand Down
10 changes: 2 additions & 8 deletions lib/web/fetch/headers.js
Original file line number Diff line number Diff line change
Expand Up @@ -572,16 +572,10 @@ class Headers {
return (this[kHeadersList][kHeadersSortedMap] = headers)
}

[Symbol.for('nodejs.util.inspect.custom')] () {
webidl.brandCheck(this, Headers)

return this[kHeadersList]
}

[util.inspect.custom] (depth, options) {
const inspected = util.inspect(this[kHeadersList].entries)
options.depth ??= depth

return `Headers ${inspected}`
return `Headers ${util.formatWithOptions(options, this[kHeadersList].entries)}`
}
}

Expand Down
4 changes: 3 additions & 1 deletion lib/web/fetch/request.js
Original file line number Diff line number Diff line change
Expand Up @@ -778,6 +778,8 @@ class Request {
options.depth = 2
}

options.colors ??= true

const properties = {
method: this.method,
url: this.url,
Expand All @@ -796,7 +798,7 @@ class Request {
signal: this.signal
}

return nodeUtil.formatWithOptions(options, { ...properties })
return `Request ${nodeUtil.formatWithOptions(options, properties)}`
}
}

Expand Down
4 changes: 3 additions & 1 deletion lib/web/fetch/response.js
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,8 @@ class Response {
options.depth = 2
}

options.colors ??= true

const properties = {
status: this.status,
statusText: this.statusText,
Expand All @@ -271,7 +273,7 @@ class Response {
url: this.url
}

return nodeUtil.formatWithOptions(options, `Response ${nodeUtil.inspect(properties)}`)
return `Response ${nodeUtil.formatWithOptions(options, properties)}`
}
}

Expand Down
2 changes: 1 addition & 1 deletion test/fetch/formdata-inspect-custom.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ test('FormData class custom inspection', () => {
formData.append('username', 'john_doe')
formData.append('email', 'john@example.com')

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

assert.deepStrictEqual(inspect(formData), expectedOutput)
})
2 changes: 1 addition & 1 deletion test/fetch/request-inspect-custom.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ describe('Request custom inspection', () => {

const inspectedOutput = util.inspect(request)

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}'
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}"
assert.strictEqual(inspectedOutput, expectedOutput)
})
})

0 comments on commit dcab693

Please sign in to comment.