Completed
Push — phpunit ( 7f2080...75f541 )
by Marcos
14:28 queued 10:45
created

js/importers/import-main.js   B

Complexity

Conditions 1
Paths 2

Size

Total Lines 102

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 1
c 2
b 0
f 0
nc 2
nop 3
dl 0
loc 102
rs 8.2857

7 Functions

Rating   Name   Duplication   Size   Complexity  
A PassmanImporter.parseRow_ 0 21 4
A PassmanImporter.toObject_ 0 11 2
A PassmanImporter.htmlDecode 0 5 1
A PassmanImporter.join_ 0 9 3
B PassmanImporter.newCredential 0 25 1
B PassmanImporter.readCsv 0 19 5
A PassmanImporter.readJson 0 3 1

How to fix   Long Method   

Long Method

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:

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