cisv 0.4.10 → 0.4.12

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Binary file
package/cisv/wrapper.js CHANGED
@@ -18,6 +18,9 @@ function fastConfigFromOptions(options) {
18
18
  if (typeof quote !== 'string' || quote.length !== 1) {
19
19
  return null;
20
20
  }
21
+ if (delimiter === '\n' || delimiter === '\r' || quote === '\n' || quote === '\r') {
22
+ return null;
23
+ }
21
24
 
22
25
  if (options.escape != null && options.escape !== '') {
23
26
  return null;
@@ -196,31 +199,33 @@ function parseSimpleRows(data, delimiter) {
196
199
  }
197
200
 
198
201
  function parseUniformRows(data, delimiter, rowCount, cols) {
199
- let end = data.length;
200
- if (end === 0) {
202
+ const length = data.length;
203
+ if (length === 0) {
201
204
  return [];
202
205
  }
203
- if (data.charCodeAt(end - 1) === 10) {
204
- end--;
206
+
207
+ let bodyEnd = length;
208
+ if (data.charCodeAt(bodyEnd - 1) === 10) {
209
+ bodyEnd--;
205
210
  }
206
- if (end === 0) {
211
+ if (bodyEnd === 0) {
207
212
  return [['']];
208
213
  }
209
214
 
215
+ const hasTrailingEmptyRow = data.charCodeAt(bodyEnd - 1) === 10;
210
216
  const usePrealloc = rowCount > 0 && cols > 0;
211
217
  if (!usePrealloc) {
212
218
  cols = 1;
213
- for (let i = 0; i < end && data.charCodeAt(i) !== 10; i++) {
219
+ for (let i = 0; i < bodyEnd && data.charCodeAt(i) !== 10; i++) {
214
220
  if (data[i] === delimiter) {
215
221
  cols++;
216
222
  }
217
223
  }
218
224
  }
219
-
220
225
  const rows = usePrealloc ? new Array(rowCount) : [];
221
226
  let rowIdx = 0;
222
227
  let pos = 0;
223
- while (pos < end) {
228
+ while (pos < bodyEnd) {
224
229
  const row = new Array(cols);
225
230
  for (let col = 0; col < cols - 1; col++) {
226
231
  const next = data.indexOf(delimiter, pos);
@@ -229,8 +234,8 @@ function parseUniformRows(data, delimiter, rowCount, cols) {
229
234
  }
230
235
 
231
236
  let lineEnd = data.indexOf('\n', pos);
232
- if (lineEnd === -1 || lineEnd > end) {
233
- lineEnd = end;
237
+ if (lineEnd === -1 || lineEnd > bodyEnd) {
238
+ lineEnd = bodyEnd;
234
239
  }
235
240
  row[cols - 1] = data.slice(pos, lineEnd);
236
241
  if (usePrealloc) {
@@ -241,6 +246,18 @@ function parseUniformRows(data, delimiter, rowCount, cols) {
241
246
  pos = lineEnd + 1;
242
247
  }
243
248
 
249
+ if (hasTrailingEmptyRow) {
250
+ const row = new Array(cols);
251
+ for (let col = 0; col < cols; col++) {
252
+ row[col] = '';
253
+ }
254
+ if (usePrealloc) {
255
+ rows[rowIdx] = row;
256
+ } else {
257
+ rows.push(row);
258
+ }
259
+ }
260
+
244
261
  return rows;
245
262
  }
246
263
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "cisv",
3
- "version": "0.4.10",
3
+ "version": "0.4.12",
4
4
  "description": "The csv parser on steroids.",
5
5
  "author": "sanix<s4nixd@gmail.com>",
6
6
  "main": "./build/Release/cisv.node",