Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cleanup format #2959

Merged
merged 7 commits into from
Mar 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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', '[email protected]')

const expectedOutput = 'FormData:\nusername: john_doe\nemail: [email protected]\n'
const expectedOutput = "FormData {\n username: 'john_doe',\n email: '[email protected]'\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)
})
})
Loading