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

Commit

Permalink
objname: accept a buffer
Browse files Browse the repository at this point in the history
  • Loading branch information
wdavidw committed Dec 30, 2018
1 parent b1147fe commit fae2e78
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 19 deletions.
12 changes: 11 additions & 1 deletion CHANGELOG.md
@@ -1,10 +1,20 @@

# Changelog

## Todo

* max_comment_size: new option
* promise: new API module

## Trunk

Enhancements:
New features:

* objname: accept a buffer

Minor enhancements:

* objname: validation refinements
* from: validation refinements
* escape: validation refinements
* skip_empty_lines: validation refinements
Expand Down
17 changes: 16 additions & 1 deletion lib/index.js
Expand Up @@ -11,7 +11,6 @@ const ResizeableBuffer = require('./ResizeableBuffer')

const default_options = {
from_line: 1,
objname: undefined,
// TODO create a max_comment_size
to_line: -1,
to: -1,
Expand Down Expand Up @@ -138,6 +137,22 @@ class Parser extends Transform {
}else{
throw new Error(`Invalid Option: max_record_size must be a positive integer, got ${JSON.stringify(options.max_record_size)}`)
}
// Normalize option `objname`
if(options.objname === undefined || options.objname === null || options.objname === false){
options.objname = Buffer.from(',')
}else if(Buffer.isBuffer(options.objname)){
if(options.objname.length === 0){
throw new Error(`Invalid Option: objname must be a non empty buffer`)
}
options.objname = options.objname.toString()
}else if(typeof options.objname === 'string'){
if(options.objname.length === 0){
throw new Error(`Invalid Option: objname must be a non empty string`)
}
// Great, nothing to do
}else{
throw new Error(`Invalid Option: objname must be a string or a buffer, got ${options.objname}`)
}
// Normalize option `quote`
if(options.quote === null || options.quote === false || options.quote === ''){
options.quote = null
Expand Down
63 changes: 46 additions & 17 deletions test/option.objname.coffee
Expand Up @@ -2,29 +2,58 @@
parse = require '../lib'

describe 'Option `objname`', ->

it 'validation', ->
parse '', objname: 'sth', (->)
parse '', objname: Buffer.from('sth'), (->)
parse '', objname: null, (->)
parse '', objname: undefined, (->)
(->
parse '', objname: '', (->)
).should.throw 'Invalid Option: objname must be a non empty string'
(->
parse '', objname: Buffer.from(''), (->)
).should.throw 'Invalid Option: objname must be a non empty buffer'
(->
parse '', objname: true, (->)
).should.throw 'Invalid Option: objname must be a string or a buffer, got true'

it 'convert a buffer to a column name', (next) ->
parse """
a,b,c
""", objname: Buffer.from('h1'), columns: ['h1', 'h2', 'h3'], (err, data) ->
data.should.eql(
'a':
'h1': 'a'
'h2': 'b'
'h3': 'c'
) unless err
next err


it 'should print object of objects with properties using value of given column from columns', (next) ->
parse """
20322051544,1979,8.8017226E7,ABC,45,2000-01-01
28392898392,1974,8.8392926E7,DEF,23,2050-11-27
""", objname: "FIELD_1", columns: ["FIELD_1", "FIELD_2", "FIELD_3", "FIELD_4", "FIELD_5", "FIELD_6"], (err, data) ->
return next err if err
data.should.eql "20322051544":
"FIELD_1":"20322051544"
"FIELD_2":"1979"
"FIELD_3":"8.8017226E7"
"FIELD_4":"ABC"
"FIELD_5":"45"
"FIELD_6":"2000-01-01"
,
"28392898392":
"FIELD_1":"28392898392"
"FIELD_2":"1974"
"FIELD_3": "8.8392926E7"
"FIELD_4":"DEF"
"FIELD_5":"23"
"FIELD_6":"2050-11-27"
next()
data.should.eql(
"20322051544":
"FIELD_1":"20322051544"
"FIELD_2":"1979"
"FIELD_3":"8.8017226E7"
"FIELD_4":"ABC"
"FIELD_5":"45"
"FIELD_6":"2000-01-01"
,
"28392898392":
"FIELD_1":"28392898392"
"FIELD_2":"1974"
"FIELD_3": "8.8392926E7"
"FIELD_4":"DEF"
"FIELD_5":"23"
"FIELD_6":"2050-11-27"
) unless err
next err

it 'should print object of objects with properties using value of given column from header row', (next) ->
parse """
Expand Down

0 comments on commit fae2e78

Please sign in to comment.