Skip to content

Commit 4806c8b

Browse files
committed
adding tests, all branches should be handled. need to clean up the code a bit and comment it a bit better before going ahead and merging this into master
1 parent 625b1fc commit 4806c8b

File tree

5 files changed

+332
-19
lines changed

5 files changed

+332
-19
lines changed

csv2json-test.js

+8-3
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,19 @@
11
var converter = require('./lib/converter');
22

33
var mycsv = 'header1,header2,"header3"\n'
4-
+ 'row1 column1,"row1 column2, with comma","row1 column3"\n'
4+
+ '"row1 column1",row1 column2,row1 column3\n'
55
+ '"row2 column1","row2 column2","row2 column3"\n';
66

77
var csv = 'header1,header2,header3\n'
8-
+ 'row1 column1,"row1 column2, with comma", row1 column3\n'
8+
+ ',"row1 column2, with comma", row1 column3\n'
99
+ 'row2 column1,row2 column2, row2 column3\n';
1010

11-
converter.csv2json(csv, function (err, json) {
11+
var csvArrays = '"info.name","coursesTaken","year"\n'
12+
+ '"Mike","[CS2500/CS2510]","Sophomore"\n'
13+
+ '"John","[ANTH1101/POL2312/MATH2142/POL3305/LAW2100]","Senior"\n'
14+
+ '"Joe","[]","Freshman"\n';
15+
16+
converter.csv2json(mycsv, function (err, json) {
1217
if (!err) {
1318
return console.log(json);
1419
}

lib/csv-2-json.js

+26-14
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ var splitLine = function (line) {
130130
character,
131131
charBefore,
132132
charAfter,
133-
lastLineIndex = line.length - 1,
133+
lastCharacterIndex = line.length - 1,
134134
stateVariables = {
135135
insideWrapDelimiter: false,
136136
parsingValue: true,
@@ -141,26 +141,35 @@ var splitLine = function (line) {
141141
while(index < line.length) {
142142
character = line[index];
143143
charBefore = index ? line[index - 1] : '';
144-
charAfter = index < lastLineIndex ? line[index + 1] : '';
144+
charAfter = index < lastCharacterIndex ? line[index + 1] : '';
145145

146-
console.log(charBefore, character, charAfter);
147146
// If we reached the end of the line and are in the wrap delimiter, substring up to, but not including this char
148-
if (index === lastLineIndex && stateVariables.insideWrapDelimiter) {
149-
console.log('CASE 1');
147+
if (index === lastCharacterIndex && stateVariables.insideWrapDelimiter) {
150148
stateVariables.parsingValue = false;
151149
splitLine.push(line.substring(stateVariables.startIndex, index));
152150
index++;
153151
}
154152
// If we reached the end of the line and are not in the wrap delimiter
155-
else if (index === lastLineIndex) {
156-
console.log('CASE 2');
153+
else if (index === lastCharacterIndex) {
157154
stateVariables.parsingValue = false;
158155
splitLine.push(line.substring(stateVariables.startIndex));
159156
index++;
160157
}
158+
// If the line starts with a wrap delimiter
159+
else if (character === options.DELIMITER.WRAP && index === 0) {
160+
stateVariables.startIndex = index + 1;
161+
stateVariables.insideWrapDelimiter = true;
162+
stateVariables.parsingValue = true;
163+
index++;
164+
}
165+
// If the first field is empty
166+
else if (character === options.DELIMITER.FIELD && index == 0) {
167+
splitLine.push('');
168+
stateVariables.parsingValue = false;
169+
index++;
170+
}
161171
// If we reached a wrap delimiter with a field delimiter after it (ie. *",)
162172
else if (character === options.DELIMITER.WRAP && charAfter === options.DELIMITER.FIELD) {
163-
console.log('CASE 3');
164173
splitLine.push(line.substring(stateVariables.startIndex, index));
165174
stateVariables.startIndex = index + 2;
166175
stateVariables.insideWrapDelimiter = false;
@@ -169,29 +178,32 @@ var splitLine = function (line) {
169178
}
170179
// If we reached a wrap delimiter with a field delimiter after it (ie. ,"*)
171180
else if (character === options.DELIMITER.WRAP && charBefore === options.DELIMITER.FIELD) {
172-
console.log('CASE 4');
173181
if (stateVariables.parsingValue) {
174182
splitLine.push(line.substring(stateVariables.startIndex, index-1));
175183
}
176184
stateVariables.insideWrapDelimiter = true;
177185
stateVariables.parsingValue = true;
178186
stateVariables.startIndex = ++index;
179187
}
180-
// TODO: if index === 0 and comma, then first field is empty
181-
// TODO: if character === '"' and index === 0
182188
// If we reached a field delimiter and are not inside the wrap delimiters (ie. *,*)
183189
else if (character === options.DELIMITER.FIELD && charBefore !== options.DELIMITER.WRAP
184190
&& charAfter !== options.DELIMITER.WRAP && !stateVariables.insideWrapDelimiter
185191
&& stateVariables.parsingValue) {
186-
console.log('CASE 5');
187192
splitLine.push(line.substring(stateVariables.startIndex, index));
188193
stateVariables.insideWrapDelimiter = false;
189194
stateVariables.parsingValue = true;
190195
stateVariables.startIndex = ++index;
191-
} else {
196+
}
197+
else if (character === options.DELIMITER.FIELD && charBefore === options.DELIMITER.WRAP
198+
&& charAfter !== options.DELIMITER.WRAP) {
199+
stateVariables.insideWrapDelimiter = false;
200+
stateVariables.parsingValue = true;
201+
stateVariables.startIndex = ++index;
202+
}
203+
// Otherwise...
204+
else {
192205
index++;
193206
}
194-
console.log(splitLine);
195207
}
196208

197209
return splitLine;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
"carModel","priceRange.min","priceRange.max"
2+
Audi,"9000",11000
3+
"BMW",14000,16000
4+
"Mercedes",19000,"21000"
5+
"Porsche","29000",31000

0 commit comments

Comments
 (0)