Skip to content

Commit

Permalink
Port the Author class
Browse files Browse the repository at this point in the history
  • Loading branch information
cedx committed Nov 7, 2024
1 parent 0d5392e commit bbeb406
Show file tree
Hide file tree
Showing 4 changed files with 106 additions and 92 deletions.
34 changes: 5 additions & 29 deletions src/author.ts → lib/author.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,44 +37,20 @@ export class Author {
* Creates a new author.
* @param options An object providing values to initialize this instance.
*/
constructor(options: AuthorOptions = {}) {
this.email = options.email ?? "";
this.ipAddress = options.ipAddress ?? "";
this.name = options.name ?? "";
this.role = options.role ?? "";
this.url = options.url ? new URL(options.url) : null;
this.userAgent = options.userAgent ?? "";
}
constructor(options?: AuthorOptions);

/**
* Creates a new author from the specified JSON object.
* @param json A JSON object representing an author.
* @returns The instance corresponding to the specified JSON object.
*/
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 : "",
name: typeof json.comment_author == "string" ? json.comment_author : "",
role: typeof json.user_role == "string" ? json.user_role : "",
url: typeof json.comment_author_url == "string" ? json.comment_author_url : "",
userAgent: typeof json.user_agent == "string" ? json.user_agent : ""
});
}
static fromJson(json: Record<string, any>): Author;

/**
* Returns a JSON representation of this object.
* @returns The JSON representation of this object.
*/
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;
if (this.url) map.comment_author_url = this.url.href;
if (this.userAgent) map.user_agent = this.userAgent;
return map;
}
toJSON(): Record<string, any>;
}

/**
Expand Down Expand Up @@ -116,13 +92,13 @@ export type AuthorOptions = Partial<{
/**
* Specifies the role of an author.
*/
export const AuthorRole = Object.freeze({
export const AuthorRole: Readonly<{

/**
* The author is an administrator.
*/
administrator: "administrator"
});
}>;

/**
* Specifies the role of an author.
Expand Down
48 changes: 48 additions & 0 deletions src/author.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
# Represents the author of a comment.
export class Author

# Creates a new author.
constructor: (options = {}) ->

# The author's mail address. If you set it to `"[email protected]"`, Akismet will always return `true`.
@email = options.email ? ""

# The author's IP address.
@ipAddress = options.ipAddress ? ""

# The author's name. If you set it to `"viagra-test-123"`, Akismet will always return `true`.
@name = options.name ? ""

# The author's role. If you set it to `"administrator"`, Akismet will always return `false`.
@role = options.role ? ""

# The URL of the author's website.
@url = if options.url then new URL options.url else null

# The author's user agent, that is the string identifying the Web browser used to submit comments.
@userAgent = options.userAgent ? ""

# Creates a new author from the specified JSON object.
@fromJson: (json) -> new @
email: if typeof json.comment_author_email == "string" then json.comment_author_email else ""
ipAddress: if typeof json.user_ip == "string" then json.user_ip else ""
name: if typeof json.comment_author == "string" then json.comment_author else ""
role: if typeof json.user_role == "string" then json.user_role else ""
url: if typeof json.comment_author_url == "string" then json.comment_author_url else ""
userAgent: if typeof json.user_agent == "string" then json.user_agent else ""

# Returns a JSON representation of this object.
toJSON: ->
map = user_ip: @ipAddress
map.comment_author_email = @email if @email
map.comment_author = @name if @name
map.user_role = @role if @role
map.comment_author_url = @url.href if @url
map.user_agent = @userAgent if @userAgent
map

# Specifies the role of an author.
export AuthorRole = Object.freeze

# The author is an administrator.
administrator: "administrator"
53 changes: 53 additions & 0 deletions test/author_test.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import {Author, AuthorRole} from "@cedx/akismet"
import {equal, ok} from "node:assert/strict"
import {describe, it} from "node:test"

# Tests the features of the `Author` class.
describe "Author", ->
describe "fromJson()", ->
it "should return an empty instance with an empty map", ->
author = Author.fromJson {}
equal author.email.length, 0
equal author.ipAddress.length, 0
equal author.name.length, 0
equal author.role.length, 0
equal author.url, null
equal author.userAgent.length, 0

it "should return an initialized instance with a non-empty map", ->
author = Author.fromJson
comment_author: "Cédric Belin"
comment_author_email: "[email protected]"
comment_author_url: "https://belin.io"
user_agent: "Mozilla/5.0"
user_ip: "127.0.0.1"
user_role: "administrator"

equal author.email, "[email protected]"
equal author.ipAddress, "127.0.0.1"
equal author.role, AuthorRole.administrator
ok author.url instanceof URL
equal author.url.href, "https://belin.io/"
equal author.userAgent, "Mozilla/5.0"

describe "toJSON()", ->
it "should return only the IP address with a newly created instance", ->
json = new Author(ipAddress: "127.0.0.1").toJSON()
equal Object.keys(json).length, 1
equal json.user_ip, "127.0.0.1"

it "should return a non-empty map with an initialized instance", ->
json = new Author(
email: "[email protected]"
ipAddress: "192.168.0.1"
name: "Cédric Belin"
url: "https://belin.io"
userAgent: "Mozilla/5.0"
).toJSON()

equal Object.keys(json).length, 5
equal json.comment_author, "Cédric Belin"
equal json.comment_author_email, "[email protected]"
equal json.comment_author_url, "https://belin.io/"
equal json.user_agent, "Mozilla/5.0"
equal json.user_ip, "192.168.0.1"
63 changes: 0 additions & 63 deletions test/author_test.js

This file was deleted.

0 comments on commit bbeb406

Please sign in to comment.