Skip to content

Commit

Permalink
Restore the TypeScript sources
Browse files Browse the repository at this point in the history
  • Loading branch information
cedx committed Jul 11, 2024
1 parent 6c51205 commit 908e09e
Show file tree
Hide file tree
Showing 8 changed files with 232 additions and 164 deletions.
80 changes: 49 additions & 31 deletions src/author.js → src/author.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,45 +5,39 @@ export class Author {

/**
* The author's mail address. If you set it to `"[email protected]"`, Akismet will always return `true`.
* @type {string}
*/
email;
email: string;

/**
* The author's IP address.
* @type {string}
*/
ipAddress;
ipAddress: string;

/**
* The author's name. If you set it to `"viagra-test-123"`, Akismet will always return `true`.
* @type {string}
*/
name;
name: string;

/**
* The author's role. If you set it to `"administrator"`, Akismet will always return `false`.
* @type {AuthorRole|string}
*/
role;
role: AuthorRole|string;

/**
* The URL of the author's website.
* @type {URL|null}
*/
url;
url: URL|null;

/**
* The author's user agent, that is the string identifying the Web browser used to submit comments.
* @type {string}
*/
userAgent;
userAgent: string;

/**
* Creates a new author.
* @param {Partial<AuthorOptions>} options An object providing values to initialize this instance.
* @param options An object providing values to initialize this instance.
*/
constructor(options = {}) {
constructor(options: Partial<AuthorOptions> = {}) {
this.email = options.email ?? "";
this.ipAddress = options.ipAddress ?? "";
this.name = options.name ?? "";
Expand All @@ -54,10 +48,10 @@ export class Author {

/**
* Creates a new author from the specified JSON object.
* @param {Record<string, any>} json A JSON object representing an author.
* @returns {Author} The instance corresponding to the specified JSON object.
* @param json A JSON object representing an author.
* @returns The instance corresponding to the specified JSON object.
*/
static fromJson(json) {
static fromJson(json: Record<string, any>): Author {
return new this({
email: typeof json.comment_author_email == "string" ? json.comment_author_email : "",
ipAddress: typeof json.user_ip == "string" ? json.user_ip : "",
Expand All @@ -70,10 +64,10 @@ export class Author {

/**
* Returns a JSON representation of this object.
* @returns {Record<string, any>} The JSON representation of this object.
* @returns The JSON representation of this object.
*/
toJSON() {
/** @type {Record<string, any>} */ const map = {user_ip: this.ipAddress};
toJSON(): Record<string, any> {
const map: Record<string, any> = {user_ip: this.ipAddress};
if (this.email) map.comment_author_email = this.email;
if (this.name) map.comment_author = this.name;
if (this.role) map.user_role = this.role;
Expand All @@ -85,23 +79,47 @@ export class Author {

/**
* Defines the options of an {@link Author} instance.
* @typedef {object} AuthorOptions
* @property {string} email The author's mail address. If you set it to `"[email protected]"`, Akismet will always return `true`.
* @property {string} ipAddress The author's IP address.
* @property {string} name The author's name.
* @property {AuthorRole|string} role The author's role.
* @property {URL|string} url The URL of the author's website.
* @property {string} userAgent The author's user agent, that is the string identifying the Web browser used to submit comments.
*/
export interface AuthorOptions {

/**
* The author's mail address. If you set it to `"[email protected]"`, Akismet will always return `true`.
*/
email: string;

/**
* The author's IP address.
*/
ipAddress: string;

/**
* The author's name. If you set it to `"viagra-test-123"`, Akismet will always return `true`.
*/
name: string;

/**
* The author's role. If you set it to `"administrator"`, Akismet will always return `false`.
*/
role: AuthorRole|string;

/**
* The URL of the author's website.
*/
url: URL|string;

/**
* The author's user agent, that is the string identifying the Web browser used to submit comments.
*/
userAgent: string;
}

/**
* Specifies the role of an author.
* @enum {string}
*/
export const AuthorRole = Object.freeze({
export enum AuthorRole {

/**
* The author is an administrator.
*/
administrator: "administrator"
});
administrator = "administrator"
}
46 changes: 28 additions & 18 deletions src/blog.js → src/blog.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,38 +5,35 @@ export class Blog {

/**
* The character encoding for the values included in comments.
* @type {string}
*/
charset;
charset: string;

/**
* The languages in use on the blog or site, in ISO 639-1 format.
* @type {string[]}
*/
languages;
languages: string[];

/**
* The blog or site URL.
* @type {URL|null}
*/
url;
url: URL|null;

/**
* Creates a new blog.
* @param {Partial<BlogOptions>} options An object providing values to initialize this instance.
* @param options An object providing values to initialize this instance.
*/
constructor(options = {}) {
constructor(options: Partial<BlogOptions> = {}) {
this.charset = options.charset ?? "";
this.languages = options.languages ?? [];
this.url = options.url ? new URL(options.url) : null;
}

/**
* Creates a new blog from the specified JSON object.
* @param {Record<string, any>} json A JSON object representing a blog.
* @returns {Blog} The instance corresponding to the specified JSON object.
* @param json A JSON object representing a blog.
* @returns The instance corresponding to the specified JSON object.
*/
static fromJson(json) {
static fromJson(json: Record<string, any>): Blog {
return new this({
charset: typeof json.blog_charset == "string" ? json.blog_charset : "",
languages: typeof json.blog_lang == "string" ? json.blog_lang.split(",").map(language => language.trim()) : [],
Expand All @@ -46,10 +43,10 @@ export class Blog {

/**
* Returns a JSON representation of this object.
* @returns {Record<string, any>} The JSON representation of this object.
* @returns The JSON representation of this object.
*/
toJSON() {
/** @type {Record<string, any>} */ const map = {blog: this.url?.href ?? ""};
toJSON(): Record<string, any> {
const map: Record<string, any> = {blog: this.url?.href ?? ""};
if (this.charset) map.blog_charset = this.charset;
if (this.languages.length) map.blog_lang = this.languages.join();
return map;
Expand All @@ -58,8 +55,21 @@ export class Blog {

/**
* Defines the options of a {@link Blog} instance.
* @typedef {object} BlogOptions
* @property {string} charset The character encoding for the values included in comments.
* @property {string[]} languages The languages in use on the blog or site, in ISO 639-1 format.
* @property {URL|string} url The blog or site URL.
*/
export interface BlogOptions {

/**
* The character encoding for the values included in comments.
*/
charset: string;

/**
* The languages in use on the blog or site, in ISO 639-1 format.
*/
languages: string[];

/**
* The blog or site URL.
*/
url: URL|string;
}
11 changes: 5 additions & 6 deletions src/check_result.js → src/check_result.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,20 @@
/**
* Specifies the result of a comment check.
* @enum {number}
*/
export const CheckResult = Object.freeze({
export enum CheckResult {

/**
* The comment is not a spam (i.e. a ham).
*/
ham: 0,
ham,

/**
* The comment is a spam.
*/
spam: 1,
spam,

/**
* The comment is a pervasive spam (i.e. it can be safely discarded).
*/
pervasiveSpam: 2
});
pervasiveSpam
}
Loading

0 comments on commit 908e09e

Please sign in to comment.