Total Complexity | 31 |
Complexity/F | 2.38 |
Lines of Code | 122 |
Function Count | 13 |
Duplicated Lines | 49 |
Ratio | 40.16 % |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
1 | /** global: UB */ |
||
6 | var arrayFuncs = { |
||
7 | |||
8 | |||
9 | View Code Duplication | isEqual: function(list2){ |
|
|
|||
10 | var list = this; |
||
11 | if (list == null || list2 == null) { |
||
2 ignored issues
–
show
|
|||
12 | return list == list2; |
||
13 | } |
||
14 | if(list.length != list2.length){ |
||
15 | return false; |
||
16 | } |
||
17 | |||
18 | var len = list.length; |
||
19 | |||
20 | for (var i = 0;i<len;i++){ |
||
21 | if(list[i] !== list2[i]){ |
||
22 | return false; |
||
23 | } |
||
24 | } |
||
25 | |||
26 | return true; |
||
27 | }, |
||
28 | isNotEqual: function(list2){ |
||
29 | var list = this; |
||
30 | return !IsEqual(list, list2); |
||
31 | }, |
||
32 | |||
33 | exists: function(){ |
||
34 | var list = this; |
||
35 | return list != null && list.length > 0; |
||
1 ignored issue
–
show
|
|||
36 | }, |
||
37 | isFirst: function(value){ |
||
38 | var list = this; |
||
39 | return list != null && list[0] == value; |
||
1 ignored issue
–
show
|
|||
40 | }, |
||
41 | isLast: function(value){ |
||
42 | var list = this; |
||
43 | return list != null && list[list.length - 1] == value; |
||
1 ignored issue
–
show
|
|||
44 | }, |
||
45 | |||
46 | |||
47 | beginsWithArray: function(value){ |
||
48 | return this.startsWithArray(value); |
||
49 | }, |
||
50 | startsWithArray: function(check){ |
||
51 | var list = this; |
||
52 | |||
53 | // quickly test if length sufficient |
||
54 | var clen = check.length; |
||
55 | var mlen = list.length; |
||
56 | if (mlen < clen) { |
||
57 | return false; |
||
58 | } |
||
59 | |||
60 | // check if first slots match |
||
61 | for (var b = 0;b<clen;b++){ |
||
62 | if (list[b] != check[b]) { |
||
63 | return false; |
||
64 | } |
||
65 | } |
||
66 | return true; |
||
67 | }, |
||
68 | View Code Duplication | endsWithArray: function(check){ |
|
69 | var list = this; |
||
70 | |||
71 | // quickly test if length sufficient |
||
72 | var clen = check.length; |
||
73 | var mlen = list.length; |
||
74 | if (mlen < clen) { |
||
75 | return false; |
||
76 | } |
||
77 | |||
78 | // check if last slots match |
||
79 | var off = (mlen - clen); |
||
80 | for (var b = 0;b<clen;b++){ |
||
81 | if (list[off + b] != check[b]) { |
||
82 | return false; |
||
83 | } |
||
84 | } |
||
85 | return true; |
||
86 | }, |
||
87 | beginsWith: function(value){ |
||
88 | return this.startsWith(value); |
||
89 | }, |
||
90 | startsWith: function(value){ |
||
91 | var list = this; |
||
92 | return list[0] == value; |
||
93 | }, |
||
94 | endsWith: function(value){ |
||
95 | var list = this; |
||
96 | return list[list.length - 1] == value; |
||
97 | }, |
||
98 | |||
99 | View Code Duplication | next: function(obj, wrap = false){ |
|
100 | var list = this; |
||
101 | if (list == null) { |
||
1 ignored issue
–
show
|
|||
102 | return null; |
||
103 | } |
||
104 | var i = list.indexOf(obj); |
||
105 | if (i > -1) { |
||
106 | return (wrap && i >= (list.length - 1)) ? list[0] : list[i + 1]; |
||
107 | } |
||
108 | return null; |
||
109 | }, |
||
110 | prev: function(obj, wrap = false){ |
||
111 | var list = this; |
||
112 | if (list == null) { |
||
1 ignored issue
–
show
|
|||
113 | return null; |
||
114 | } |
||
115 | var i = list.indexOf(obj); |
||
116 | if (i > 0) { |
||
117 | return wrap ? list[list.length - 1] : list[i - 1]; |
||
118 | } |
||
119 | return null; |
||
120 | }, |
||
121 | |||
122 | |||
123 | none:null |
||
124 | }; |
||
125 | |||
126 | // register funcs |
||
127 | UB.registerFuncs(Array.prototype, arrayFuncs); |
||
128 |