All files / core character-dictionary.ts

61.54% Statements 24/39
40.91% Branches 9/22
68.75% Functions 11/16
57.14% Lines 20/35
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107          5x       30x                   3x         1x               179x 174x 173x   1x                 38x   30x 38x     30x 1x       29x 1x   1x 1x     1x 1x       28x                                                                            
import Character from './character';
 
/**
 * The dictionary contains different characters which are accessible via the find method
 */
export default class CharacterDictionary {
  private _characters: Character[];
 
  constructor() {
    this._characters = [];
  }
 
  /** Returns the different characters in the dictionary */
  public get characters() {
    return this._characters
  }
 
  /** Returns the height of the tallest character within the dictionary */
  public get height() {
    return Math.max.apply(Math, this._characters.map(x => x.height))
  }
 
  /** Returns the length of the dictionary */
  public get length() {
    return this._characters.length;
  }
 
  /**
 * Finds an input within the dictionary 
 * @param input The input corresponding
 */
  public find(input: string): Character {
    const character = this._characters.filter(x => x.hasPattern(input));
    if (character && character.length > 0) {
      return character[0];
    }
    throw `Could not find character ${input} in the alphabet`;
  }
 
  /**
   * Adds characters to the dictionary
   * @param pendingCharacters Characters pending to be added to the dictionary
   */
  public add(pendingCharacters: Character[]) {
    // Make sure no pending characters have the same pattern
    const pendingPatterns: string[] = pendingCharacters.map(x => x.pattern);
 
    const duplicatedPendingPatterns = pendingPatterns.filter((value, index, array) => {
      return array.indexOf(value) != index;
    })
 
    if (duplicatedPendingPatterns.length > 0) {
      throw `Pattern already used by another pending character`;
    }
 
    // Make sure no pending characters have the same pattern as already added characters
    if (this._characters.length > 0) {
      const alreadyAddedPatterns: string[] = this._characters.map(x => x.pattern);
 
      const duplicatedPatterns = alreadyAddedPatterns.filter((value) => {
        return pendingPatterns.indexOf(value) != -1;
      })
 
      Eif (duplicatedPatterns.length > 0) {
        throw `Pattern already used by another character`;
      }
    }
 
    this._characters.push(...pendingCharacters);
  }
 
  /**
 * Edits a character from the dictionary
 * @param pendingCharacter Character pending to be edited from the dictionary
 */
  public edit(pendingCharacter: Character) {
    let edited = false;
    this._characters.forEach((character, index, arr) => {
      if (character.pattern == pendingCharacter.pattern && !edited) {
        arr[index] = pendingCharacter;
        edited = true;
      }
    });
    if (!edited) {
      throw `Could not find character ${pendingCharacter.pattern} in the alphabet. Aborted edit operation`;
    }
  }
 
  /**
* Deletes a character from the dictionary
* @param pendingCharacter Character pending to be deleted from the dictionary
*/
  public delete(pendingCharacter: Character) {
    let deleted = false;
    this._characters.forEach((character, index, arr) => {
      if (character.pattern == pendingCharacter.pattern && !deleted) {
        arr.splice(index, 1);
        deleted = true;
      }
    });
    if (!deleted) {
      throw `Could not find character ${pendingCharacter.pattern} in the alphabet. Aborted delete operation`;
    }
  }
};