{"size": 1452, "ext": "js", "max_stars_repo_path": "src/js/models/lists/property/languages.js", "max_stars_repo_name": "Frumple/statblock5e-creator", "max_stars_repo_stars_event_min_datetime": "2020-02-12T07:29:34.000Z", "max_stars_repo_stars_event_max_datetime": "2021-11-09T22:46:53.000Z", "max_issues_repo_path": "src/js/models/lists/property/languages.js", "max_issues_repo_name": "palikhov/statblock5e-creator", "max_issues_repo_issues_event_min_datetime": "2021-03-10T06:51:13.000Z", "max_issues_repo_issues_event_max_datetime": "2021-03-31T16:31:07.000Z", "max_forks_repo_path": "src/js/models/lists/property/languages.js", "max_forks_repo_name": "palikhov/statblock5e-creator", "max_forks_repo_forks_event_min_datetime": "2020-04-09T15:42:59.000Z", "max_forks_repo_forks_event_max_datetime": "2021-01-12T12:12:31.000Z"}
{"max_stars_count": 14, "max_issues_count": 6, "max_forks_count": 2, "avg_line_length": 22.0, "max_line_length": 78, "alphanum_fraction": 0.5902203857}
import PropertyListModel from './property-list-model.js';
import { convertToInteger } from '../../../helpers/number-helpers.js';

export default class Languages extends PropertyListModel {
  constructor() {
    super('Languages',
          'Language',
          'languages');
  }

  reset() {
    this.clear();
    this.items = ['any one language (usually Common)'];
  }

  clear() {
    super.clear();
    this.telepathy = null;
  }

  get text() {
    let text = super.text;

    if (this.telepathy !== null) {
      if (text !== '') {
        text += ', ';
      }

      text += `telepathy ${this.telepathy} ft.`;
    }

    if (text === '') {
      // This is an EM dash (U+2014).
      // This appears significantly wider than a normal dash.
      text = '—';
    }

    return text;
  }

  fromOpen5e(json) {
    super.fromOpen5e(json);

    const regex = /(?<=^telepathy )\d+(?= ft\.$)/;
    const telepathyItemIndex = this.items.findIndex(item => regex.test(item));

    if (telepathyItemIndex >= 0) {
      const removedItems = this.items.splice(telepathyItemIndex, 1);
      const telepathyMatches = removedItems[0].match(regex);
      const telepathyValue = telepathyMatches[0];
      this.telepathy = convertToInteger(telepathyValue);
    }
  }

  fromJson(json) {
    super.fromJson(json);

    this.telepathy = json.telepathy;
  }

  toJson() {
    const json = super.toJson();
    json.telepathy = this.telepathy;
    return json;
  }
}