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

refactor: use !!v insetead of Boolean(v) to reduce bundle size #3708

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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
2 changes: 1 addition & 1 deletion src/helper/streaming/sse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export class SSEStreamingApi extends StreamingApi {
message.id && `id: ${message.id}`,
message.retry && `retry: ${message.retry}`,
]
.filter(Boolean)
.filter((data) => !!data)
.join('\n') + '\n\n'

await this.write(sseData)
Expand Down
22 changes: 11 additions & 11 deletions src/jsx/streaming.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@
async start(controller) {
try {
if (content instanceof JSXNode) {
// aJSXNode.toString() returns a string or Promise<string> and string is already escaped
// JSXNode.toString() returns a string or Promise<string> and string is already escaped
content = content.toString() as HtmlEscapedString | Promise<HtmlEscapedString>
}
const context = typeof content === 'object' ? content : {}
Expand Down Expand Up @@ -158,21 +158,21 @@
true,
context
)
;(res as HtmlEscapedString).callbacks
?.map((c) => c({ phase: HtmlEscapedCallbackPhase.Stream, context }))
// eslint-disable-next-line @typescript-eslint/no-explicit-any
.filter<Promise<string>>(Boolean as any)
.forEach(then)
;(
(res as HtmlEscapedString).callbacks
?.map((c) => c({ phase: HtmlEscapedCallbackPhase.Stream, context }))
.filter((result) => !!result) as Promise<string>[]
).forEach(then)

Check failure on line 165 in src/jsx/streaming.ts

View workflow job for this annotation

GitHub Actions / Main

Unhandled error

TypeError: Cannot read properties of undefined (reading 'forEach') ❯ src/jsx/streaming.ts:165:19 This error originated in "src/jsx/components.test.tsx" test file. It doesn't mean the error was thrown inside the file itself, but while it was running. The latest test that might've caused the error is "src/jsx/components.test.tsx". It might mean one of the following: - The error was thrown, while Vitest was running this test. - If the error occurred after the test had been completed, this was the last documented test before it was thrown.

Check failure on line 165 in src/jsx/streaming.ts

View workflow job for this annotation

GitHub Actions / Main

Unhandled error

TypeError: Cannot read properties of undefined (reading 'forEach') ❯ src/jsx/streaming.ts:165:19 This error originated in "src/jsx/components.test.tsx" test file. It doesn't mean the error was thrown inside the file itself, but while it was running. The latest test that might've caused the error is "src/jsx/components.test.tsx". It might mean one of the following: - The error was thrown, while Vitest was running this test. - If the error occurred after the test had been completed, this was the last documented test before it was thrown.

Check failure on line 165 in src/jsx/streaming.ts

View workflow job for this annotation

GitHub Actions / Main

Unhandled error

TypeError: Cannot read properties of undefined (reading 'forEach') ❯ src/jsx/streaming.ts:165:19 This error originated in "src/jsx/components.test.tsx" test file. It doesn't mean the error was thrown inside the file itself, but while it was running. The latest test that might've caused the error is "src/jsx/components.test.tsx". It might mean one of the following: - The error was thrown, while Vitest was running this test. - If the error occurred after the test had been completed, this was the last documented test before it was thrown.
resolvedCount++
controller.enqueue(textEncoder.encode(res))
})
)
}
;(resolved as HtmlEscapedString).callbacks
?.map((c) => c({ phase: HtmlEscapedCallbackPhase.Stream, context }))
// eslint-disable-next-line @typescript-eslint/no-explicit-any
.filter<Promise<string>>(Boolean as any)
.forEach(then)
;(
(resolved as HtmlEscapedString).callbacks
?.map((c) => c({ phase: HtmlEscapedCallbackPhase.Stream, context }))
.filter((result) => !!result) as Promise<string>[]
).forEach(then)
while (resolvedCount !== callbacks.length) {
await Promise.all(callbacks)
}
Expand Down
2 changes: 1 addition & 1 deletion src/middleware/combine/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ export const except = (
return condition
}
})
.filter(Boolean) as Condition[]
.filter((condition) => !!condition) as Condition[]

if (router) {
conditions.unshift((c: Context) => !!router?.match(METHOD_NAME_ALL, c.req.path)?.[0]?.[0]?.[0])
Expand Down
2 changes: 1 addition & 1 deletion src/middleware/secure-headers/secure-headers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -306,7 +306,7 @@ function getPermissionsPolicyDirectives(policy: PermissionsPolicyOptions): strin

return ''
})
.filter(Boolean)
.filter((directive) => !!directive)
.join(', ')
}

Expand Down
7 changes: 3 additions & 4 deletions src/utils/html.ts
Original file line number Diff line number Diff line change
Expand Up @@ -167,10 +167,9 @@ export const resolveCallback = async (

const resStr = Promise.all(callbacks.map((c) => c({ phase, buffer, context }))).then((res) =>
Promise.all(
res
// eslint-disable-next-line @typescript-eslint/no-explicit-any
.filter<string>(Boolean as any)
.map((str) => resolveCallback(str, phase, false, context, buffer))
(res.filter((res) => !!res) as string[]).map((str) =>
resolveCallback(str, phase, false, context, buffer)
)
).then(() => (buffer as [string])[0])
)

Expand Down
Loading