Total Complexity | 12 |
Complexity/F | 4 |
Lines of Code | 45 |
Function Count | 3 |
Duplicated Lines | 4 |
Ratio | 8.89 % |
Changes | 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 | var isDate = function(value) { |
||
2 | if (value == "") return false; |
||
3 | var rxDatePattern = /^(\d{1,2})(\.)(\d{1,2})(\.)(\d{4})$/; //Declare Regex |
||
4 | var dtArray = value.match(rxDatePattern); // is format OK? |
||
5 | if (dtArray == null) return false; |
||
|
|||
6 | //Checks for mm/dd/yyyy format. |
||
7 | var dtMonth = dtArray[3]; |
||
8 | var dtDay = dtArray[1]; |
||
9 | var dtYear = dtArray[5]; |
||
10 | if (dtMonth < 1 || dtMonth > 12) { |
||
11 | return false; |
||
12 | } else if (dtDay < 1 || dtDay > 31) { |
||
13 | return false; |
||
14 | } else if ((dtMonth == 4 || dtMonth == 6 || dtMonth == 9 || dtMonth == 11) && dtDay == 31) { |
||
15 | return false; |
||
16 | View Code Duplication | } else if (dtMonth == 2) { |
|
17 | var isleap = (dtYear % 4 == 0 && (dtYear % 100 != 0 || dtYear % 400 == 0)); |
||
18 | if (dtDay > 29 || (dtDay == 29 && !isleap)) return false; |
||
19 | } |
||
20 | return true; |
||
21 | } |
||
22 | |||
23 | var dateValidator = { |
||
24 | type: 'DATE', |
||
25 | validate(value, validationExpression = null) { |
||
26 | return !value || value.length == 0 || isDate(value); |
||
27 | } |
||
28 | } |
||
29 | |||
30 | var regexpValidator = { |
||
31 | type: 'REGEXP', |
||
32 | validate(value, validationExpression = null) { |
||
33 | if (value && value.length > 0 && validationExpression && validationExpression.length > 0) { |
||
34 | try { |
||
35 | let regexp = new RegExp(validationExpression); |
||
36 | let res = value.match(regexp); |
||
37 | return res && res.length == 1 && res[0] == value; |
||
38 | } catch (err) { |
||
39 | return false; |
||
40 | } |
||
41 | } else { |
||
42 | return true; |
||
43 | } |
||
44 | } |
||
45 | } |
||
46 |