Completed
Pull Request — master (#70)
by Sander
03:14
created

PassmanExporter.csv.export   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 34

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 1
c 2
b 0
f 0
nc 1
nop 1
dl 0
loc 34
rs 8.8571

1 Function

Rating   Name   Duplication   Size   Complexity  
B 0 31 5
1
/** global: PassmanExporter */
2
PassmanExporter.csv = {
3
	info: {
4
		name: 'CSV',
5
		id: 'csv',
6
		description: 'Export credentials as csv.'
7
	}
8
};
9
10
PassmanExporter.csv.export = function (credentials) {
11
	/** global: C_Promise */
12
	return new C_Promise(function () {
13
		var _this = this;
14
		var headers = ['label', 'username', 'password', 'email', 'description', 'tags'];
15
		var file_data = '"' + headers.join('","') + '"\n';
16
		for (var i = 0; i < credentials.length; i++) {
17
			var _credential = credentials[i];
18
			var row_data = [];
19
			for (var h = 0; h < headers.length; h++) {
20
				var field = headers[h];
21
				if (field === 'tags') {
22
					var _tags = [];
23
					for (var t = 0; t < _credential[field].length; t++) {
24
						_tags.push(_credential[field][t].text);
25
					}
26
					var data = '[' + _tags.join(",") + ']';
27
					row_data.push('"' + data + '"');
28
				} else {
29
					row_data.push('"' + _credential[field] + '"');
30
				}
31
			}
32
			var progress = {
33
				percent: i / credentials.length * 100,
34
				loaded: i,
35
				total: credentials.length
36
			};
37
			_this.call_progress(progress);
38
			file_data += row_data.join(',') + "\n";
39
		}
40
		_this.call_then();
41
		download(file_data, 'passman-export.csv');
42
	});
43
};
44