| Conditions | 1 | 
| Paths | 4 | 
| Total Lines | 153 | 
| Lines | 0 | 
| Ratio | 0 % | 
| Changes | 1 | ||
| Bugs | 0 | Features | 0 | 
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | define(['Navigo'], function (Navigo) { | 
            ||
| 4 |   return function (language) { | 
            ||
| 5 | var init = false;  | 
            ||
| 6 |     var objects = { nodes: {}, links: {} }; | 
            ||
| 7 | var targets = [];  | 
            ||
| 8 |     var views = {}; | 
            ||
| 9 |     var current = {}; | 
            ||
| 10 |     var state = { lang: language.getLocale(), view: 'map' }; | 
            ||
| 11 | |||
| 12 |     function resetView() { | 
            ||
| 13 |       targets.forEach(function (t) { | 
            ||
| 14 | t.resetView();  | 
            ||
| 15 | });  | 
            ||
| 16 | }  | 
            ||
| 17 | |||
| 18 |     function gotoNode(d) { | 
            ||
| 19 |       if (d.nodeId in objects.nodes) { | 
            ||
| 20 |         targets.forEach(function (t) { | 
            ||
| 21 | t.gotoNode(objects.nodes[d.nodeId]);  | 
            ||
| 22 | });  | 
            ||
| 23 | }  | 
            ||
| 24 | }  | 
            ||
| 25 | |||
| 26 |     function gotoLink(d) { | 
            ||
| 27 |       if (d.linkId in objects.links) { | 
            ||
| 28 |         targets.forEach(function (t) { | 
            ||
| 29 | t.gotoLink(objects.links[d.linkId]);  | 
            ||
| 30 | });  | 
            ||
| 31 | }  | 
            ||
| 32 | }  | 
            ||
| 33 | |||
| 34 |     function view(d) { | 
            ||
| 35 |       if (d.view in views) { | 
            ||
| 36 | views[d.view]();  | 
            ||
| 37 | state.view = d.view;  | 
            ||
| 38 | resetView();  | 
            ||
| 39 | }  | 
            ||
| 40 | }  | 
            ||
| 41 | |||
| 42 |     function customRoute(lang, viewValue, node, link, zoom, lat, lng) { | 
            ||
| 43 |       current = { | 
            ||
| 44 | lang: lang,  | 
            ||
| 45 | view: viewValue,  | 
            ||
| 46 | node: node,  | 
            ||
| 47 | link: link,  | 
            ||
| 48 | zoom: zoom,  | 
            ||
| 49 | lat: lat,  | 
            ||
| 50 | lng: lng  | 
            ||
| 51 | };  | 
            ||
| 52 | |||
| 53 |       if (lang && lang !== state.lang) { | 
            ||
| 54 | language.setLocale(lang);  | 
            ||
| 55 | }  | 
            ||
| 56 | |||
| 57 |       if (!init || viewValue && viewValue !== state.view) { | 
            ||
| 58 |         if (!viewValue) { | 
            ||
| 59 | viewValue = state.view;  | 
            ||
| 60 | }  | 
            ||
| 61 |         view({ view: viewValue }); | 
            ||
| 62 | init = true;  | 
            ||
| 63 | }  | 
            ||
| 64 | |||
| 65 |       if (node) { | 
            ||
| 66 |         gotoNode({ nodeId: node }); | 
            ||
| 67 |       } else if (link) { | 
            ||
| 68 |         gotoLink({ linkId: link }); | 
            ||
| 69 |       } else if (lat) { | 
            ||
| 70 |         targets.forEach(function (t) { | 
            ||
| 71 |           t.gotoLocation({ | 
            ||
| 72 | zoom: parseInt(zoom, 10),  | 
            ||
| 73 | lat: parseFloat(lat),  | 
            ||
| 74 | lng: parseFloat(lng)  | 
            ||
| 75 | });  | 
            ||
| 76 | });  | 
            ||
| 77 |       } else { | 
            ||
| 78 | resetView();  | 
            ||
| 79 | }  | 
            ||
| 80 | }  | 
            ||
| 81 | |||
| 82 | var root = null;  | 
            ||
| 83 | var useHash = true;  | 
            ||
| 84 | const router = new Navigo(root, useHash);  | 
            ||
| 85 | |||
| 86 | router  | 
            ||
| 87 |       .on(/^\/?#?\/([\w]{2})?\/?(map|graph)?\/?([a-f\d]{12})?([a-f\d\-]{25})?\/?(?:(\d+)\/([\d.]+)\/([\d.]+))?$/, customRoute) | 
            ||
| 88 |       .on({ | 
            ||
| 89 |         '*': function () { | 
            ||
| 90 | router.fullUrl();  | 
            ||
| 91 | }  | 
            ||
| 92 | });  | 
            ||
| 93 | |||
| 94 |     router.generateLink = function generateLink(data, full, deep) { | 
            ||
| 95 | var result = '';  | 
            ||
| 96 | |||
| 97 |       if (full) { | 
            ||
| 98 |         data = Object.assign({}, state, data); | 
            ||
| 99 |       } else if (deep) { | 
            ||
| 100 |         data = Object.assign({}, current, data); | 
            ||
| 101 | }  | 
            ||
| 102 | |||
| 103 |       for (var key in data) { | 
            ||
| 104 |         if (!data.hasOwnProperty(key) || data[key] === undefined) { | 
            ||
| 105 | continue;  | 
            ||
| 106 | }  | 
            ||
| 107 | result += '/' + data[key];  | 
            ||
| 108 | }  | 
            ||
| 109 | |||
| 110 | return useHash ? '#' + result : result;  | 
            ||
| 111 | };  | 
            ||
| 112 | |||
| 113 |     router.fullUrl = function fullUrl(data, e, deep) { | 
            ||
| 114 |       if (e) { | 
            ||
| 115 | e.preventDefault();  | 
            ||
| 116 | }  | 
            ||
| 117 | router.navigate(router.generateLink(data, !deep, deep));  | 
            ||
| 118 | };  | 
            ||
| 119 | |||
| 120 |     router.getLang = function getLang() { | 
            ||
| 121 |       var lang = location.hash.match(/^\/?#\/([\w]{2})\//); | 
            ||
| 122 |       if (lang) { | 
            ||
| 123 | return lang[1];  | 
            ||
| 124 | }  | 
            ||
| 125 | return null;  | 
            ||
| 126 | };  | 
            ||
| 127 | |||
| 128 |     router.addTarget = function addTarget(d) { | 
            ||
| 129 | targets.push(d);  | 
            ||
| 130 | };  | 
            ||
| 131 | |||
| 132 |     router.removeTarget = function removeTarget(d) { | 
            ||
| 133 |       targets = targets.filter(function (e) { | 
            ||
| 134 | return d !== e;  | 
            ||
| 135 | });  | 
            ||
| 136 | };  | 
            ||
| 137 | |||
| 138 |     router.addView = function addView(k, d) { | 
            ||
| 139 | views[k] = d;  | 
            ||
| 140 | };  | 
            ||
| 141 | |||
| 142 |     router.setData = function setData(data) { | 
            ||
| 143 |       objects.nodes = {}; | 
            ||
| 144 |       objects.links = {}; | 
            ||
| 145 | |||
| 146 |       data.nodes.all.forEach(function (d) { | 
            ||
| 147 | objects.nodes[d.nodeinfo.node_id] = d;  | 
            ||
| 148 | });  | 
            ||
| 149 | |||
| 150 |       data.graph.links.forEach(function (d) { | 
            ||
| 151 | objects.links[d.id] = d;  | 
            ||
| 152 | });  | 
            ||
| 153 | };  | 
            ||
| 154 | |||
| 155 | return router;  | 
            ||
| 156 | };  | 
            ||
| 157 | });  | 
            ||
| 158 |