code complexity & repetition analysis tool
1// Example of high cyclomatic complexity
2function processUserData(user, options) {
3 if (!user) {
4 throw new Error("User required");
5 }
6
7 if (user.age < 18 && !options.allowMinors) {
8 return { error: "User too young" };
9 }
10
11 if (user.country === "US" || user.country === "CA") {
12 if (user.state && user.state.length === 2) {
13 console.log("North American user");
14 }
15 } else if (user.country === "UK" || user.country === "IE") {
16 console.log("European user");
17 }
18
19 const result = {};
20
21 if (options.includeEmail && user.email) {
22 result.email = user.email.toLowerCase();
23 }
24
25 if (options.includePhone && user.phone) {
26 result.phone = user.phone.replace(/\D/g, '');
27 }
28
29 if (user.premium || (user.credits && user.credits > 100)) {
30 result.tier = "premium";
31 } else if (user.credits && user.credits > 10) {
32 result.tier = "standard";
33 } else {
34 result.tier = "basic";
35 }
36
37 for (let i = 0; i < user.preferences.length; i++) {
38 const pref = user.preferences[i];
39 if (pref.enabled && pref.value !== null) {
40 result.preferences = result.preferences || [];
41 result.preferences.push(pref);
42 }
43 }
44
45 return result;
46}
47
48// Cyclomatic complexity: ~15-20