src/Botlang.js   A
last analyzed

Size

Lines of Code 88

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 88
rs 10
noi 3

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
import pkg from '../package.json';
6
7
/**
8
 * Botlnag interpreter.
9
 */
10
class Botlang {
11
  /**
12
   * Create a new Botlang interpreter.
13
   * @param {String} brain
14
   */
15
  constructor(brain) {
16
    const input = new Input(brain),
17
          lexer = new Lexer(input),
18
          parser = new Parser(lexer);
19
20
    /**
21
     * @private
22
     * @type {Object}
23
     */
24
    this.program = parser.parse();
25
  }
26
27
  /**
28
   * @private
29
   * @param {String} trigger
30
   * @param {Object} node
31
   * @type {String}
32
   */
33
  static evalTriggerNode(trigger, node) {
34
    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...
35
36
    let reply = node.responses[Math.floor(Math.random() * node.responses.length)].value;
37
38
    // Do string substitution
39
    const pos = reply.indexOf('$');
40
    if (-1 < pos) {
41
      const pattern = node.src.replace(/\*/, '(\\w+)').replace(/\$/, '(\\w+)'),
42
            match = new RegExp(pattern, 'i').exec(trigger);
43
44
      if (Array.isArray(match)) {
45
        const substitution = undefined !== match[2]
46
          ? match[2]
47
          : match[1];
48
49
        reply = `${reply.substring(0, pos)}${substitution}${reply.substring(pos + 1)}`;
50
      }
51
    }
52
53
    return reply;
54
  }
55
56
  /**
57
   * @private
58
   * @type {Object}
59
   */
60
  match(message) {
61
    let res = null;
62
63
    this.program.body.forEach((el) => {
64
      if ('trigger' === el.type && new RegExp(el.pattern, 'i').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...
65
    });
66
67
    return res || 'Sorry, I do not know the answer to that question.';
68
  }
69
70
  /**
71
   * Returns a reply to a given message accordingly to the defined brain.
72
   * @return {String}
73
   */
74
  reply(message) {
75
    if ('' === message) return '';
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...
76
    return this.match(message);
77
  }
78
79
  /**
80
   * Return the current version
81
   * @return {String}
82
   */
83
  static version() {
84
    return pkg.version;
85
  }
86
}
87
88
export default Botlang;
89