parser-csv
Advanced tools
Comparing version 0.1.0 to 0.1.1
33
.verb.md
@@ -6,2 +6,3 @@ # {%= name %} {%= badge("fury") %} | ||
## Install | ||
{%= include("install-npm", {save: true}) %} | ||
@@ -14,28 +15,41 @@ | ||
``` | ||
{% var nickname = "csv" %} | ||
## Usage | ||
```js | ||
var {%= nickname %} = require('{%= name %}'); | ||
var parser = require('{%= name %}'); | ||
``` | ||
### async | ||
See [parse-csv] for the full range of options and features, and to report issues related to parsing. | ||
### parse | ||
```js | ||
{%= nickname %}.parse(str, options, function(err, result) { | ||
parser.parse(str, options, function(err, res) { | ||
if (err) { throw err; } | ||
console.log(result); | ||
console.log(res); | ||
}); | ||
``` | ||
### sync | ||
### parseFile | ||
```js | ||
var result = {%= nickname %}.parseSync(str, options); | ||
console.log(result); | ||
parser.parseFile('fixtures/a.csv', function (err, res) { | ||
if (err) { throw err; } | ||
console.log(res); | ||
}); | ||
``` | ||
### parseSync | ||
```js | ||
var res = parser.parseSync(str, options); | ||
console.log(res); | ||
``` | ||
## API | ||
{%= comments("index.js") %} | ||
## Contributing | ||
Pull requests and stars are always welcome. For bugs and feature requests, [please create an issue]({%= bugs.url %}) | ||
@@ -54,2 +68,3 @@ | ||
[parser-cache]: https://github.com/jonschlinkert/parser-cache | ||
[parser-cache]: https://github.com/jonschlinkert/parser-cache | ||
[parse-csv]: https://github.com/jonschlinkert/parse-csv |
90
index.js
@@ -7,9 +7,13 @@ 'use strict'; | ||
var fs = require('fs'); | ||
var extend = require('extend-shallow'); | ||
var csv = require('parse-csv'); | ||
/** | ||
* Requires cache | ||
*/ | ||
var requires = {}; | ||
/** | ||
* Expose `parser` | ||
* | ||
* @type {Object} | ||
*/ | ||
@@ -20,14 +24,15 @@ | ||
/** | ||
* Parse the given `str` of CSV and callback `next(err, json)`. | ||
* Parse the given `str` of CSV and callback `cb(err, json)`. | ||
* | ||
* @name .parse | ||
* @param {String|Object} `str` The object or string to parse. | ||
* @param {Object|Function} `options` or `next` callback function. | ||
* @param {Function} `next` callback function. | ||
* @param {Object|Function} `options` or `cb` callback function. | ||
* @param {Function} `cb` callback function. | ||
* @api public | ||
*/ | ||
parser.parse = function(str, options, next) { | ||
parser.parse = function(str, options, cb) { | ||
var csv = requires.csv || (requires.csv = require('parse-csv')); | ||
if (typeof options === 'function') { | ||
next = options; | ||
cb = options; | ||
options = {}; | ||
@@ -39,5 +44,5 @@ } | ||
opts.jsonType = opts.jsonType || 'jsonDict'; | ||
next(null, JSON.parse(csv.to(opts.csv, str, opts))); | ||
cb(null, JSON.parse(csv.to(opts.csv, stripBOM(str), opts))); | ||
} catch (err) { | ||
next(err); | ||
cb(err); | ||
return; | ||
@@ -56,8 +61,7 @@ } | ||
parser.parseSync = function(str, options) { | ||
options = options || {}; | ||
var csv = requires.csv || (requires.csv = require('parse-csv')); | ||
var opts = extend({headers: {included: true}}, options); | ||
try { | ||
opts.jsonType = opts.jsonType || 'jsonDict'; | ||
return JSON.parse(csv.to(opts.csv, str, opts)); | ||
return JSON.parse(csv.to(opts.csv, stripBOM(str), opts)); | ||
} catch (err) { | ||
@@ -67,1 +71,61 @@ return err; | ||
}; | ||
/** | ||
* CSV file support. Parse the given `str` of CSV and callback `cb(err, data)`. | ||
* | ||
* @param {String|Object} `str` The object or string to parse. | ||
* @return {Object} | ||
* @api public | ||
*/ | ||
parser.parseFile = function(fp, options, cb) { | ||
if (typeof options === 'function') { | ||
cb = options; | ||
options = {}; | ||
} | ||
var opts = extend({}, options); | ||
try { | ||
fs.readFile(fp, 'utf8', function(err, str) { | ||
parser.parse(str, opts, cb); | ||
}); | ||
} catch (err) { | ||
cb(err); | ||
return; | ||
} | ||
}; | ||
/** | ||
* CSV file support. Parse a file at the given `fp`. | ||
* | ||
* ```js | ||
* parser.parseFile('foo/bar/baz.csv'); | ||
* ``` | ||
* | ||
* @param {String} `fp` | ||
* @param {Object} `options` Options to pass to [parse-csv] | ||
* @api public | ||
*/ | ||
parser.parseFileSync = function(fp, options) { | ||
try { | ||
var str = fs.readFileSync(fp, 'utf8'); | ||
return parser.parseSync(str, options); | ||
} catch (err) { | ||
return err; | ||
} | ||
}; | ||
/** | ||
* Strip byte-order marks | ||
* | ||
* @api private | ||
*/ | ||
function stripBOM(str) { | ||
if (str[0] === '\uFEFF') { | ||
return str.substring(1); | ||
} else { | ||
return str; | ||
} | ||
} |
{ | ||
"name": "parser-csv", | ||
"description": "CSV parser, compatible with [parser-cache].", | ||
"version": "0.1.0", | ||
"version": "0.1.1", | ||
"homepage": "https://github.com/jonschlinkert/parser-csv", | ||
@@ -30,2 +30,6 @@ "author": { | ||
}, | ||
"dependencies": { | ||
"extend-shallow": "^0.1.1", | ||
"parse-csv": "^0.1.0" | ||
}, | ||
"devDependencies": { | ||
@@ -36,8 +40,13 @@ "mocha": "*", | ||
"keywords": [ | ||
"parser" | ||
], | ||
"dependencies": { | ||
"extend-shallow": "^0.1.1", | ||
"parse-csv": "^0.1.0" | ||
} | ||
} | ||
"cache", | ||
"conversion", | ||
"convert", | ||
"converter", | ||
"csv", | ||
"json", | ||
"parse", | ||
"parser", | ||
"parser-cache", | ||
"render" | ||
] | ||
} |
@@ -6,2 +6,3 @@ # parser-csv [![NPM version](https://badge.fury.io/js/parser-csv.svg)](http://badge.fury.io/js/parser-csv) | ||
## Install | ||
### Install with [npm](npmjs.org) | ||
@@ -21,38 +22,70 @@ | ||
```js | ||
var csv = require('parser-csv'); | ||
var parser = require('parser-csv'); | ||
``` | ||
### async | ||
See [parse-csv] for the full range of options and features, and to report issues related to parsing. | ||
### parse | ||
```js | ||
csv.parse(str, options, function(err, result) { | ||
parser.parse(str, options, function(err, res) { | ||
if (err) { throw err; } | ||
console.log(result); | ||
console.log(res); | ||
}); | ||
``` | ||
### sync | ||
### parseFile | ||
```js | ||
var result = csv.parseSync(str, options); | ||
console.log(result); | ||
parser.parseFile('fixtures/a.csv', function (err, res) { | ||
if (err) { throw err; } | ||
console.log(res); | ||
}); | ||
``` | ||
### parseSync | ||
```js | ||
var res = parser.parseSync(str, options); | ||
console.log(res); | ||
``` | ||
## API | ||
### [parse](index.js#L28) | ||
* `str` **{String|Object}**: The object or string to parse. | ||
* `options` **{Object|Function}**: or `next` callback function. | ||
* `next` **{Function}**: callback function. | ||
### [parse](index.js#L32) | ||
Parse the given `str` of CSV and callback `next(err, json)`. | ||
* `str` **{String|Object}**: The object or string to parse. | ||
* `options` **{Object|Function}**: or `cb` callback function. | ||
* `cb` **{Function}**: callback function. | ||
### [parseSync](index.js#L52) | ||
Parse the given `str` of CSV and callback `cb(err, json)`. | ||
* `str` **{String|Object}**: The object or string to parse. | ||
* `returns`: {Object} | ||
### [parseSync](index.js#L57) | ||
* `str` **{String|Object}**: The object or string to parse. | ||
* `returns`: {Object} | ||
Parse the given `str` of CSV and return an object. | ||
### [parseFile](index.js#L76) | ||
* `str` **{String|Object}**: The object or string to parse. | ||
* `returns`: {Object} | ||
CSV file support. Parse the given `str` of CSV and callback `cb(err, data)`. | ||
### [parseFileSync](index.js#L105) | ||
CSV file support. Parse a file at the given `fp`. | ||
* `fp` **{String}** | ||
* `options` **{Object}**: Options to pass to [parse-csv] | ||
```js | ||
parser.parseFile('foo/bar/baz.csv'); | ||
``` | ||
## Contributing | ||
Pull requests and stars are always welcome. For bugs and feature requests, [please create an issue](https://github.com/jonschlinkert/parser-csv/issues) | ||
@@ -63,8 +96,8 @@ | ||
**Jon Schlinkert** | ||
+ [github/jonschlinkert](https://github.com/jonschlinkert) | ||
+ [twitter/jonschlinkert](http://twitter.com/jonschlinkert) | ||
+ [twitter/jonschlinkert](http://twitter.com/jonschlinkert) | ||
## License | ||
Copyright (c) 2014 Jon Schlinkert | ||
Copyright (c) 2014 Jon Schlinkert | ||
Released under the MIT license | ||
@@ -74,4 +107,5 @@ | ||
_This file was generated by [verb](https://github.com/assemble/verb) on November 22, 2014._ | ||
_This file was generated by [verb](https://github.com/assemble/verb) on November 24, 2014._ | ||
[parser-cache]: https://github.com/jonschlinkert/parser-cache | ||
[parser-cache]: https://github.com/jonschlinkert/parser-cache | ||
[parse-csv]: https://github.com/jonschlinkert/parse-csv |
Filesystem access
Supply chain riskAccesses the file system, and could potentially read sensitive data.
Found 1 instance in 1 package
9600
143
107
1