csv-string
Advanced tools
Comparing version 1.0.2 to 1.0.3
137
lib/csv.js
@@ -31,3 +31,3 @@ 'use strict'; | ||
else { | ||
foo += ''+item; | ||
foo += ''+item; | ||
} | ||
@@ -40,5 +40,5 @@ return foo; | ||
exports.detect = function(input) | ||
exports.detect = function (input) | ||
{ | ||
var separators = [{c: 0, v: ','}, {c: 0, v: ';'}, {c: 0, v: '|'}, {c: 0, v: '\t'}, {c:0, v:null}]; | ||
var separators = [{c: 0, v: ','}, {c: 0, v: ';'}, {c: 0, v: '|'}, {c: 0, v: '\t'}, {c: 0, v: null}]; | ||
separators.forEach(function (item, indice) { | ||
@@ -63,15 +63,17 @@ item.c += input.split(item.v).length; | ||
exports.stringify = function(input, sep) { | ||
exports.stringify = function (input, sep) { | ||
var ret = ''; | ||
sep = sep || exports.separator; | ||
if (Array.isArray(input) && !Array.isArray(input[0])) { | ||
input.forEach(function(item) { | ||
input.forEach(function (item) { | ||
ret = reducer(item, ret, sep); | ||
}); | ||
} | ||
); | ||
ret += exports.eol; | ||
} | ||
else if (Array.isArray(input) && Array.isArray(input[0])) { | ||
input.forEach(function(item, index) { | ||
input.forEach(function (item, index) { | ||
ret += exports.stringify(item, sep); | ||
}); | ||
} | ||
); | ||
} | ||
@@ -92,8 +94,9 @@ else if (typeof input == 'object') { | ||
exports.parse = function(input, sep) { | ||
exports.parse = function (input, sep) { | ||
var output = []; | ||
exports.forEach(input, sep, function(row, index) { | ||
exports.forEach(input, sep, function (row, index) { | ||
output.push(row); | ||
}) | ||
} | ||
) | ||
return output; | ||
@@ -103,3 +106,3 @@ } | ||
exports.forEach = function(input, sep, callback) { | ||
exports.forEach = function (input, sep, callback) { | ||
if (arguments.length < 3) { | ||
@@ -110,22 +113,25 @@ callback = sep; | ||
var i = 0, s = 0, r; | ||
while(r = exports.read(input.slice(s), sep, function(fields) { | ||
while (r = exports.read(input.slice(s), sep, function (fields) { | ||
callback(fields, i++); | ||
} | ||
)) s += r; | ||
) | ||
) { | ||
s += r; | ||
} | ||
} | ||
exports.read = function(input, sep, callback) { | ||
exports.read = function (input, sep, callback) { | ||
sep = sep || ","; | ||
var fields = [], reminderIndex = 0, token = '', endingIndex = 0, endingLine = 0, startingIndex = 0, | ||
sepx = new RegExp('[^\\s'+sep+']+.'); | ||
sepx = new RegExp('[^\\s' + sep + ']+.'); | ||
// console.log('input:', input); | ||
// console.log('input.length:', input.length); | ||
// console.log('sep:', sep); | ||
while (startingIndex <= input.length ) { | ||
//console.log('input:', input); | ||
//console.log('input.length:', input.length); | ||
//console.log('sep:', sep); | ||
while (startingIndex <= input.length) { | ||
endingIndex = input.indexOf(sep, startingIndex); | ||
endingLine = input.indexOf('\n', startingIndex); | ||
// console.log('startingIndex:', startingIndex); | ||
// console.log('endingIndex:', endingIndex); | ||
// console.log('endingLine:', endingLine); | ||
//console.log('startingIndex:', startingIndex); | ||
//console.log('endingIndex:', endingIndex); | ||
//console.log('endingLine:', endingLine); | ||
@@ -136,61 +142,62 @@ if (endingIndex < 0) { | ||
token = input.slice(startingIndex, endingIndex).trim(); | ||
// console.log('token:', token); | ||
if (token.charAt(0) == '"' && token.charAt(token.length - 1) == '"') { | ||
// D : "token" | ||
fields.push(token.slice(1, -1)); | ||
//console.log('D : "token" =>', token); | ||
fields.push(token.slice(1, -1).replace(/\"\"/g, '"')); | ||
startingIndex = endingIndex + 1; | ||
} | ||
else if (token.charAt(0) == '"') { | ||
// A : "token | ||
// | ||
var s = input.slice(startingIndex + 1); | ||
if (s.indexOf('"') < 0) { | ||
reminderIndex = startingIndex; | ||
callback(fields); | ||
return reminderIndex; | ||
//console.log('A : "token =>', token); | ||
var s = input.slice(startingIndex + 1); | ||
if (s.indexOf('"') < 0) { | ||
reminderIndex = startingIndex; | ||
callback(fields); | ||
return reminderIndex; | ||
} | ||
else { | ||
var i = s.search(/[^\"]\"(?!\")/) + 1; | ||
if (i === 0 /* -1 + 1 */) { | ||
i = s.search(/[^\"]\"\"\"(?!\")/) + 3; | ||
} | ||
fields.push(input.slice(startingIndex + 1, startingIndex + i + 1).replace(/\"\"/g, '"')); | ||
var j = input.slice(startingIndex + i + 2).search(sepx); | ||
startingIndex += i + 2 + (j >= 0 ? j : 0); | ||
} | ||
} | ||
else if (endingLine >= 0 && endingLine < endingIndex) { | ||
if (input.charAt(endingLine - 1) === '\r') { | ||
fields.push(input.slice(startingIndex, endingLine - 1)); | ||
} | ||
else { | ||
var i = s.search(/[^\"]\"(?!\")/) + 1; | ||
fields.push(input.slice(startingIndex + 1, startingIndex + i + 1)); | ||
var j = input.slice(startingIndex + i + 2).search(sepx); | ||
startingIndex += i + 2 + (j >= 0 ? j : 0); | ||
fields.push(input.slice(startingIndex, endingLine)); | ||
} | ||
reminderIndex = endingLine + 1; | ||
callback(fields); | ||
return reminderIndex; | ||
} | ||
} | ||
else if (endingLine >= 0 && endingLine < endingIndex) { | ||
if (input.charAt(endingLine - 1) === '\r') { | ||
fields.push(input.slice(startingIndex, endingLine - 1)); | ||
else if (token.charAt(token.length - 1) == '"') { | ||
//console.log('C : token" =>', token); | ||
fields.push(input.slice(startingIndex, endingIndex)); | ||
startingIndex = endingIndex + 1; | ||
} | ||
else { | ||
fields.push(input.slice(startingIndex, endingLine)); | ||
// console.log('B : token =>', token); | ||
// console.log(endingLine, '<', endingIndex); | ||
fields.push(input.slice(startingIndex, endingIndex)); | ||
startingIndex = endingIndex + 1; | ||
} | ||
reminderIndex = endingLine + 1; | ||
callback(fields); | ||
return reminderIndex; | ||
} | ||
else if (token.charAt(token.length - 1) == '"') { | ||
// C : token" | ||
fields.push(input.slice(startingIndex, endingIndex)); | ||
startingIndex = endingIndex + 1; | ||
} | ||
else { | ||
// B : token | ||
// console.log(endingLine, '<', endingIndex); | ||
fields.push(input.slice(startingIndex, endingIndex)); | ||
startingIndex = endingIndex + 1; | ||
} | ||
reminderIndex = null; | ||
callback(fields); | ||
return reminderIndex; | ||
} | ||
reminderIndex = null; | ||
callback(fields); | ||
return reminderIndex; | ||
} | ||
exports.fetch = function(input, sep) { | ||
exports.fetch = function (input, sep) { | ||
var output; | ||
exports.read(input, sep, function(fields) { | ||
exports.read(input, sep, function (fields) { | ||
output = fields; | ||
}); | ||
} | ||
); | ||
return output; | ||
} |
{ | ||
"name": "csv-string", | ||
"version": "1.0.2", | ||
"version": "1.0.3", | ||
"author": "Nicolas Thouvenin <nthouvenin@gmail.com>", | ||
@@ -5,0 +5,0 @@ "contributors": [ ], |
@@ -6,2 +6,3 @@ 'use strict'; | ||
describe('CSV', function () { | ||
/* */ | ||
describe('#1 stringify()', function () { | ||
@@ -111,3 +112,2 @@ it('should', function() { | ||
); | ||
/* | ||
describe('#1a fetch()', function () { | ||
@@ -185,3 +185,2 @@ it('should', function() { | ||
); | ||
*/ | ||
describe('#7 fetch()', function () { | ||
@@ -195,3 +194,2 @@ it('should', function() { | ||
); | ||
/* | ||
describe('#1 forEach()', function () { | ||
@@ -255,3 +253,2 @@ it('should', function() { | ||
describe('#1 parse()', function () { | ||
@@ -268,2 +265,22 @@ it('should', function() { | ||
describe('#2 parse()', function () { | ||
it('should handle escaped quotes in a cell', function () { | ||
var data = 'a,b,c,1,"hello ""world""",12,14' | ||
var cols = CSV.parse(data)[0] | ||
cols.length.should.equal(7) | ||
var expected = ['a','b','c','1','hello "world"', '12', '14'] | ||
JSON.stringify(cols).should.equal(JSON.stringify(expected)) | ||
}) | ||
}); | ||
describe('#2 parse()', function () { | ||
it('should handle escaped quotes in a cell', function () { | ||
var data = 'a,b,c,1,"hello, ""world""",12,14' | ||
var cols = CSV.parse(data)[0] | ||
cols.length.should.equal(7) | ||
var expected = ['a','b','c','1','hello, "world"', '12', '14'] | ||
JSON.stringify(cols).should.equal(JSON.stringify(expected)) | ||
}) | ||
}); | ||
describe('#1 detect()', function () { | ||
@@ -270,0 +287,0 @@ it('should', function() { |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
18103
468