| Conditions | 1 |
| Paths | 16 |
| Total Lines | 111 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 2 | ||
| 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 | /** |
||
| 24 | (function(window, $, PassmanImporter) { |
||
| 25 | 'use strict'; |
||
| 26 | |||
| 27 | |||
| 28 | PassmanImporter.parseRow_ = function(row) { |
||
| 29 | // Strip leading quote. |
||
| 30 | row = row.trim(); |
||
| 31 | var isQuoted = false; |
||
| 32 | if (row.charAt(0) === '"') { |
||
| 33 | row = row.substring(1); |
||
| 34 | isQuoted = true; |
||
| 35 | } |
||
| 36 | if (row.charAt(row.length - 2) === '"') { |
||
| 37 | row = row.substring(0, row.length - 2); |
||
| 38 | isQuoted = true; |
||
| 39 | } |
||
| 40 | // Strip trailing quote. There seems to be a character between the last quote |
||
| 41 | // and the line ending, hence 2 instead of 1. |
||
| 42 | if(isQuoted === true) { |
||
| 43 | row = row.split('","'); |
||
| 44 | } else { |
||
| 45 | row = row.split(','); |
||
| 46 | } |
||
| 47 | return row; |
||
| 48 | }; |
||
| 49 | PassmanImporter.htmlDecode = function(input){ |
||
| 50 | var e = document.createElement('div'); |
||
| 51 | e.innerHTML = input; |
||
| 52 | return e.childNodes[0].nodeValue; |
||
| 53 | }; |
||
| 54 | PassmanImporter.toObject_ = function(headings, row) { |
||
| 55 | var result = {}; |
||
| 56 | for (var i = 0, ii = row.length; i < ii; i++) { |
||
| 57 | if(headings[i]) { |
||
| 58 | headings[i] = headings[i].replace(',', '_') |
||
| 59 | .toLowerCase().replace(' ', '_') |
||
| 60 | .replace('(', '').replace(')', '') |
||
| 61 | .replace('"', ''); |
||
| 62 | result[headings[i]] = row[i]; |
||
| 63 | } else { |
||
| 64 | result[ii] = row[i]; |
||
| 65 | } |
||
| 66 | } |
||
| 67 | return result; |
||
| 68 | }; |
||
| 69 | |||
| 70 | PassmanImporter.join_ = function(arr, sep) { |
||
| 71 | var parts = []; |
||
| 72 | for (var i = 0, ii = arr.length; i < ii; i++) { |
||
| 73 | if(arr[i]){ |
||
| 74 | parts.push(arr[i]); |
||
| 75 | } |
||
| 76 | } |
||
| 77 | return parts.join(sep); |
||
| 78 | }; |
||
| 79 | |||
| 80 | PassmanImporter.newCredential = function () { |
||
| 81 | var credential = { |
||
| 82 | 'credential_id': null, |
||
| 83 | 'guid': null, |
||
| 84 | 'vault_id': null, |
||
| 85 | 'label': null, |
||
| 86 | 'description': null, |
||
| 87 | 'created': null, |
||
| 88 | 'changed': null, |
||
| 89 | 'tags': [], |
||
| 90 | 'email': null, |
||
| 91 | 'username': null, |
||
| 92 | 'password': null, |
||
| 93 | 'url': null, |
||
| 94 | 'favicon': null, |
||
| 95 | 'renew_interval': null, |
||
| 96 | 'expire_time': 0, |
||
| 97 | 'delete_time': 0, |
||
| 98 | 'files': [], |
||
| 99 | 'custom_fields': [], |
||
| 100 | 'otp': {}, |
||
| 101 | 'hidden': false |
||
| 102 | }; |
||
| 103 | return credential; |
||
| 104 | }; |
||
| 105 | |||
| 106 | /** |
||
| 107 | * Read a csv |
||
| 108 | * @param csv the csv file contents |
||
| 109 | * @param hasHeadings does csv has headings? (default true) |
||
| 110 | */ |
||
| 111 | PassmanImporter.readCsv = function( csv, hasHeadings ){ |
||
| 112 | hasHeadings = (hasHeadings === undefined) ? true : hasHeadings; |
||
| 113 | var lines = []; |
||
| 114 | /** global: Papa */ |
||
| 115 | Papa.parse(csv, { |
||
| 116 | complete: function(results) { |
||
| 117 | if(results.data) { |
||
| 118 | var headings = (hasHeadings) ? results.data[0] : null; |
||
| 119 | var start = (hasHeadings) ? 1 : 0; |
||
| 120 | for(var i = start; i < results.data.length; i++){ |
||
| 121 | var _row = (hasHeadings) ? PassmanImporter.toObject_(headings, results.data[i]) : results.data[i]; |
||
| 122 | lines.push(_row); |
||
| 123 | } |
||
| 124 | } |
||
| 125 | } |
||
| 126 | }); |
||
| 127 | |||
| 128 | return lines; |
||
| 129 | }; |
||
| 130 | |||
| 131 | PassmanImporter.readJson = function (string){ |
||
| 132 | return JSON.parse(string); |
||
| 133 | }; |
||
| 134 | })(window, $, PassmanImporter); |
||
| 135 |