-
Notifications
You must be signed in to change notification settings - Fork 5
/
index.js
88 lines (67 loc) · 2.15 KB
/
index.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
/**
* Module dependencies
*/
var isArray = Array.isArray
, keys = Object.keys;
CSV.CHAR_RETURN = 0xd;
CSV.CHAR_NEWLINE = 0xa;
CSV.DELIMITER = 0x2c;
CSV.CHAR_ENCAPSULATE = 0x22;
function head (a) {
return a[0];
}
function tail (a) {
return a[a.length -1];
}
function char (c) {
return 'number' === typeof c
? String.fromCharCode.apply(null, arguments)
: c;
}
function needsEncapsulation (string) {
return !!string && (
string.toString().indexOf(char(CSV.DELIMITER)) >= 0 ||
string.toString().indexOf(char(CSV.CHAR_RETURN)) >= 0 ||
string.toString().indexOf(char(CSV.CHAR_NEWLINE)) >= 0 ||
string.toString().indexOf(char(CSV.CHAR_ENCAPSULATE)) >= 0
);
}
function encapsulate (string) {
var wrapperChar = char(CSV.CHAR_ENCAPSULATE)
, replaceWith = "\\" + char(CSV.CHAR_ENCAPSULATE)
, escapedValue = string.toString().replace(new RegExp(wrapperChar, 'g'), replaceWith);
return wrapperChar + escapedValue + wrapperChar;
}
/**
* Parses an array of objects to a CSV output
*/
try { module.exports = CSV; } catch(e) {}
function CSV (objects, opts) {
if ('object' !== typeof objects) throw new TypeError("expecting an array");
opts = 'object' === typeof opts
? opts
: {};
objects = isArray(objects)
? objects.slice()
: [objects];
if (!objects.length) throw new Error("expecting at least one object");
var headers = keys(head(objects))
, buf = [];
while (objects.length) {
var lbuf = []
, object = objects.shift();
for (var i = 0 ;i < headers.length; ++i) {
var header = headers[i];
if (lbuf.length) lbuf.push(char(CSV.DELIMITER));
object[header] = needsEncapsulation(object[header])
? encapsulate(object[header])
: object[header];
lbuf.push(object[header]);
}
buf.push(lbuf.join(''));
buf.push(char(CSV.CHAR_RETURN, CSV.CHAR_NEWLINE));
}
return false !== opts.headers
? [].concat(headers.join(char(CSV.DELIMITER)), char(CSV.CHAR_NEWLINE)).concat(buf).filter(Boolean).join('')
: buf.filter(Boolean).join('');
}