Skip to content

Commit

Permalink
Port the Blog class
Browse files Browse the repository at this point in the history
  • Loading branch information
cedx committed Nov 7, 2024
1 parent bbeb406 commit 19e0476
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 65 deletions.
23 changes: 4 additions & 19 deletions src/blog.ts → lib/blog.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export class Blog {
/**
* The languages in use on the blog or site, in ISO 639-1 format.
*/
languages: Array<string>;
languages: Set<string>;

/**
* The blog or site URL.
Expand All @@ -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<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()) : [],
url: typeof json.blog == "string" ? json.blog : ""
});
}
static fromJson(json: Record<string, any>): Blog;

/**
* Returns a JSON representation of this object.
* @returns The JSON representation of this object.
*/
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;
}
toJSON(): Record<string, any>;
}

/**
Expand Down
27 changes: 27 additions & 0 deletions src/blog.coffee
Original file line number Diff line number Diff line change
@@ -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
36 changes: 36 additions & 0 deletions test/blog_test.coffee
Original file line number Diff line number Diff line change
@@ -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"
46 changes: 0 additions & 46 deletions test/blog_test.js

This file was deleted.

0 comments on commit 19e0476

Please sign in to comment.