Skip to content

Commit 147210f

Browse files
committed
cleaned up the code a little bit. going to take another look tomorrow and add a few more tests; incrementing to 1.3.0
1 parent 4806c8b commit 147210f

File tree

4 files changed

+22
-48
lines changed

4 files changed

+22
-48
lines changed

README.md

+4-4
Original file line numberDiff line numberDiff line change
@@ -145,10 +145,10 @@ $ npm run coverage
145145

146146
Current Coverage is:
147147
```
148-
Statements : 94.49% ( 120/127 )
149-
Branches : 90.24% ( 74/82 )
150-
Functions : 100% ( 30/30 )
151-
Lines : 97.39% ( 112/115 )
148+
Statements : 94.51% ( 155/164 )
149+
Branches : 92.62% ( 113/122 )
150+
Functions : 100% ( 31/31 )
151+
Lines : 96.69% ( 146/151 )
152152
```
153153

154154
## Features

bower.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "json-2-csv",
3-
"version": "1.2.1",
3+
"version": "1.3.0",
44
"homepage": "https://github.com/mrodrig/json-2-csv",
55
"moduleType": [
66
"node"

lib/csv-2-json.js

+16-42
Original file line numberDiff line numberDiff line change
@@ -6,21 +6,6 @@ var _ = require('underscore'),
66

77
var options = {}; // Initialize the options - this will be populated when the csv2json function is called.
88

9-
/** Helper function from http://phpjs.org/functions/addslashes/ **/
10-
var addSlashes = function (str) {
11-
return (str + '')
12-
.replace(/[\\"']/g, '\\$&')
13-
.replace(/\u0000/g, '\\0');
14-
};
15-
16-
var lstrip = function (str, search) {
17-
return str.replace(new RegExp('^' + search + '*'), '');
18-
};
19-
20-
var rstrip = function (str, search) {
21-
return str.replace(new RegExp(search + '*$'), '');
22-
};
23-
249
/**
2510
* Generate the JSON heading from the CSV
2611
* @param lines
@@ -138,43 +123,36 @@ var splitLine = function (line) {
138123
},
139124
index = 0;
140125

126+
// Loop through each character in the line to identify where to split the values
141127
while(index < line.length) {
142-
character = line[index];
128+
// Current character
129+
character = line[index];
130+
// Previous character
143131
charBefore = index ? line[index - 1] : '';
132+
// Next character
144133
charAfter = index < lastCharacterIndex ? line[index + 1] : '';
145134

146-
// If we reached the end of the line and are in the wrap delimiter, substring up to, but not including this char
147-
if (index === lastCharacterIndex && stateVariables.insideWrapDelimiter) {
148-
stateVariables.parsingValue = false;
149-
splitLine.push(line.substring(stateVariables.startIndex, index));
150-
index++;
151-
}
152-
// If we reached the end of the line and are not in the wrap delimiter
153-
else if (index === lastCharacterIndex) {
154-
stateVariables.parsingValue = false;
155-
splitLine.push(line.substring(stateVariables.startIndex));
156-
index++;
135+
// If we reached the end of the line, add the remaining value
136+
if (index === lastCharacterIndex) {
137+
splitLine.push(line.substring(stateVariables.startIndex, stateVariables.insideWrapDelimiter ? index : undefined));
157138
}
158139
// If the line starts with a wrap delimiter
159140
else if (character === options.DELIMITER.WRAP && index === 0) {
160-
stateVariables.startIndex = index + 1;
161141
stateVariables.insideWrapDelimiter = true;
162142
stateVariables.parsingValue = true;
163-
index++;
143+
stateVariables.startIndex = index + 1;
164144
}
165-
// If the first field is empty
145+
// If the first field is empty (ie. line starts with a field delimiter)
166146
else if (character === options.DELIMITER.FIELD && index == 0) {
167147
splitLine.push('');
168148
stateVariables.parsingValue = false;
169-
index++;
170149
}
171150
// If we reached a wrap delimiter with a field delimiter after it (ie. *",)
172151
else if (character === options.DELIMITER.WRAP && charAfter === options.DELIMITER.FIELD) {
173152
splitLine.push(line.substring(stateVariables.startIndex, index));
174-
stateVariables.startIndex = index + 2;
153+
stateVariables.startIndex = index + 2; // next value starts after the field delimiter
175154
stateVariables.insideWrapDelimiter = false;
176155
stateVariables.parsingValue = false;
177-
index++;
178156
}
179157
// If we reached a wrap delimiter with a field delimiter after it (ie. ,"*)
180158
else if (character === options.DELIMITER.WRAP && charBefore === options.DELIMITER.FIELD) {
@@ -183,27 +161,23 @@ var splitLine = function (line) {
183161
}
184162
stateVariables.insideWrapDelimiter = true;
185163
stateVariables.parsingValue = true;
186-
stateVariables.startIndex = ++index;
164+
stateVariables.startIndex = index + 1;
187165
}
188166
// If we reached a field delimiter and are not inside the wrap delimiters (ie. *,*)
189167
else if (character === options.DELIMITER.FIELD && charBefore !== options.DELIMITER.WRAP
190168
&& charAfter !== options.DELIMITER.WRAP && !stateVariables.insideWrapDelimiter
191169
&& stateVariables.parsingValue) {
192170
splitLine.push(line.substring(stateVariables.startIndex, index));
193-
stateVariables.insideWrapDelimiter = false;
194-
stateVariables.parsingValue = true;
195-
stateVariables.startIndex = ++index;
171+
stateVariables.startIndex = index + 1;
196172
}
197173
else if (character === options.DELIMITER.FIELD && charBefore === options.DELIMITER.WRAP
198174
&& charAfter !== options.DELIMITER.WRAP) {
199175
stateVariables.insideWrapDelimiter = false;
200176
stateVariables.parsingValue = true;
201-
stateVariables.startIndex = ++index;
202-
}
203-
// Otherwise...
204-
else {
205-
index++;
177+
stateVariables.startIndex = index + 1;
206178
}
179+
// Otherwise increment to the next character
180+
index++;
207181
}
208182

209183
return splitLine;

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"author": "mrodrig",
33
"name": "json-2-csv",
44
"description": "A JSON to CSV and CSV to JSON converter that natively supports sub-documents and auto-generates the CSV heading.",
5-
"version": "1.2.1",
5+
"version": "1.3.0",
66
"repository": {
77
"type": "git",
88
"url": "http://github.com/mrodrig/json-2-csv.git"

0 commit comments

Comments
 (0)