node_modules/dns-packet/types.js   F
last analyzed

Complexity

Total Complexity 91
Complexity/F 45.5

Size

Lines of Code 100
Function Count 2

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 0
eloc 95
nc 1
dl 0
loc 100
rs 2
c 0
b 0
f 0
wmc 91
mnd 1
bc 2
fnc 2
bpm 1
cpm 45.5
noi 0

1 Function

Rating   Name   Duplication   Size   Complexity  
F exports.toType 0 50 46

How to fix   Complexity   

Complexity

Complex classes like node_modules/dns-packet/types.js often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

1
exports.toString = function (type) {
2
  switch (type) {
3
    case 1: return 'A'
4
    case 10: return 'NULL'
5
    case 28: return 'AAAA'
6
    case 18: return 'AFSDB'
7
    case 42: return 'APL'
8
    case 257: return 'CAA'
9
    case 60: return 'CDNSKEY'
10
    case 59: return 'CDS'
11
    case 37: return 'CERT'
12
    case 5: return 'CNAME'
13
    case 49: return 'DHCID'
14
    case 32769: return 'DLV'
15
    case 39: return 'DNAME'
16
    case 48: return 'DNSKEY'
17
    case 43: return 'DS'
18
    case 55: return 'HIP'
19
    case 13: return 'HINFO'
20
    case 45: return 'IPSECKEY'
21
    case 25: return 'KEY'
22
    case 36: return 'KX'
23
    case 29: return 'LOC'
24
    case 15: return 'MX'
25
    case 35: return 'NAPTR'
26
    case 2: return 'NS'
27
    case 47: return 'NSEC'
28
    case 50: return 'NSEC3'
29
    case 51: return 'NSEC3PARAM'
30
    case 12: return 'PTR'
31
    case 46: return 'RRSIG'
32
    case 17: return 'RP'
33
    case 24: return 'SIG'
34
    case 6: return 'SOA'
35
    case 99: return 'SPF'
36
    case 33: return 'SRV'
37
    case 44: return 'SSHFP'
38
    case 32768: return 'TA'
39
    case 249: return 'TKEY'
40
    case 52: return 'TLSA'
41
    case 250: return 'TSIG'
42
    case 16: return 'TXT'
43
    case 252: return 'AXFR'
44
    case 251: return 'IXFR'
45
    case 41: return 'OPT'
46
    case 255: return 'ANY'
47
  }
48
  return 'UNKNOWN_' + type
49
}
50
51
exports.toType = function (name) {
52
  switch (name.toUpperCase()) {
53
    case 'A': return 1
54
    case 'NULL': return 10
55
    case 'AAAA': return 28
56
    case 'AFSDB': return 18
57
    case 'APL': return 42
58
    case 'CAA': return 257
59
    case 'CDNSKEY': return 60
60
    case 'CDS': return 59
61
    case 'CERT': return 37
62
    case 'CNAME': return 5
63
    case 'DHCID': return 49
64
    case 'DLV': return 32769
65
    case 'DNAME': return 39
66
    case 'DNSKEY': return 48
67
    case 'DS': return 43
68
    case 'HIP': return 55
69
    case 'HINFO': return 13
70
    case 'IPSECKEY': return 45
71
    case 'KEY': return 25
72
    case 'KX': return 36
73
    case 'LOC': return 29
74
    case 'MX': return 15
75
    case 'NAPTR': return 35
76
    case 'NS': return 2
77
    case 'NSEC': return 47
78
    case 'NSEC3': return 50
79
    case 'NSEC3PARAM': return 51
80
    case 'PTR': return 12
81
    case 'RRSIG': return 46
82
    case 'RP': return 17
83
    case 'SIG': return 24
84
    case 'SOA': return 6
85
    case 'SPF': return 99
86
    case 'SRV': return 33
87
    case 'SSHFP': return 44
88
    case 'TA': return 32768
89
    case 'TKEY': return 249
90
    case 'TLSA': return 52
91
    case 'TSIG': return 250
92
    case 'TXT': return 16
93
    case 'AXFR': return 252
94
    case 'IXFR': return 251
95
    case 'OPT': return 41
96
    case 'ANY': return 255
97
    case '*': return 255
98
  }
99
  return 0
100
}
101