src/assets/js/dynamikaweb.validator.js   A
last analyzed

Complexity

Total Complexity 21
Complexity/F 4.2

Size

Lines of Code 83
Function Count 5

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
wmc 21
eloc 53
mnd 16
bc 16
fnc 5
dl 0
loc 83
rs 10
bpm 3.2
cpm 4.2
noi 5
c 0
b 0
f 0
1
var dynamikaweb = (typeof dynamikaweb == "undefined" || !dynamikaweb)? {} : dynamikaweb;
0 ignored issues
show
Bug introduced by
The variable dynamikaweb seems to be never initialized.
Loading history...
Bug introduced by
The variable dynamikaweb seems to not be initialized for all possible execution paths.
Loading history...
2
3
dynamikaweb.validation = (function($) {
0 ignored issues
show
Unused Code introduced by
The parameter $ is not used and could be removed.

This check looks for parameters in functions that are not used in the function body and are not followed by other parameters which are used inside the function.

Loading history...
4
  var pub = {
5
    isEmpty: function(value) {
6
      return value === null || value === undefined || value == [] || value === '';
7
    },
8
    addMessage: function(messages, message, value) {
9
        messages.push(message.replace(/\{value\}/g, value));
10
    },
11
    isAllCharEquals: function(string) {
12
        var c = string.charAt(0);
13
        for (var i in string) {
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...
14
            if (c != string.charAt(i)) {
15
                return false;
16
            }
17
        }
18
        return true;
19
    },
20
    titulo: function(value, messages, options) {
21
      if (options.skipOnEmpty && pub.isEmpty(value)) {
22
          return;
23
      }
24
25
      var valid = true;
26
      var titulo = value.replace(/[^0-9_]/g, "");
27
      var uf = titulo.substr(-4, 2);
28
      var tam = titulo.length;
29
30
      if((uf < 1 || uf > 28) || (tam < 5 || tam > 13)){
31
        valid = false;
32
      } else {
33
        for (var x = 0; x < 10; x++) {
34
          if (titulo === x.toString().repeat(10)) {
35
            valid = false;
36
          }
37
        }
38
      }
39
40
      if (valid) {
41
        var dv = titulo.substr(titulo.length - 2, 2);
42
        var sequencia = titulo.substr(0, titulo.length - 4);
43
        var base = 2;
44
        for (var i = 0; i < 2; i++) {
45
          var fator = 9;
46
          var soma = 0;
47
48
          for (var j = (sequencia.length -1); j > -1 ; j--) {
49
            soma += sequencia.charAt(j) * fator;
50
            
51
            if (fator == base) {
52
              fator = 10;
53
            }
54
55
            fator--;
56
          }
57
58
          var digito = soma % 11;
59
  
60
          if (digito == 0 && uf < 3){
0 ignored issues
show
Best Practice introduced by
Comparing digito to 0 using the == operator is not safe. Consider using === instead.
Loading history...
61
            digito = 1;
62
          } else if (digito == 10) {
63
            digito = 0;
64
          }
65
66
          if (dv.charAt(i) != digito) {
67
            valid = false;
68
          }
69
70
          switch (i) {
71
            case 0:
72
              sequencia = uf.toString() + digito.toString();
73
              break
74
          } 
75
        }
76
      }
77
      if (!valid) {
78
          pub.addMessage(messages, options.message, value);
79
      }
80
    },
81
  };
82
  return pub;
83
})(jQuery);
84
  
85