-
Notifications
You must be signed in to change notification settings - Fork 0
/
csvjson.js
161 lines (131 loc) · 4.53 KB
/
csvjson.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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
/**
* csvjson.js - A script to convert between CSV and JSON formats
* Author: Aaron Snoswell (@aaronsnoswell, elucidatedbianry.com)
*/
// Namespace
csvjson = {};
// Hide from global scope
(function(){
function isdef(ob) {
if(typeof(ob) == "undefined") return false;
return true;
}
/**
* splitCSV function (c) 2009 Brian Huisman, see http://www.greywyvern.com/?post=258
* Works by spliting on seperators first, then patching together quoted values
*/
function splitCSV(str, sep) {
for (var foo = str.split(sep = sep || ","), x = foo.length - 1, tl; x >= 0; x--) {
if (foo[x].replace(/"\s+$/, '"').charAt(foo[x].length - 1) == '"') {
if ((tl = foo[x].replace(/^\s+"/, '"')).length > 1 && tl.charAt(0) == '"') {
foo[x] = foo[x].replace(/^\s*"|"\s*$/g, '').replace(/""/g, '"');
} else if (x) {
foo.splice(x - 1, 2, [foo[x - 1], foo[x]].join(sep));
} else foo = foo.shift().split(sep).concat(foo);
} else foo[x].replace(/""/g, '"');
} return foo;
};
/**
* Converts from CSV formatted data (as a string) to JSON returning
* an object.
* @required csvdata {string} The CSV data, formatted as a string.
* @param args.delim {string} The delimiter used to seperate CSV
* items. Defauts to ','.
* @param args.textdelim {string} The delimiter used to wrap text in
* the CSV data. Defaults to nothing (an empty string).
*/
csvjson.csv2json = function(csvdata, args) {
args = args || {};
var delim = isdef(args.delim) ? args.delim : ",";
// Unused
//var textdelim = isdef(args.textdelim) ? args.textdelim : "";
var csvlines = csvdata.split("\n");
var csvheaders = splitCSV(csvlines[0], delim);
var csvrows = csvlines.slice(1, csvlines.length);
var ret = {};
ret.headers = csvheaders;
ret.rows = [];
for(var r in csvrows) {
if (csvrows.hasOwnProperty(r)) {
var row = csvrows[r];
var rowitems = splitCSV(row, delim);
// Break if we're at the end of the file
if(row.length == 0) break;
var rowob = {};
for(var i in rowitems) {
if (rowitems.hasOwnProperty(i)) {
var item = rowitems[i];
// Try to (intelligently) cast the item to a number, if applicable
if(!isNaN(item*1)) {
item = item*1;
}
rowob[csvheaders[i]] = item;
}
}
ret.rows.push(rowob);
}
}
return ret;
} // end csv2json
/**
* Taken an object of the form
* {
* headers: ["Heading 1", "Header 2", ...]
* rows: [
* {"Heading 1": SomeValue, "Heading 2": SomeOtherValue},
* {"Heading 1": SomeValue, "Heading 2": SomeOtherValue},
* ...
* ]
* }
* and returns a CSV representation as a string.
* @requires jsondata {object} The JSON object to parse.
* @param args.delim {string} The delimiter used to seperate CSV
* items. Defauts to ','.
* @param args.textdelim {string} The delimiter used to wrap text in
* the CSV data. Defaults to nothing (an empty string).
*/
csvjson.json2csv = function(jsondata, args) {
args = args || {};
var delim = isdef(args.delim) ? args.delim : ",";
var textdelim = isdef(args.textdelim) ? args.textdelim : "";
if(typeof(jsondata) == "string") {
// JSON text parsing is not implemented (yet)
return null;
}
var ret = "";
// Add the headers
for(var h in jsondata.headers) {
if (jsondata.headers.hasOwnProperty(h)) {
var heading = jsondata.headers[h];
ret += textdelim + heading + textdelim + delim;
}
}
// Remove trailing delimiter
ret = ret.slice(0, ret.length-1);
ret += "\n";
// Add the items
for(var r in jsondata.rows) {
if (jsondata.rows.hasOwnProperty(r)) {
var row = jsondata.rows[r];
// Only add elements that are mentioned in the headers (in order, obviously)
for(var h in jsondata.headers) {
if (jsondata.headers.hasOwnProperty(h)) {
var heading = jsondata.headers[h];
var data = row[heading];
if(typeof(data) == "string") {
ret += textdelim + row[heading] + textdelim + delim;
} else {
ret += row[heading] + delim;
}
}
}
// Remove trailing delimiter
ret = ret.slice(0, ret.length-1);
ret += "\n";
}
}
// Remove trailing newling
ret = ret.slice(0, ret.length-1);
return ret;
}
})(); // Execute hidden-scope code