| Conditions | 1 |
| Paths | 2 |
| Total Lines | 96 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 1 | Features | 1 |
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 | /** |
||
| 26 | (function(window, $, PassmanImporter) { |
||
| 27 | 'use strict'; |
||
| 28 | // Define the importer |
||
| 29 | PassmanImporter.EnPassTXT = { |
||
| 30 | info: { |
||
| 31 | name: 'EnPass text file', |
||
| 32 | id: 'EnPassTXT', |
||
| 33 | exportSteps: ['Access your Enpass Database. Select "File" > "Export" > "As Text"'] |
||
| 34 | } |
||
| 35 | }; |
||
| 36 | |||
| 37 | function parseEnpass(fileData){ |
||
| 38 | var lastProperty, matches, loginBlocks, property; |
||
| 39 | loginBlocks = fileData.replaceAll("Title :","<~passman~>\nTitle :").split('<~passman~>\n').clean(""); |
||
| 40 | var regex = /(.*) : (.*)/; |
||
| 41 | var results = []; |
||
| 42 | for(var l = 0; l < loginBlocks.length; l++){ |
||
| 43 | var loginBlock = loginBlocks[l]; |
||
| 44 | var lrow = loginBlock.split('\n'); |
||
| 45 | var result = {}; |
||
| 46 | for(var r = 0; r < lrow.length; r++){ |
||
| 47 | var row = lrow[r]; |
||
| 48 | matches = regex.exec(row); |
||
| 49 | if(matches){ |
||
| 50 | property = matches[1]; |
||
| 51 | result[property] = matches[2]; |
||
| 52 | } else { |
||
| 53 | if(lastProperty){ |
||
| 54 | result[lastProperty] += "\n" + row; |
||
| 55 | } |
||
| 56 | } |
||
| 57 | if(property) { |
||
| 58 | lastProperty = property; |
||
| 59 | } |
||
| 60 | } |
||
| 61 | results.push(result) |
||
|
|
|||
| 62 | } |
||
| 63 | return results |
||
| 64 | } |
||
| 65 | |||
| 66 | PassmanImporter.EnPassTXT.readFile = function (file_data) { |
||
| 67 | var mapper = { |
||
| 68 | 'Title': 'label', |
||
| 69 | 'Username': 'username', |
||
| 70 | 'Password': 'password', |
||
| 71 | 'Email': 'email', |
||
| 72 | 'Url': 'url', |
||
| 73 | 'Note': 'description' |
||
| 74 | }; |
||
| 75 | |||
| 76 | var secret_fields = ['cvc', 'pin', 'security answer']; |
||
| 77 | |||
| 78 | /** global: C_Promise */ |
||
| 79 | return new C_Promise(function(){ |
||
| 80 | var credential_list = []; |
||
| 81 | var credentials = parseEnpass(file_data); |
||
| 82 | for (var i = 0; i < credentials.length; i++) { |
||
| 83 | var enpass_credential = credentials[i]; |
||
| 84 | var new_credential = PassmanImporter.newCredential(); |
||
| 85 | for(var key in enpass_credential){ |
||
| 86 | if(!enpass_credential.hasOwnProperty(key)){ |
||
| 87 | continue; |
||
| 88 | } |
||
| 89 | |||
| 90 | if(mapper.hasOwnProperty(key)){ |
||
| 91 | var prop = mapper[key]; |
||
| 92 | new_credential[prop] = enpass_credential[key]; |
||
| 93 | } else { |
||
| 94 | if(key !== 'TOTP') { |
||
| 95 | var isSecret = (secret_fields.indexOf(key.toLowerCase()) !== -1) ? 1 : 0; |
||
| 96 | new_credential.custom_fields.push({ |
||
| 97 | 'label': key, |
||
| 98 | 'value': enpass_credential[key], |
||
| 99 | 'secret': isSecret |
||
| 100 | }) |
||
| 101 | } |
||
| 102 | } |
||
| 103 | } |
||
| 104 | |||
| 105 | if(enpass_credential.hasOwnProperty('TOTP')){ |
||
| 106 | new_credential.otp.secret = enpass_credential['TOTP']; |
||
| 107 | } |
||
| 108 | |||
| 109 | var progress = { |
||
| 110 | percent: i/credentials.length*100, |
||
| 111 | loaded: i, |
||
| 112 | total: credentials.length |
||
| 113 | }; |
||
| 114 | |||
| 115 | credential_list.push(new_credential); |
||
| 116 | this.call_progress(progress); |
||
| 117 | } |
||
| 118 | this.call_then(credential_list); |
||
| 119 | }); |
||
| 120 | }; |
||
| 121 | })(window, $, PassmanImporter); |
Requirement of semicolons purely is a coding style issue since JavaScript has specific rules about semicolons which are followed by all browsers.
Further Readings: