1 | import Markov from './Markov'; |
||
2 | import Static from './Static'; |
||
3 | |||
4 | export default class BaseGenerator { |
||
5 | // eslint-disable-next-line sonarjs/cognitive-complexity |
||
6 | constructor(fatum) { |
||
7 | this.fatum = fatum; |
||
8 | if (this.constructor.model) { |
||
9 | const { type } = this.constructor.model; |
||
10 | |||
11 | if (type === 'markov') { |
||
12 | this._markov = new Markov(this.constructor.model, this.constructor.safeLength); |
||
13 | } |
||
14 | |||
15 | if (type === 'merged_types') { |
||
16 | this._generators = {}; |
||
17 | for (const typeName of Object.keys(this.constructor.model.types)) { |
||
18 | const model = this.constructor.model.types[typeName]; |
||
19 | |||
20 | if (model.type === 'markov') { |
||
21 | this._generators[typeName] = new Markov(model); |
||
22 | } |
||
23 | |||
24 | if (model.type === 'static') { |
||
25 | this._generators[typeName] = new Static(model); |
||
26 | } |
||
27 | } |
||
28 | } |
||
29 | } |
||
30 | } |
||
31 | |||
32 | generateByModel(type, ...params) { |
||
33 | if (this._markov) return this._markov.generate(type, ...params); |
||
0 ignored issues
–
show
|
|||
34 | if (this._generators) return this._generators[type].generate(...params); |
||
0 ignored issues
–
show
Curly braces around statements make for more readable code and help prevent bugs when you add further statements.
Consider adding curly braces around all statements when they are executed conditionally. This is optional if there is only one statement, but leaving them out can lead to unexpected behaviour if another statement is added later. Consider: if (a > 0)
b = 42;
If you or someone else later decides to put another statement in, only the first statement will be executed. if (a > 0)
console.log("a > 0");
b = 42;
In this case the statement if (a > 0) {
console.log("a > 0");
b = 42;
}
ensures that the proper code will be executed conditionally no matter how many statements are added or removed. ![]() There is no return statement if
this._generators is false . Are you sure this is correct? If so, consider adding return; explicitly.
This check looks for functions where a Consider this little piece of code function isBig(a) {
if (a > 5000) {
return "yes";
}
}
console.log(isBig(5001)); //returns yes
console.log(isBig(42)); //returns undefined
The function This behaviour may not be what you had intended. In any case, you can add a
![]() |
|||
35 | } |
||
36 | } |
||
37 |
Consider adding curly braces around all statements when they are executed conditionally. This is optional if there is only one statement, but leaving them out can lead to unexpected behaviour if another statement is added later.
Consider:
If you or someone else later decides to put another statement in, only the first statement will be executed.
In this case the statement
b = 42
will always be executed, while the logging statement will be executed conditionally.ensures that the proper code will be executed conditionally no matter how many statements are added or removed.