analysis/index.js   A
last analyzed

Complexity

Total Complexity 10
Complexity/F 2

Size

Lines of Code 50
Function Count 5

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 0
c 1
b 0
f 0
nc 4
dl 0
loc 50
rs 10
wmc 10
mnd 3
bc 11
fnc 5
bpm 2.2
cpm 2
noi 2

2 Functions

Rating   Name   Duplication   Size   Complexity  
A Analysis._analyzed 0 16 1
B Analysis.getFrequentText 0 27 4
1
/**
2
 * hsBot: Supply template and your bot is ready.
3
 * 
4
 * Hardik Shah <[email protected]>
5
 * 
6
 * MIT License
7
 */
8
9
function Analysis(){
10
11
}
12
13
Analysis.prototype._analyzed = function(userData){
14
  var analyzedData = {};
15
  var loggedInTime = userData.loggedIn;
16
  var lastActivity = userData.activities[userData.activities.length-1].ut;
17
  analyzedData.timeSpent = lastActivity - loggedInTime;
18
  var frequentArr = this.getFrequentText(userData.activities);
19
  frequentArr.forEach(function(obj){
20
    if(obj.type == "question"){
21
      analyzedData.frequentBotText = obj.text;
22
    }
23
    if(obj.type == "command"){
24
      analyzedData.frequentUserText = obj.text;
25
    }
26
  });
27
  return analyzedData;
28
};
29
30
Analysis.prototype.getFrequentText = function(activities){
31
  var countArr = {
32
    question : {},
33
    command: {}
34
  };
35
  activities.forEach(function(act){
36
    countArr.question[act.preQ] = (countArr.question[act.preQ]||0) + 1;
37
    countArr.command[act.pattern] = (countArr.command[act.pattern]||0) + 1;
38
  });
39
40
  var max = 0, t = "", frequentArr = [];
41
  for (var key in countArr) {
0 ignored issues
show
Complexity introduced by
A for in loop automatically includes the property of any prototype object, consider checking the key using hasOwnProperty.

When iterating over the keys of an object, this includes not only the keys of the object, but also keys contained in the prototype of that object. It is generally a best practice to check for these keys specifically:

var someObject;
for (var key in someObject) {
    if ( ! someObject.hasOwnProperty(key)) {
        continue; // Skip keys from the prototype.
    }

    doSomethingWith(key);
}
Loading history...
42
    for (var key1 in countArr[key]) {
0 ignored issues
show
Complexity introduced by
A for in loop automatically includes the property of any prototype object, consider checking the key using hasOwnProperty.

When iterating over the keys of an object, this includes not only the keys of the object, but also keys contained in the prototype of that object. It is generally a best practice to check for these keys specifically:

var someObject;
for (var key in someObject) {
    if ( ! someObject.hasOwnProperty(key)) {
        continue; // Skip keys from the prototype.
    }

    doSomethingWith(key);
}
Loading history...
43
      if(max <= parseInt(countArr[key][key1])){
44
        max = countArr[key][key1];
45
        t = key1;
46
      } else {
47
        max = max;
48
      }
49
    }
50
    frequentArr.push({"text": t, type: key});
51
    max = 0;
52
    t= "";
53
  }
54
55
  return frequentArr;
56
};
57
58
module.exports = Analysis;