diff --git a/src/author.ts b/lib/author.d.ts similarity index 63% rename from src/author.ts rename to lib/author.d.ts index ff76c6a..29ab17f 100644 --- a/src/author.ts +++ b/lib/author.d.ts @@ -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): 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): Author; /** * Returns a JSON representation of this object. * @returns The JSON representation of this object. */ - toJSON(): Record { - const map: Record = {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; } /** @@ -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. diff --git a/src/author.coffee b/src/author.coffee new file mode 100644 index 0000000..9fa80b6 --- /dev/null +++ b/src/author.coffee @@ -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 `"akismet-guaranteed-spam@example.com"`, 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" diff --git a/test/author_test.coffee b/test/author_test.coffee new file mode 100644 index 0000000..ca8fe26 --- /dev/null +++ b/test/author_test.coffee @@ -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: "cedric@belin.io" + comment_author_url: "https://belin.io" + user_agent: "Mozilla/5.0" + user_ip: "127.0.0.1" + user_role: "administrator" + + equal author.email, "cedric@belin.io" + 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: "cedric@belin.io" + 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, "cedric@belin.io" + equal json.comment_author_url, "https://belin.io/" + equal json.user_agent, "Mozilla/5.0" + equal json.user_ip, "192.168.0.1" diff --git a/test/author_test.js b/test/author_test.js deleted file mode 100644 index a81feca..0000000 --- a/test/author_test.js +++ /dev/null @@ -1,63 +0,0 @@ -import {Author, AuthorRole} from "@cedx/akismet"; -import {equal, ok} from "node:assert/strict"; -import {describe, it} from "node:test"; - -/** - * Tests the features of the {@link Author} class. - */ -describe("Author", () => { - describe("fromJson()", () => { - it("should return an empty instance with an empty map", () => { - const 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", () => { - const author = Author.fromJson({ - comment_author: "Cédric Belin", - comment_author_email: "cedric@belin.io", - comment_author_url: "https://belin.io", - user_agent: "Mozilla/5.0", - user_ip: "127.0.0.1", - user_role: "administrator" - }); - - equal(author.email, "cedric@belin.io"); - 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", () => { - const 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", () => { - const json = new Author({ - email: "cedric@belin.io", - 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, "cedric@belin.io"); - equal(json.comment_author_url, "https://belin.io/"); - equal(json.user_agent, "Mozilla/5.0"); - equal(json.user_ip, "192.168.0.1"); - }); - }); -});