Skip to content
This repository has been archived by the owner on Jun 28, 2021. It is now read-only.

Commit

Permalink
cast: inject lines property
Browse files Browse the repository at this point in the history
  • Loading branch information
wdavidw committed May 5, 2018
1 parent 0df56f1 commit e55527d
Show file tree
Hide file tree
Showing 8 changed files with 75 additions and 26 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@

# Changelog

## Trunk

cast: inject lines property

## Version 2.2.0

* cast: deprecate auto_parse
Expand Down
9 changes: 6 additions & 3 deletions lib/es5/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -420,6 +420,9 @@ Parser.prototype.__write = function (chars, end) {
if (context.quoting == null) {
context.quoting = !!_this2._.closingQuote;
}
if (context.lines == null) {
context.lines = _this2.lines;
}
if (context.count == null) {
context.count = _this2.count;
}
Expand Down Expand Up @@ -654,6 +657,9 @@ Parser.prototype.__write = function (chars, end) {
}
// Flush remaining fields and lines
if (end) {
if (l === 0) {
this.lines++;
}
if (this._.field != null) {
if (rtrim) {
if (!this._.closingQuote) {
Expand All @@ -666,9 +672,6 @@ Parser.prototype.__write = function (chars, end) {
if (((ref5 = this._.field) != null ? ref5.length : void 0) > this.options.max_limit_on_data_read) {
return Error('Delimiter not found in the file ' + JSON.stringify(this.options.delimiter));
}
if (l === 0) {
this.lines++;
}
if (this._.line.length > this.options.max_limit_on_data_read) {
return Error('Row delimiter not found in the file ' + JSON.stringify(this.options.rowDelimiter));
}
Expand Down
9 changes: 6 additions & 3 deletions lib/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions src/index.coffee.md
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,7 @@ Implementation of the [`stream.Transform` API][transform]
cast = (value, context = {}) =>
return value unless @options.cast
context.quoting ?= !!@_.closingQuote
context.lines ?= @lines
context.count ?= @count
context.index ?= @_.line.length
context.column ?= if Array.isArray @options.columns then @options.columns[context.index] else context.index
Expand Down Expand Up @@ -422,15 +423,15 @@ Implementation of the [`stream.Transform` API][transform]
return Error "Row delimiter not found in the file #{JSON.stringify(@options.rowDelimiter)}"
# Flush remaining fields and lines
if end
if l is 0
@lines++
if @_.field?
if rtrim
@_.field = @_.field?.trimRight() unless @_.closingQuote
@_.line.push cast @_.field or ''
@_.field = null
if @_.field?.length > @options.max_limit_on_data_read
return Error "Delimiter not found in the file #{JSON.stringify(@options.delimiter)}"
if l is 0
@lines++
if @_.line.length > @options.max_limit_on_data_read
return Error "Row delimiter not found in the file #{JSON.stringify(@options.rowDelimiter)}"
# Store un-parsed chars for next call
Expand Down
4 changes: 2 additions & 2 deletions test/options.auto_parse.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@ describe 'options "auto_parse" (deprecated)', ->
else {...context}
, (err, records) ->
records.should.eql [
[ '2000-01-01T05:00:00.000Z', {quoting: false, count: 0, index: 1, column: 1} ]
[ '2050-11-27T05:00:00.000Z', {quoting: false, count: 1, index: 1, column: 1} ]
[ '2000-01-01T05:00:00.000Z', {quoting: false, count: 0, index: 1, column: 1, lines: 1} ]
[ '2050-11-27T05:00:00.000Z', {quoting: false, count: 1, index: 1, column: 1, lines: 2} ]
] unless err
next err

Expand Down
21 changes: 19 additions & 2 deletions test/options.cast.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@ describe 'options "cast"', ->
else {...context}
, (err, records) ->
records.should.eql [
[ '2000-01-01T05:00:00.000Z', {quoting: false, count: 0, index: 1, column: 1} ]
[ '2050-11-27T05:00:00.000Z', {quoting: false, count: 1, index: 1, column: 1} ]
[ '2000-01-01T05:00:00.000Z', {quoting: false, count: 0, index: 1, column: 1, lines: 1} ]
[ '2050-11-27T05:00:00.000Z', {quoting: false, count: 1, index: 1, column: 1, lines: 2} ]
] unless err
next err

Expand All @@ -70,3 +70,20 @@ describe 'options "cast"', ->
[ false, true ]
] unless err
next err

it 'with column', (next) ->
parse """
a,b,c
1,2,3
4,5,6
""",
columns: true
auto_parse: (value, context) ->
console.log context
# if count is -1 then value else parseInt value
, (err, records) ->
# records.should.eql [
# {a: 1, b: 2, c: 3}
# {a: 4, b: 5, c: 6}
# ] unless err
next err
16 changes: 14 additions & 2 deletions test/options.quote.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -112,9 +112,21 @@ describe 'error', ->
""", quote: '"', escape: '"', delimiter: "\t", (err) ->
err.message.should.eql 'Invalid closing quote at line 1; found " " instead of delimiter "\\t"'
next()

it '"invalid opening quotes" count empty lines', (next) ->
parse """
"this","line","is",valid
"this","line",is,"also,valid"
this,"line",is,invalid h"ere"
"and",valid,line,follows...
""", (err, data) ->
err.message.should.eql 'Invalid opening quote at line 4'
(data == undefined).should.be.true
next()

it 'when invalid quotes from string', (next) ->
it '"Invalid closing quote" display expected delimiter', (next) ->
parse '"",1974,8.8392926E7,""t,""', quote: '"', escape: '"', (err) ->
err.message.should.match /Invalid closing quote/
err.message.should.eql 'Invalid closing quote at line 1; found "t" instead of delimiter ","'
next()

33 changes: 21 additions & 12 deletions test/properties.lines.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,35 @@ parse = require '../src'

describe 'properties counter', ->

it 'should display correct line number when invalid opening quotes', (next) ->
parse """
"this","line","is",valid
"this","line",is,"also,valid"
this,"line",is,"invalid",h"ere"
"and",valid,line,follows...
it 'count lines', (next) ->
p = parse """
a,b,c
d,e,f
h,i,j
""", (err, data) ->
err.message.should.match /Invalid opening quote at line 3/
(data == undefined).should.be.true
next()
p.lines.should.eql 3 unless err
next err

it 'should count empty lines', (next) ->
p = parse """
a,b,c
d,e,f
h,i,j
""", skip_empty_lines: true, (err, data) ->
p.lines.should.eql 6 unless err
next err

it 'should display correct line number when invalid opening quotes', (next) ->
parse """
"this","line","is",valid
"this","line",is,"also,valid"
this,"line",is,invalid h"ere"
this,"line",is,"invalid",h"ere"
"and",valid,line,follows...
""", (err, data) ->
err.message.should.match /Invalid opening quote at line 4/
err.message.should.match /Invalid opening quote at line 3/
(data == undefined).should.be.true
next()

Expand Down

0 comments on commit e55527d

Please sign in to comment.