Comparing version
@@ -31,2 +31,3 @@ /* | ||
Baby.parse = CsvToJson; | ||
Baby.parseFiles = ParseFiles; | ||
Baby.unparse = JsonToCsv; | ||
@@ -41,2 +42,37 @@ Baby.RECORD_SEP = String.fromCharCode(30); | ||
function ParseFiles(_input, _config) | ||
{ | ||
if (Array.isArray(_input)) { | ||
var results = []; | ||
_input.forEach(function(input) { | ||
if(typeof input === 'object') | ||
results.push(ParseFiles(input.file, input.config)); | ||
else | ||
results.push(ParseFiles(input, _config)); | ||
}); | ||
return results; | ||
} else { | ||
var results = { | ||
data: [], | ||
errors: [] | ||
}; | ||
if ((/(\.csv|\.txt)$/).test(_input)) { | ||
try { | ||
var contents = fs.readFileSync(_input).toString(); | ||
return CsvToJson(contents, _config); | ||
} catch (err) { | ||
results.errors.push(err); | ||
return results; | ||
} | ||
} else { | ||
results.errors.push({ | ||
type: '', | ||
code: '', | ||
message: 'Unsupported file type.', | ||
row: '' | ||
}); | ||
return results; | ||
} | ||
} | ||
} | ||
@@ -43,0 +79,0 @@ function CsvToJson(_input, _config) |
{ | ||
"name": "babyparse", | ||
"version": "0.4.3", | ||
"version": "0.4.4", | ||
"title": "BabyParse", | ||
@@ -5,0 +5,0 @@ "repository": { |
@@ -8,2 +8,10 @@ Baby Parse | ||
Installation | ||
----- | ||
```js | ||
// simply install using npm | ||
npm install babyparse | ||
``` | ||
Basic Usage | ||
@@ -20,2 +28,25 @@ ----- | ||
Parse File(s) | ||
----- | ||
Baby Parse will assume the input is a filename if it ends in .csv or .txt. | ||
```js | ||
// Parse single file | ||
parsed = Baby.parseFiles(file[, config]) | ||
rows = parsed.data | ||
``` | ||
```js | ||
// Parse multiple files | ||
// Files can be either an array of strings or objects { file: filename[, config: config] } | ||
// When using and array of objects and you include a config it will be used in place of the global config | ||
parsed = Baby.parseFiles(files[, globalConfig]) | ||
rows = parsed[index].data | ||
``` | ||
For a complete understanding of the power of this library, please refer to the [Papa Parse web site](http://papaparse.com). | ||
@@ -22,0 +53,0 @@ |
59422
2.35%2059
1.78%63
96.88%