Skip to content

Commit

Permalink
Merge pull request #1226 from neet/simplify-negate
Browse files Browse the repository at this point in the history
chore: Simplify nullish check operator
  • Loading branch information
neet authored Oct 20, 2024
2 parents 6bb91af + 1e720c7 commit 7110ad0
Show file tree
Hide file tree
Showing 11 changed files with 30 additions and 20 deletions.
4 changes: 2 additions & 2 deletions src/adapters/action/dispatcher-http-hook-mastodon.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ async function waitForMediaAttachment(
let media: mastodon.v1.MediaAttachment | undefined;
const signal = AbortSignal.timeout(timeout);

while (media == undefined) {
while (!media) {
if (signal.aborted) {
throw new MastoTimeoutError(`Media processing timed out of ${timeout}ms`);
}
Expand All @@ -70,7 +70,7 @@ async function waitForMediaAttachment(
`/api/v1/media/${id}`,
);

if (processing.url != undefined) {
if (processing.url) {
media = processing;
}
} catch (error) {
Expand Down
2 changes: 1 addition & 1 deletion src/adapters/action/dispatcher-http.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export class HttpActionDispatcher implements ActionDispatcher<HttpAction> {
) {}

dispatch<T>(action: HttpAction): T | Promise<T> {
if (this.hook != undefined) {
if (this.hook) {
action = this.hook.beforeDispatch(action);
}

Expand Down
6 changes: 3 additions & 3 deletions src/adapters/action/paginator-http.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export class PaginatorHttp<Entity, Params = undefined>
) {}

async next(): Promise<IteratorResult<Entity, undefined>> {
if (this.nextPath == undefined) {
if (!this.nextPath) {
return { done: true, value: undefined };
}

Expand Down Expand Up @@ -98,12 +98,12 @@ export class PaginatorHttp<Entity, Params = undefined>
}

private getLink(value?: string | null): URL | undefined {
if (value == undefined) {
if (!value) {
return;
}

const parsed = parseLinkHeader(value).get(this.direction);
if (parsed == undefined) {
if (!parsed) {
return;
}

Expand Down
2 changes: 1 addition & 1 deletion src/adapters/action/proxy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ const apply =
const action = context.pop();

/* istanbul ignore next */
if (action == undefined) {
if (!action) {
throw new Error("No action specified");
}

Expand Down
6 changes: 3 additions & 3 deletions src/adapters/config/http-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ export class HttpConfigImpl implements HttpConfig {

if (typeof params === "string") {
url.search = params;
} else if (params != undefined) {
} else if (params) {
url.search = this.serializer.serialize("querystring", params);
}

Expand All @@ -81,15 +81,15 @@ export class HttpConfigImpl implements HttpConfig {
): AbortSignal {
const signals: AbortSignal[] = [];

if (this.props.timeout != undefined) {
if (this.props.timeout) {
signals.push(AbortSignal.timeout(this.props.timeout));
}

if (this.props.requestInit?.signal) {
signals.push(this.props.requestInit.signal);
}

if (signal != undefined) {
if (signal) {
signals.push(signal);
}

Expand Down
16 changes: 13 additions & 3 deletions src/adapters/http/http-native-impl.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,29 @@ import { HttpNativeImpl } from "./http-native-impl";

describe("HttpNativeImpl", () => {
it("timeouts", async () => {
const server = node_http.createServer((_, res) => {
res.writeHead(200, { "Content-Type": "application/json" });
res.end("{}");
});

const port = await getPort();
server.listen(port);

const serializer = new SerializerNativeImpl();
const http = new HttpNativeImpl(
serializer,
new HttpConfigImpl(
{
url: "https://example.com",
timeout: 0,
url: `http://localhost:${port}`,
timeout: 1,
},
serializer,
),
);

await expect(() => http.get("/")).rejects.toThrowError(MastoTimeoutError);
await expect(() => http.get("/")).rejects.toThrow(MastoTimeoutError);

server.close();
});

it("throws an error if server returned non-JSON", async () => {
Expand Down
4 changes: 2 additions & 2 deletions src/adapters/http/http-native-impl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ export class HttpNativeImpl extends BaseHttp implements Http {

const text = await response.text();
const encoding = getEncoding(response.headers);
if (encoding == undefined) {
if (!encoding) {
throw new MastoUnexpectedError(
"The server returned data with an unknown encoding.",
);
Expand Down Expand Up @@ -89,7 +89,7 @@ export class HttpNativeImpl extends BaseHttp implements Http {
private async createError(error: unknown): Promise<unknown> {
if (error instanceof Response) {
const encoding = getEncoding(error.headers);
if (encoding == undefined) {
if (!encoding) {
throw new MastoUnexpectedError(
"The server returned data with an unknown encoding. The server may be down.",
);
Expand Down
2 changes: 1 addition & 1 deletion src/adapters/serializers/flatten.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ export const flattenForRailsQueryString = (object: unknown): string => {
});

return flatten(object)
.filter(([, v]) => v != undefined)
.filter(([, v]) => !!v)
.map(([k, v]) => `${k}=${encodeURIComponent(v as string)}`)
.join("&");
};
4 changes: 2 additions & 2 deletions src/adapters/ws/async-iterable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,15 @@ export async function* toAsyncIterable(
): AsyncIterableIterator<WebSocket.MessageEvent> {
const handleClose = async (e: WebSocket.CloseEvent) => {
/* istanbul ignore next */
if (events.return == undefined) {
if (!events.return) {
throw new MastoUnexpectedError("events.return is undefined");
}
await events.return(e);
};

const handleError = async (e: WebSocket.ErrorEvent) => {
/* istanbul ignore next */
if (events.return == undefined) {
if (!events.return) {
throw new MastoUnexpectedError("events.return is undefined");
}
await events.return(e);
Expand Down
2 changes: 1 addition & 1 deletion src/adapters/ws/web-socket-connector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ export class WebSocketConnectorImpl implements WebSocketConnector {
throw new MastoWebSocketError("WebSocket closed");
}

if (this.ws != undefined) {
if (this.ws) {
return Promise.resolve(this.ws);
}

Expand Down
2 changes: 1 addition & 1 deletion src/adapters/ws/web-socket-subscription.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ export class WebSocketSubscription implements mastodon.streaming.Subscription {
}

unsubscribe(): void {
if (this.connection == undefined) {
if (!this.connection) {
return;
}

Expand Down

0 comments on commit 7110ad0

Please sign in to comment.