| Conditions | 1 |
| Paths | 2 |
| Total Lines | 102 |
| 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 | var PassmanImporter = {}; |
||
| 2 | (function(window, $, PassmanImporter) { |
||
| 3 | 'use strict'; |
||
| 4 | |||
| 5 | |||
| 6 | PassmanImporter.parseRow_ = function(row) { |
||
| 7 | // Strip leading quote. |
||
| 8 | row = row.trim(); |
||
| 9 | var isQuoted = false; |
||
| 10 | if (row.charAt(0) == '"') { |
||
| 11 | row = row.substring(1); |
||
| 12 | isQuoted = true; |
||
| 13 | } |
||
| 14 | if (row.charAt(row.length - 2) == '"') { |
||
| 15 | row = row.substring(0, row.length - 2); |
||
| 16 | isQuoted = true; |
||
| 17 | } |
||
| 18 | // Strip trailing quote. There seems to be a character between the last quote |
||
| 19 | // and the line ending, hence 2 instead of 1. |
||
| 20 | if(isQuoted === true) { |
||
| 21 | row = row.split('","'); |
||
| 22 | } else { |
||
| 23 | row = row.split(','); |
||
| 24 | } |
||
| 25 | return row; |
||
| 26 | }; |
||
| 27 | PassmanImporter.htmlDecode = function(input){ |
||
| 28 | var e = document.createElement('div'); |
||
| 29 | e.innerHTML = input; |
||
| 30 | return e.childNodes[0].nodeValue; |
||
| 31 | }; |
||
| 32 | PassmanImporter.toObject_ = function(headings, row) { |
||
| 33 | var result = {}; |
||
| 34 | for (var i = 0, ii = row.length; i < ii; i++) { |
||
| 35 | headings[i] = headings[i].replace(',','_') |
||
| 36 | .toLowerCase().replace(' ','_') |
||
| 37 | .replace('(','').replace(')','') |
||
| 38 | .replace('"',''); |
||
| 39 | result[headings[i]] = row[i]; |
||
| 40 | } |
||
| 41 | return result; |
||
| 42 | }; |
||
| 43 | |||
| 44 | PassmanImporter.join_ = function(arr, sep) { |
||
| 45 | var parts = []; |
||
| 46 | for (var i = 0, ii = arr.length; i < ii; i++) { |
||
| 47 | if(arr[i]){ |
||
| 48 | parts.push(arr[i]); |
||
| 49 | } |
||
| 50 | } |
||
| 51 | return parts.join(sep); |
||
| 52 | }; |
||
| 53 | |||
| 54 | PassmanImporter.newCredential = function () { |
||
| 55 | var credential = { |
||
| 56 | 'credential_id': null, |
||
| 57 | 'guid': null, |
||
| 58 | 'vault_id': null, |
||
| 59 | 'label': null, |
||
| 60 | 'description': null, |
||
| 61 | 'created': null, |
||
| 62 | 'changed': null, |
||
| 63 | 'tags': [], |
||
| 64 | 'email': null, |
||
| 65 | 'username': null, |
||
| 66 | 'password': null, |
||
| 67 | 'url': null, |
||
| 68 | 'favicon': null, |
||
| 69 | 'renew_interval': null, |
||
| 70 | 'expire_time': 0, |
||
| 71 | 'delete_time': 0, |
||
| 72 | 'files': [], |
||
| 73 | 'custom_fields': [], |
||
| 74 | 'otp': {}, |
||
| 75 | 'hidden': false |
||
| 76 | }; |
||
| 77 | return credential; |
||
| 78 | }; |
||
| 79 | |||
| 80 | PassmanImporter.readCsv = function( csv, hasHeadings ){ |
||
| 81 | hasHeadings = (hasHeadings === undefined) ? true : hasHeadings; |
||
| 82 | var i, _row; |
||
| 83 | var lines = []; |
||
| 84 | var rows = csv.split('\n'); |
||
| 85 | if(hasHeadings) { |
||
| 86 | var headings = this.parseRow_(rows[0]); |
||
| 87 | for (i = 1; i < rows.length; i++) { |
||
| 88 | _row = this.toObject_(headings, this.parseRow_(rows[i])); |
||
| 89 | lines.push(_row); |
||
| 90 | } |
||
| 91 | } else { |
||
| 92 | for (i = 1; i < rows.length; i++) { |
||
| 93 | _row = this.toObject_(null, this.parseRow_(rows[i])); |
||
| 94 | lines.push(_row); |
||
| 95 | } |
||
| 96 | } |
||
| 97 | return lines; |
||
| 98 | }; |
||
| 99 | |||
| 100 | PassmanImporter.readJson = function (string){ |
||
| 101 | return JSON.parse(string); |
||
| 102 | }; |
||
| 103 | })(window, $, PassmanImporter); |
||
| 104 |