Skip to content

Commit

Permalink
Replace buffer constructor with buffer-alloc and buffer-from (#83)
Browse files Browse the repository at this point in the history
  • Loading branch information
jasnell authored and mafintosh committed Mar 21, 2018
1 parent 18f9a0f commit 9fc626a
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 10 deletions.
22 changes: 12 additions & 10 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,25 @@ var stream = require('stream')
var inherits = require('inherits')
var genobj = require('generate-object-property')
var genfun = require('generate-function')
var bufferFrom = require('buffer-from')
var bufferAlloc = require('buffer-alloc')

var quote = new Buffer('"')[0]
var comma = new Buffer(',')[0]
var cr = new Buffer('\r')[0]
var nl = new Buffer('\n')[0]
var quote = bufferFrom('"')[0]
var comma = bufferFrom(',')[0]
var cr = bufferFrom('\r')[0]
var nl = bufferFrom('\n')[0]

var Parser = function (opts) {
if (!opts) opts = {}
if (Array.isArray(opts)) opts = {headers: opts}

stream.Transform.call(this, {objectMode: true, highWaterMark: 16})

this.separator = opts.separator ? new Buffer(opts.separator)[0] : comma
this.quote = opts.quote ? new Buffer(opts.quote)[0] : quote
this.escape = opts.escape ? new Buffer(opts.escape)[0] : this.quote
this.separator = opts.separator ? bufferFrom(opts.separator)[0] : comma
this.quote = opts.quote ? bufferFrom(opts.quote)[0] : quote
this.escape = opts.escape ? bufferFrom(opts.escape)[0] : this.quote
if (opts.newline) {
this.newline = new Buffer(opts.newline)[0]
this.newline = bufferFrom(opts.newline)[0]
this.customNewline = true
} else {
this.newline = nl
Expand All @@ -36,7 +38,7 @@ var Parser = function (opts) {
this._first = true
this._quoted = false
this._escaped = false
this._empty = this._raw ? new Buffer(0) : ''
this._empty = this._raw ? bufferAlloc(0) : ''
this._Row = null

if (this.headers) {
Expand All @@ -48,7 +50,7 @@ var Parser = function (opts) {
inherits(Parser, stream.Transform)

Parser.prototype._transform = function (data, enc, cb) {
if (typeof data === 'string') data = new Buffer(data)
if (typeof data === 'string') data = bufferFrom(data)

var start = 0
var buf = data
Expand Down
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
"url": "git+https://github.com/mafintosh/csv-parser.git"
},
"dependencies": {
"buffer-alloc": "^1.1.0",
"buffer-from": "^1.0.0",
"generate-function": "^1.0.1",
"generate-object-property": "^1.0.0",
"inherits": "^2.0.1",
Expand Down

0 comments on commit 9fc626a

Please sign in to comment.