Skip to content
This repository was archived by the owner on Oct 30, 2024. It is now read-only.

Commit 765e32c

Browse files
Merge pull request roofstock#18 from Neuromobile/feature-17/separate-json2csv
Separate JSON2CSV function from export method at csv service
2 parents 8e0d399 + 36c0441 commit 765e32c

File tree

3 files changed

+81
-55
lines changed

3 files changed

+81
-55
lines changed

addon/services/csv.js

Lines changed: 52 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -10,79 +10,76 @@ const defaultConfig = {
1010
export default Ember.Service.extend({
1111

1212
export: function (data, options) {
13-
1413
options = optionize(options, defaultConfig);
1514

16-
function JSON2CSV(objArray) {
17-
var array = typeof objArray != 'object' ? JSON.parse(objArray) : objArray;
15+
var csv = this.jsonToCsv(data, options);
1816

19-
var str = '';
20-
var line = '';
17+
saveAs(new Blob([csv],{type:"data:text/csv;charset=utf-8"}), options.fileName);
18+
},
2119

22-
if (options.withSeparator) {
23-
// add separator identifier;
24-
str += `sep=${options.separator}\r\n`;
25-
}
20+
jsonToCsv(objArray, options) {
21+
var array = typeof objArray != 'object' ? JSON.parse(objArray) : objArray;
2622

27-
// add heading row
28-
var head = array[0];
29-
for (var i = 0; i < head.length; i++) {
30-
var value = head[i] + "";
31-
if (i > 0) {
32-
line += options.separator;
33-
}
34-
line += '"' + value.replace(/"/g, '""') + '"';
35-
}
23+
var str = '';
24+
var line = '';
3625

37-
str += line + '\r\n';
26+
if (options.withSeparator) {
27+
// add separator identifier;
28+
str += `sep=${options.separator}\r\n`;
29+
}
3830

39-
// add items
40-
for (var i = 1; i < array.length; i++) {
41-
var line = '';
31+
// add heading row
32+
var head = array[0];
33+
for (var i = 0; i < head.length; i++) {
34+
var value = head[i] + "";
35+
if (i > 0) {
36+
line += options.separator;
37+
}
38+
line += '"' + value.replace(/"/g, '""') + '"';
39+
}
4240

43-
for (var index = 0; index < array[i].length; index++) {
44-
var value = array[i][index];
41+
str += line + '\r\n';
4542

46-
if (index > 0) {
47-
line += options.separator;
48-
}
49-
if (typeof value === 'object') {
50-
if (value) {
51-
var resolveValue;
52-
if (value._d instanceof Date) {
53-
// dealing with encoding issue in IE browsers.
54-
resolveValue = (value._d.getMonth() + 1) + '/' + value._d.getDate() + '/' + value._d.getFullYear();
55-
}
56-
else {
57-
resolveValue = value._d.toString();
58-
}
43+
// add items
44+
for (var i = 1; i < array.length; i++) {
45+
var line = '';
46+
47+
for (var index = 0; index < array[i].length; index++) {
48+
var value = array[i][index];
5949

60-
line += '"' + resolveValue.replace(/"/g, '""') + '"';
50+
if (index > 0) {
51+
line += options.separator;
52+
}
53+
if (typeof value === 'object') {
54+
if (value) {
55+
var resolveValue;
56+
if (value._d instanceof Date) {
57+
// dealing with encoding issue in IE browsers.
58+
resolveValue = (value._d.getMonth() + 1) + '/' + value._d.getDate() + '/' + value._d.getFullYear();
6159
}
6260
else {
63-
line += '""';
61+
resolveValue = value._d.toString();
6462
}
63+
64+
line += '"' + resolveValue.replace(/"/g, '""') + '"';
6565
}
6666
else {
67-
value = value + "";
68-
if (value && value != 'undefined') {
69-
line += '"' + value.replace(/"/g, '""') + '"';
70-
}
71-
else {
72-
line += '""';
73-
}
67+
line += '""';
68+
}
69+
}
70+
else {
71+
value = value + "";
72+
if (value && value != 'undefined') {
73+
line += '"' + value.replace(/"/g, '""') + '"';
74+
}
75+
else {
76+
line += '""';
7477
}
7578
}
76-
77-
str += line + '\r\n';
7879
}
79-
return str;
80-
}
81-
82-
var csv = JSON2CSV(data);
83-
84-
saveAs(new Blob([csv],{type:"data:text/csv;charset=utf-8"}), options.fileName);
8580

81+
str += line + '\r\n';
82+
}
83+
return str;
8684
}
87-
8885
});

tests/index.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
<script src="assets/dummy.js"></script>
2727
<script src="testem.js"></script>
2828
<script src="assets/test-loader.js"></script>
29+
<script src="assets/tests.js"></script>
2930

3031
{{content-for 'body-footer'}}
3132
{{content-for 'test-body-footer'}}

tests/unit/services/csv-test.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import { moduleFor, test } from 'ember-qunit';
2+
3+
moduleFor('service:csv', 'Unit | Service | csv', {
4+
// Specify the other units that are required for this test.
5+
// needs: ['service:foo']
6+
});
7+
8+
// Replace this with your real tests.
9+
test('#jsonToCsv - with valid Array of Arrays, it should return an string with CSV format', function(assert) {
10+
const service = this.subject();
11+
const array = [
12+
[
13+
'name',
14+
'last_name',
15+
],
16+
[
17+
'Dale',
18+
'Cooper'
19+
],
20+
];
21+
const expectedCSV = '\"name\",\"last_name\"\r\n\"Dale\",\"Cooper\"\r\n';
22+
const options = {
23+
separator: ',',
24+
withSeparator: false,
25+
};
26+
27+
assert.deepEqual(service.jsonToCsv(array, options), expectedCSV);
28+
});

0 commit comments

Comments
 (0)