models/character_name.js

/**
 * Represents a character's name with various attributes.
 *
 * @class CharacterName
 * @property {String} given - The given (first) name of the character.
 * @property {String} [nickname] - The nickname or alternative name of the character.
 * @property {String} [family] - The family (last) name of the character.
 * @property {String} [heritage] - The heritage of the character (anglo or latino)
 * @property {String} [gender] - The gender of the character.
 * @property {String} [born] - The maiden name of the character.
 *
 * @example
 * const name = new CharacterName({
 *   given: 'Jane',
 *   family: 'Doe',
 *   nickname: 'Janey',
 *   heritage: 'anglo',
 *   gender: 'male',
 *   born: 'Smith'
 * });
 * name.toString();
 * => "Janey"
 * name.toLastNameString();
 * => "Doe (born Smith)"
 */
class CharacterName {
  /**
   * Creates an instance of CharacterName.
   * @param [params] {Object}
   * @param {String} [params.given] - The given (first) name of the character.
   * @param {String} [params.nickname] - The nickname or alternative name of the character.
   * @param {String} [params.family] - The family (last) name of the character.
   * @param {String} [params.heritage] - The heritage of the character (anglo or latino)
   * @param {String} [params.gender] - The gender of the character.
   * @param {String} [params.born] - The maiden name of the character.
   */
  constructor({ given, nickname, family, heritage, gender, born } = {}) {
    this.given = given;
    // Nickname includes the rest of the name, so it's either the nickname, or the rest
    this.nickname = nickname;
    this.family = family;
    this.heritage = heritage;
    this.gender = gender;
    this.born = born; // maiden name
  }
  /**
   * @returns {String} the family name, with the born (maiden) name if present
   */
  toLastNameString() {
    return this.family + (this.born ? ` (born ${this.born})` : "");
  }
  /**
   * @returns {String} the full name, preferring nickname if present
   */
  toString() {
    if (this.nickname) {
      return this.nickname;
    }
    return `${this.given} ${this.toLastNameString()}`;
  }
}

export default CharacterName;