Skip to content

Commit

Permalink
Code optimization
Browse files Browse the repository at this point in the history
  • Loading branch information
cedx committed Aug 5, 2024
1 parent 7e4d330 commit faa17ad
Show file tree
Hide file tree
Showing 4 changed files with 6 additions and 6 deletions.
2 changes: 1 addition & 1 deletion src/author.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ export class Author {
this.ipAddress = options.ipAddress ?? "";
this.name = options.name ?? "";
this.role = options.role ?? "";
this.url = options.url ? new URL(options.url) : null;
this.url = options.url && URL.canParse(options.url) ? new URL(options.url) : null;
this.userAgent = options.userAgent ?? "";
}

Expand Down
2 changes: 1 addition & 1 deletion src/blog.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export class Blog {
constructor(options: Partial<BlogOptions> = {}) {
this.charset = options.charset ?? "";
this.languages = options.languages ?? [];
this.url = options.url ? new URL(options.url) : null;
this.url = options.url && URL.canParse(options.url) ? new URL(options.url) : null;
}

/**
Expand Down
4 changes: 2 additions & 2 deletions src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,11 @@ export class Client {
* @param options An object providing values to initialize this instance.
*/
constructor(apiKey: string, blog: Blog, options: Partial<ClientOptions> = {}) {
const {baseUrl = "https://rest.akismet.com"} = options;
const {baseUrl = ""} = options;
const url = baseUrl instanceof URL ? baseUrl.href : baseUrl;

this.apiKey = apiKey;
this.baseUrl = new URL(url.endsWith("/") ? url : `${url}/`);
this.baseUrl = new URL(URL.canParse(url) ? (url.endsWith("/") ? url : `${url}/`) : "https://rest.akismet.com/");
this.blog = blog;
this.isTest = options.isTest ?? false;
this.userAgent = options.userAgent ?? `${navigator.userAgent} | Akismet/${Client.#version}`;
Expand Down
4 changes: 2 additions & 2 deletions src/comment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,10 @@ export class Comment {
this.content = options.content ?? "";
this.context = options.context ?? [];
this.date = options.date ?? null;
this.permalink = options.permalink ? new URL(options.permalink) : null;
this.permalink = options.permalink && URL.canParse(options.permalink) ? new URL(options.permalink) : null;
this.postModified = options.postModified ?? null;
this.recheckReason = options.recheckReason ?? "";
this.referrer = options.referrer ? new URL(options.referrer) : null;
this.referrer = options.referrer && URL.canParse(options.referrer) ? new URL(options.referrer) : null;
this.type = options.type ?? "";
}

Expand Down

0 comments on commit faa17ad

Please sign in to comment.