Issues (33)

src/Helper/GeezCalculator.js (10 issues)

1
const ONE = 1;
0 ignored issues
show
The constant ONE seems to be never used. Consider removing it.
Loading history...
2
const HUNDRED = 100;
0 ignored issues
show
The constant HUNDRED seems to be never used. Consider removing it.
Loading history...
3
const TEN_THOUSAND = 10000;
0 ignored issues
show
The constant TEN_THOUSAND seems to be never used. Consider removing it.
Loading history...
4
5
/**
6
 * GeezCalculator calculate the ascii number from the parsed queue.
7
 *
8
 * @author Sam As End <4sam21{at}gmail.com>
9
 */
10
module.exports = class GeezCalculator {
11
  constructor($queue) {
12
    this.queue = $queue;
13
    this.total = 0;
14
    this.sub_total = 0;
15
  }
16
17
  /**
18
   * Do the magic.
19
   */
20
  calculate() {
21
    this.resetSubTotalToZero();
22
23
    this.queue.forEach(($token) => {
24
      this.processToken($token);
25
    });
26
  }
27
28
  /**
29
   * set the sub total attribute to zero.
30
   */
31
  resetSubTotalToZero() {
32
    this.sub_total = 0;
33
  }
34
35
  /**
36
   * Process a single token from the Queue.
37
   *
38
   * @param $token
39
   */
40
  processToken($token) {
41
    const $block = $token.block;
42
    const $separator = $token.separator;
43
44
    this.processBySeparator($block, $separator);
45
  }
46
47
  /**
48
   * Process based on separator.
49
   *
50
   * @param $block
51
   * @param $separator
52
   */
53
  processBySeparator($block, $separator) {
54
    switch ($separator) {
55
      case ONE:
0 ignored issues
show
The variable ONE seems to be never declared. If this is a global, consider adding a /** global: ONE */ 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...
56
        this.addToTotal($block);
57
        break;
58
      case HUNDRED:
0 ignored issues
show
The variable HUNDRED seems to be never declared. If this is a global, consider adding a /** global: HUNDRED */ 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...
59
        this.updateSubTotal($block);
60
        break;
61
      case TEN_THOUSAND:
0 ignored issues
show
The variable TEN_THOUSAND seems to be never declared. If this is a global, consider adding a /** global: TEN_THOUSAND */ 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...
62
      default:
63
        this.updateTotal($block);
64
        break;
65
    }
66
  }
67
68
  /**
69
   * Add the sub total and the block to total
70
   * and reset sub total to zero.
71
   *
72
   * @param $block
73
   *
74
   * @return void
75
   */
76
  addToTotal($block) {
77
    this.total += this.sub_total + $block;
78
    this.resetSubTotalToZero();
79
  }
80
81
  /**
82
   * Is the leading block?
83
   *
84
   * @param $block
85
   *
86
   * @return bool
87
   */
88
  isLeading($block) {
89
    return this.isBlockZero($block) && this.isSubtotalZero();
90
  }
91
92
  /**
93
   * Is the value of block zero?
94
   *
95
   * @param $block
96
   *
97
   * @return bool
98
   */
99
  isBlockZero($block) {
100
    return this.isZero($block);
101
  }
102
103
  /**
104
   * Is a number zero?
105
   *
106
   * @param $number
107
   *
108
   * @return boolean
109
   */
110
  isZero($number) {
111
    return $number === 0;
112
  }
113
114
  /**
115
   * Is sub total attribute zero?
116
   *
117
   * @return bool
118
   */
119
  isSubtotalZero() {
120
    return this.isZero(this.sub_total);
121
  }
122
123
  /**
124
   * Add number to sun total.
125
   *
126
   * @param $number integer
127
   */
128
  addToSubTotal($number) {
129
    this.sub_total += $number;
130
  }
131
132
  /**
133
   * Is the leading 10k?
134
   *
135
   * @param $block
136
   *
137
   * @return bool
138
   */
139
  isLeadingTenThousand($block) {
140
    return this.isTotalZero() && this.isLeading($block);
141
  }
142
143
  /**
144
   * Is the total attribute zero?
145
   *
146
   * @return bool
147
   */
148
  isTotalZero() {
149
    return this.isZero(this.total);
150
  }
151
152
  /**
153
   * Multiply the total attribute by ten thousand.
154
   */
155
  multiplyTotalBy10k() {
156
    this.total *= TEN_THOUSAND;
0 ignored issues
show
The variable TEN_THOUSAND seems to be never declared. If this is a global, consider adding a /** global: TEN_THOUSAND */ 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...
157
  }
158
159
  /**
160
   * Return the calculated ascii number.
161
   *
162
   * @return int
163
   */
164
  getCalculated() {
165
    return this.total;
166
  }
167
168
  /**
169
   * Update the sub total attribute.
170
   *
171
   * @param $block
172
   */
173
  updateSubTotal($block) {
174
    /* eslint-disable no-param-reassign */
175
    if (this.isLeading($block)) {
176
      $block = ONE;
0 ignored issues
show
The variable ONE seems to be never declared. If this is a global, consider adding a /** global: ONE */ 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...
177
    }
178
179
    $block *= HUNDRED;
0 ignored issues
show
The variable HUNDRED seems to be never declared. If this is a global, consider adding a /** global: HUNDRED */ 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...
180
    /* eslint-enable no-param-reassign */
181
182
    this.addToSubTotal($block);
183
  }
184
185
  /**
186
   * Update the sub total attribute.
187
   *
188
   * @param $block
189
   */
190
  updateTotal($block) {
191
    if (this.isLeadingTenThousand($block)) {
192
      // eslint-disable-next-line no-param-reassign
193
      $block = ONE;
0 ignored issues
show
The variable ONE seems to be never declared. If this is a global, consider adding a /** global: ONE */ 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...
194
    }
195
196
    this.addToTotal($block);
197
    this.multiplyTotalBy10k();
198
  }
199
};
200