diff --git a/src/blog.ts b/lib/blog.d.ts similarity index 58% rename from src/blog.ts rename to lib/blog.d.ts index 905ceb4..2cb85ec 100644 --- a/src/blog.ts +++ b/lib/blog.d.ts @@ -11,7 +11,7 @@ export class Blog { /** * The languages in use on the blog or site, in ISO 639-1 format. */ - languages: Array; + languages: Set; /** * The blog or site URL. @@ -22,35 +22,20 @@ export class Blog { * Creates a new blog. * @param options An object providing values to initialize this instance. */ - constructor(options: BlogOptions = {}) { - this.charset = options.charset ?? ""; - this.languages = options.languages ?? []; - this.url = options.url ? new URL(options.url) : null; - } + constructor(options?: BlogOptions); /** * Creates a new blog from the specified JSON object. * @param json A JSON object representing a blog. * @returns The instance corresponding to the specified JSON object. */ - static fromJson(json: Record): 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()) : [], - url: typeof json.blog == "string" ? json.blog : "" - }); - } + static fromJson(json: Record): Blog; /** * Returns a JSON representation of this object. * @returns The JSON representation of this object. */ - toJSON(): Record { - const map: Record = {blog: this.url?.href ?? ""}; - if (this.charset) map.blog_charset = this.charset; - if (this.languages.length) map.blog_lang = this.languages.join(); - return map; - } + toJSON(): Record; } /** diff --git a/src/blog.coffee b/src/blog.coffee new file mode 100644 index 0000000..6a6364e --- /dev/null +++ b/src/blog.coffee @@ -0,0 +1,27 @@ +# Represents the front page or home URL transmitted when making requests. +export class Blog + + # Creates a new blog. + constructor: (options = {}) -> + + # The character encoding for the values included in comments. + @charset = options.charset ? "" + + # The languages in use on the blog or site, in ISO 639-1 format. + @languages = new Set(options.languages ? []) + + # The blog or site URL. + @url = if options.url then new URL options.url else null + + # Creates a new blog from the specified JSON object. + @fromJson: (json) -> new @ + charset: if typeof json.blog_charset == "string" then json.blog_charset else "" + languages: if typeof json.blog_lang == "string" then json.blog_lang.split(",").map (language) -> language.trim() else [] + url: if typeof json.blog == "string" then json.blog else "" + + # Returns a JSON representation of this object. + toJSON: -> + map = blog: @url?.href ? "" + map.blog_charset = @charset if @charset + map.blog_lang = Array.from(@languages).join "," if @languages.size + map diff --git a/test/blog_test.coffee b/test/blog_test.coffee new file mode 100644 index 0000000..d8f3f2a --- /dev/null +++ b/test/blog_test.coffee @@ -0,0 +1,36 @@ +import {Blog} from "@cedx/akismet" +import {deepEqual, equal, ok} from "node:assert/strict" +import {describe, it} from "node:test" + +# Tests the features of the `Blog` class. +describe "Blog", -> + describe "fromJson()", -> + it "should return an empty instance with an empty map", -> + blog = Blog.fromJson {} + equal blog.charset.length, 0 + equal blog.languages.length, 0 + equal blog.url, null + + it "should return an initialized instance with a non-empty map", -> + blog = Blog.fromJson + blog: "https://github.com/cedx/akismet.js" + blog_charset: "UTF-8" + blog_lang: "en, fr" + + equal blog.charset, "UTF-8" + deepEqual blog.languages, ["en", "fr"] + ok blog.url instanceof URL + equal blog.url.href, "https://github.com/cedx/akismet.js" + + describe "toJSON()", -> + it "should return only the blog URL with a newly created instance", -> + json = new Blog(url: "https://github.com/cedx/akismet.js").toJSON() + equal Object.keys(json).length, 1 + equal json.blog, "https://github.com/cedx/akismet.js" + + it "should return a non-empty map with an initialized instance", -> + json = new Blog(charset: "UTF-8", languages: ["en", "fr"], url: "https://github.com/cedx/akismet.js").toJSON() + equal Object.keys(json).length, 3 + equal json.blog, "https://github.com/cedx/akismet.js" + equal json.blog_charset, "UTF-8" + equal json.blog_lang, "en,fr" diff --git a/test/blog_test.js b/test/blog_test.js deleted file mode 100644 index e99ce57..0000000 --- a/test/blog_test.js +++ /dev/null @@ -1,46 +0,0 @@ -import {Blog} from "@cedx/akismet"; -import {deepEqual, equal, ok} from "node:assert/strict"; -import {describe, it} from "node:test"; - -/** - * Tests the features of the {@link Blog} class. - */ -describe("Blog", () => { - describe("fromJson()", () => { - it("should return an empty instance with an empty map", () => { - const blog = Blog.fromJson({}); - equal(blog.charset.length, 0); - equal(blog.languages.length, 0); - equal(blog.url, null); - }); - - it("should return an initialized instance with a non-empty map", () => { - const blog = Blog.fromJson({ - blog: "https://github.com/cedx/akismet.js", - blog_charset: "UTF-8", - blog_lang: "en, fr" - }); - - equal(blog.charset, "UTF-8"); - deepEqual(blog.languages, ["en", "fr"]); - ok(blog.url instanceof URL); - equal(blog.url.href, "https://github.com/cedx/akismet.js"); - }); - }); - - describe("toJSON()", () => { - it("should return only the blog URL with a newly created instance", () => { - const json = new Blog({url: "https://github.com/cedx/akismet.js"}).toJSON(); - equal(Object.keys(json).length, 1); - equal(json.blog, "https://github.com/cedx/akismet.js"); - }); - - it("should return a non-empty map with an initialized instance", () => { - const json = new Blog({charset: "UTF-8", languages: ["en", "fr"], url: "https://github.com/cedx/akismet.js"}).toJSON(); - equal(Object.keys(json).length, 3); - equal(json.blog, "https://github.com/cedx/akismet.js"); - equal(json.blog_charset, "UTF-8"); - equal(json.blog_lang, "en,fr"); - }); - }); -});