forked from atornblad/csv
-
Notifications
You must be signed in to change notification settings - Fork 0
/
csv.js
118 lines (97 loc) · 3 KB
/
csv.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
(function(context) {
/**
* Creates a new Csv exporter object
*
* @constructor
* @this {Csv}
* @param {String[]} propertyOrder The order of the object properties to export
*/
function Csv(propertyOrder) {
this.propertyOrder = propertyOrder;
this.items = [];
}
/**
* Creates a new Csv exporter object from an array of items and, optionally, the order of the object properties
*
* @expose
* @param {Object[]} items The items to export
* @param {String[]} [optionalOrderOfProperties] The order of the object properties to export
* @return {Csv} The new Csv object
*/
Csv.autoCreate = function(items, optionalOrderOfProperties) {
if (items && Array.isArray(items) && items.length > 0) {
var properties = optionalOrderOfProperties || items[0].getOwnPropertyNames();
var result = new Csv(properties);
items.forEach(function(item) {
result.add(item);
});
return result;
} else {
return null;
}
};
/**
* @private
*/
function createTextLine(values, separator) {
var result = [];
values.forEach(function(value) {
var text = value.toString();
if (text.indexOf(separator) == -1 && text.indexOf('"') == -1) {
result.push(text);
} else {
result.push('"' + text.replace(/"/g, '""') + '"');
}
});
return result.join(separator);
}
/**
* Adds a new item to the exporter
*
* @this {Csv}
* @expose
* @param {Object} item The item to add
*/
Csv.prototype.add = function(item) {
this.items.push(item);
};
/**
* Creates and returns the CSV file contents
*
* @this {Csv}
* @param {String} [separator=","] The separator to use (default: comma)
* @param {Boolean} [includePropertyNames] Pass true to automatically use property names as column headers (default: false)
*/
Csv.prototype.getFileContents = function(separator, includePropertyNames) {
separator = separator || ",";
var textLines = [];
if (includePropertyNames) {
textLines.push(createTextLine(this.propertyOrder, separator));
}
this.items.forEach((function(item) {
var values = [];
this.propertyOrder.forEach(function(propertyName) {
values.push(item[propertyName]);
});
textLines.push(createTextLine(values, separator));
}).bind(this));
return textLines.join("\r\n");
};
/**
* Saves a CSV file to local disk
*
* @this {Csv}
* @expose
* @param {String} filename The name of the file to create
* @param {String} [separator=","] The separator to use (default: comma)
* @param {Boolean} [includePropertyNames=false] Pass true to automatically use property names as column headers (default: false)
*/
Csv.prototype.saveAs = function(filename, separator, includePropertyNames) {
var fullFileContents = this.getFileContents(separator, includePropertyNames);
// var bom = new Uint8Array([0xef, 0xbb, 0xbf]);
// var fileAsBlob = new Blob([bom, fullFileContents], {type:'text/csv'});
var fileAsBlob = new Blob(["\ufeff", fullFileContents], {type:'text/csv'});
context.saveAs(fileAsBlob, filename);
};
context["Csv"] = Csv;
})(window);