Completed
Push — master ( d921eb...e48c5a )
by Mathias
01:34
created

src/Botlang.js   A

Size

Lines of Code 58

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
nc 1
dl 0
loc 58
rs 10
noi 2

1 Function

Rating   Name   Duplication   Size   Complexity  
A Botlang.js ➔ ??? 0 11 1
1
'use strict';
2
3
import { Input, Lexer } from './Lexer';
4
import Parser from './Parser';
5
6
class Botlang {
7
  /**
8
   * Create a new Botlang interpreter.
9
   * @param {String} brain
10
   */
11
  constructor(brain) {
12
    const input = new Input(brain),
13
          lexer = new Lexer(input),
14
          parser = new Parser(lexer);
15
16
    /**
17
     * @private
18
     * @type {Object}
19
     */
20
    this.program = parser.parse();
21
  }
22
23
  /**
24
   * @private
25
   * @param {String} trigger
26
   * @param {Object} node
27
   * @type {String}
28
   */
29
  static evalTriggerNode(trigger, node) {
30
    if (0 === node.responses.length) return null;
0 ignored issues
show
Coding Style Best Practice introduced by
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 b = 42 will always be executed, while the logging statement will be executed conditionally.

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.

Loading history...
31
32
    return node.responses[Math.floor(Math.random() * node.responses.length)].value;
33
  }
34
35
  /**
36
   * @private
37
   * @type {Object}
38
   */
39
  match(message) {
40
    let res = null;
41
42
    this.program.body.forEach((el) => {
43
      if ('trigger' === el.type && new RegExp(el.pattern).test(message)) res = Botlang.evalTriggerNode(message, el);
0 ignored issues
show
Coding Style Best Practice introduced by
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 b = 42 will always be executed, while the logging statement will be executed conditionally.

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.

Loading history...
44
    });
45
46
    return res || 'Sorry, I do not know the answer to that question.';
47
  }
48
49
  /**
50
   * Returns a reply to a given message accordingly to the defined brain.
51
   * @return {String}
52
   */
53
  reply(message) {
54
    return this.match(message);
55
  }
56
}
57
58
export default Botlang;
59