Skip to content

Commit

Permalink
Clean up
Browse files Browse the repository at this point in the history
  • Loading branch information
fvdm committed Dec 6, 2017
1 parent 51422d4 commit 61bd744
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 79 deletions.
38 changes: 19 additions & 19 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,29 +1,29 @@
{
"author": {
"name": "Franklin van de Meent",
"email": "fr@nkl.in",
"url": "https://frankl.in"
"name": "Franklin van de Meent",
"email": "fr@nkl.in",
"url": "https://frankl.in"
},
"name": "readcsv",
"description": "Parse CSV file with auto format detection",
"version": "1.0.0",
"name": "readcsv",
"description": "Parse CSV file with auto format detection",
"version": "1.0.0",
"repository": {
"type": "git",
"url": "git://github.com/fvdm/nodejs-readcsv.git"
"type": "git",
"url": "git://github.com/fvdm/nodejs-readcsv.git"
},
"bugs": {
"url": "https://github.com/fvdm/nodejs-readcsv/issues"
"url": "https://github.com/fvdm/nodejs-readcsv/issues"
},
"main": "readcsv.js",
"dependencies": {},
"devDependencies": {},
"optionalDependencies": {},
"main": "readcsv.js",
"dependencies": {},
"devDependencies": {},
"engines": {
"node": "*"
"node": ">=0.12"
},
"keywords": ["csv", "fileformat", "parser"],
"license": {
"type": "Public Domain",
"url": "https://github.com/fvdm/nodejs-readcsv/raw/master/LICENSE"
}
"keywords": [
"csv",
"fileformat",
"parser"
],
"license": "Unlicense"
}
119 changes: 59 additions & 60 deletions readcsv.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,65 +8,64 @@ License: Unlicense / Public Domain
(https://github.com/fvdm/nodejs-readcsv/raw/master/LICENSE)
*/

var fs = require('fs')
var fs = require ('fs');

module.exports = function( head, file, callback ) {
if( typeof file === 'function' ) {
var callback = file
var file = head
var head = null
}
module.exports = function (head, file, callback) {
if (typeof file === 'function') {
callback = file;
file = head;
head = null;
}

fs.readFile( file, function( err, data ) {
if( err ) {
callback( err )
return
}

var output = []
data = data.toString('utf8')

var linebreak = data.slice(-2) === '\r\n' ? '\r\n' : '\n'
data = data.trim().split( linebreak )

var sep = ','
var quotes = '\''

if( data[0].match('\',\'') ) {
sep = ','
quotes = '\''
} else if( data[0].match('\';\'') ) {
sep = ';'
quotes = '\''
} else if( data[0].match('","') ) {
sep = ','
quotes = '"'
} else if( data[0].match('";"') ) {
sep = ';'
quotes = '"'
} else {
callback( new Error('cannot detect line format') )
}

data.forEach( function( line ) {
line = line.split( quotes + sep + quotes )
line[0] = line[0].slice(1)
line[ line.length -1 ] = line[ line.length -1 ].slice(0,-1)
if( head ) {
var tx = {}
head.forEach( function( name, key ) {
tx[ name ] = line[ key ]
})
output.push( tx )
delete tx
} else {
output.push( line )
}
delete line
})

delete data
callback( null, output )
delete output
})
}
fs.readFile (file, function (err, data) {
var output = [];
var sep = ',';
var quotes = '\'';
var linebreak;

if (err) {
callback (err);
return;
}

data = data.toString ('utf8');
linebreak = data.slice (-2) === '\r\n' ? '\r\n' : '\n';
data = data.trim () .split (linebreak);

if (data [0] .match ('\',\'')) {
sep = ',';
quotes = '\'';
} else if (data [0] .match ('\';\'')) {
sep = ';';
quotes = '\'';
} else if (data [0] .match ('","')) {
sep = ',';
quotes = '"';
} else if (data [0] .match ('";"')) {
sep = ';';
quotes = '"';
} else {
callback (new Error ('cannot detect line format'));
return;
}

data.forEach (function (line) {
var tx = {};

line = line.split (quotes + sep + quotes);
line [0] = line [0] .slice (1);
line [line.length - 1] = line [line.length - 1] .slice (0, -1);

if (head) {
head.forEach (function (name, key) {
tx [name] = line [key];
});
output.push (tx);
} else {
output.push (line);
}
});

callback (null, output);
});
};

0 comments on commit 61bd744

Please sign in to comment.