-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGenerator.js
87 lines (84 loc) · 2.1 KB
/
Generator.js
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
/**
* Author James Torkornoo
* This helper class generates a number array for StringCalculator to use for calculation
* It handles negative numbers, split method selection etc.
* @export
* @class Generator
*/
export default class Generator {
constructor(str) {
this.finalArray = [];
this.checkVariable;
this.negativeNumArray = [];
this.str = str;
}
/**
*
*
* @param {*} element
* @memberof Generator
*/
findNegative(element) {
if (Math.sign(element) == -1) {
this.negativeNumArray.push(element);
console.log(
`\nThe number(s) after the colon that caused the error: ${this.negativeNumArray}`
);
throw "Negative numbers not allowed";
}
}
/**
*
*
* @memberof Generator
*/
checkString() {
switch (true) {
case this.str.indexOf(",") !== -1 && this.str.indexOf("\n") === -1:
this.checkVariable = 0;
break;
case this.str.indexOf("\n") !== -1 && this.str.indexOf(",") !== -1:
this.checkVariable = 1;
break;
case this.str.indexOf(",") === -1:
this.checkVariable = -1;
break;
}
}
/**
*
*
* @memberof Generator
*/
convertString() {
this.checkString();
console.log(`This is the string provided ${this.str}`);
switch (this.checkVariable) {
case 0:
this.finalArray = this.str.split(/,|[0-9]{5}/g).map((stringElement) => {
this.findNegative(stringElement);
return parseInt(stringElement, 10);
});
case 1:
this.finalArray = this.str
.split(/(?:,|\n|[0-9]{5})+/g)
.map((stringElement) => {
this.findNegative(stringElement);
return parseInt(stringElement, 10);
});
case -1:
this.finalArray = this.str
.split(/(?:[^0-9\-])/g)
.map((stringElement) => {
this.findNegative(stringElement);
return parseInt(stringElement, 10);
})
.filter((element) => {
return !isNaN(element);
});
}
}
get finalNumArray() {
return this.finalArray;
}
}