Skip to content
Permalink
Browse files

Reverted the `Author` class to JavaScript

  • Loading branch information...
cedx committed Jun 1, 2019
1 parent 6cd1ded commit 6341863c1661b404035fb6395c12bd60e9c6cdd7
Showing with 93 additions and 84 deletions.
  1. +89 −0 lib/core/author.js
  2. +0 −78 src/author.ts
  3. +4 −6 test/{author_test.ts → author_test.js}
@@ -0,0 +1,89 @@
/**
* Defines the options of an {@link Author} instance.
* @typedef {object} AuthorOptions
* @property {string} [email] The author's mail address.
* @property {string} [name] The author's name.
* @property {string} [role] The role of the author.
* @property {?URL} [url] The URL of the author's website.
*/

/** Represents the author of a comment. */
export class Author {

/**
* Creates a new author.
* @param {string} ipAddress The author's IP address.
* @param {string} userAgent The author's user agent, that is the string identifying the Web browser used to submit comments.
* @param {AuthorOptions} [options] An object specifying values used to initialize this instance.
*/
constructor(ipAddress, userAgent, options = {}) {
const {email = '', name = '', role = '', url = null} = options;

/**
* The author's mail address.
* @type {string}
*/
this.email = email;

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

/**
* The author's name.
* @type {string}
*/
this.name = name;

/**
* The role of the author.
* @type {string}
*/
this.role = role;

/**
* The URL of the author's website.
* @type {?URL}
*/
this.url = url;

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

/**
* Creates a new author from the specified JSON map.
* @param {object} map A JSON map representing an author.
* @return {Author} The instance corresponding to the specified JSON map.
*/
static fromJson(map) {
return new Author(typeof map.user_ip == 'string' ? map.user_ip : '', typeof map.user_agent == 'string' ? map.user_agent : '', {
email: typeof map.comment_author_email == 'string' ? map.comment_author_email : '',
name: typeof map.comment_author == 'string' ? map.comment_author : '',
role: typeof map.user_role == 'string' ? map.user_role : '',
url: typeof map.comment_author_url == 'string' ? new URL(map.comment_author_url) : null
});
}

/**
* Converts this object to a map in JSON format.
* @return {object} The map in JSON format corresponding to this object.
*/
toJSON() {
const map = {
user_agent: this.userAgent,
user_ip: this.ipAddress
};

if (this.name.length) map.comment_author = this.name;
if (this.email.length) map.comment_author_email = this.email;
if (this.url) map.comment_author_url = this.url.href;
if (this.role.length) map.user_role = this.role;
return map;
}
}

This file was deleted.

@@ -1,11 +1,10 @@
/* tslint:disable: no-unused-expression */
import {expect} from 'chai';
import {Author} from '../src';
import chai from 'chai';
import {Author} from '../lib/index.js';

/** Tests the features of the [[Author]] class. */
/** Tests the features of the {@link Author} class. */
describe('Author', () => {
const {expect} = chai;

/** Tests the `Author.fromJson()` method. */
describe('.fromJson()', () => {
it('should return an empty instance with an empty map', () => {
const author = Author.fromJson({});
@@ -29,7 +28,6 @@ describe('Author', () => {
});
});

/** Tests the `Author#toJSON()` method. */
describe('#toJSON()', () => {
it('should return only the IP address and user agent with a newly created instance', () => {
const data = new Author('127.0.0.1', 'Doom/6.6.6').toJSON();

0 comments on commit 6341863

Please sign in to comment.
You can’t perform that action at this time.