Issues (33)

src/Helper/GeezParser.js (5 issues)

1
const Converter = require('../Converter/Converter');
0 ignored issues
show
The constant Converter seems to be never used. Consider removing it.
Loading history...
2
const NotGeezArgumentException = require('../Exception/NotGeezArgumentException');
0 ignored issues
show
The constant NotGeezArgumentException seems to be never used. Consider removing it.
Loading history...
3
4
/**
5
 * GeezParser parse the geez number to a queue.
6
 */
7
module.exports = class GeezParser {
8
  /**
9
   * GeezParser constructor.
10
   *
11
   * @param $geez_number
12
   *
13
   * @throws NotGeezArgumentException
14
   */
15
  constructor($geez_number) {
16
    this.setGeezNumber($geez_number);
17
    this.parsed = null;
18
  }
19
20
  /**
21
   * @param $geez_number
22
   *
23
   * @throws NotGeezArgumentException
24
   */
25
  setGeezNumber($geez_number) {
26
    if (typeof $geez_number !== typeof 'something') {
27
      throw new NotGeezArgumentException(typeof $geez_number);
0 ignored issues
show
The variable NotGeezArgumentException seems to be never declared. If this is a global, consider adding a /** global: NotGeezArgumentException */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
28
    }
29
30
    this.geez_number = $geez_number;
31
  }
32
33
  getParsed() {
34
    return this.parsed;
35
  }
36
37
  /**
38
   * Swing the magic wand and say the spell.
39
   */
40
  parse() {
41
    this.parsed = [];
42
43
    let $block = 0;
44
45
    const $length = this.getLength(this.geez_number);
46
47
    for (let $index = 0; $index < $length; $index += 1) {
48
      $block = this.parseCharacter($index, $block);
49
    }
50
51
    this.pushToQueue($block, 1);
52
  }
53
54
  /**
55
   * Get the length of the string.
56
   *
57
   * @param $geez_number
58
   *
59
   * @return int
60
   */
61
  getLength($geez_number) {
62
    return $geez_number.length;
63
  }
64
65
  /**
66
   * Parse a geez character.
67
   *
68
   * @param $index integer
69
   * @param $block integer
70
   *
71
   * @throws \Geezify\Exception\NotGeezArgumentException
72
   */
73
  parseCharacter($index, $block) {
74
    const $ascii_number = this.parseGeezAtIndex($index);
75
76
    /* eslint-disable no-param-reassign */
77
    if (this.isNotGeezSeparator($ascii_number)) {
78
      $block += $ascii_number;
79
    } else {
80
      this.pushToQueue($block, $ascii_number);
81
      $block = 0;
82
    }
83
    /* eslint-enable no-param-reassign */
84
85
    return $block;
86
  }
87
88
  /**
89
   * Get the ascii number from geez number string.
90
   *
91
   * @param $index
92
   *
93
   * @throws \Geezify\Exception\NotGeezArgumentException
94
   *
95
   * @return int
96
   */
97
  parseGeezAtIndex($index) {
98
    const $geez_char = this.getCharacterAt(this.geez_number, $index);
99
100
    return parseInt(this.getAsciiNumber($geez_char), 10);
101
  }
102
103
  /**
104
   * Fetch z character at $index from the geez number string.
105
   *
106
   * @param $geez_number
107
   * @param $index
108
   *
109
   * @return string
110
   */
111
  getCharacterAt($geez_number, $index) {
112
    return $geez_number.charAt($index);
113
  }
114
115
  /**
116
   * Convert geez number character to ascii.
117
   *
118
   * @param $geez_number
119
   *
120
   * @throws NotGeezArgumentException
121
   *
122
   * @return int
123
   */
124
  getAsciiNumber($geez_number) {
125
    const $ascii_number = Object.values(Converter.GEEZ_NUMBERS).indexOf($geez_number);
0 ignored issues
show
The variable Converter seems to be never declared. If this is a global, consider adding a /** global: Converter */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
126
127
    if ($ascii_number === -1) {
128
      throw new NotGeezArgumentException($geez_number);
0 ignored issues
show
The variable NotGeezArgumentException seems to be never declared. If this is a global, consider adding a /** global: NotGeezArgumentException */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
129
    }
130
131
    return Object.keys(Converter.GEEZ_NUMBERS)[$ascii_number];
132
  }
133
134
  /**
135
   * @param $ascii_number
136
   *
137
   * @return bool
138
   */
139
  isNotGeezSeparator($ascii_number) {
140
    return $ascii_number < 99;
141
  }
142
143
  /**
144
   * Push to the queue.
145
   *
146
   * @param $block
147
   * @param $separator
148
   */
149
  pushToQueue($block, $separator) {
150
    this.parsed.push({ block: $block, separator: $separator });
151
  }
152
};
153