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

Commit 3507c67

Browse files
committed
columns_duplicates_to_array: error and type
1 parent 45fca5c commit 3507c67

File tree

6 files changed

+50
-7
lines changed

6 files changed

+50
-7
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,11 @@
1010
* promise: new API module
1111
* errors: finish normalisation of all errors
1212

13+
## Trunk
14+
15+
Minor improvements:
16+
* columns_duplicates_to_array: error and type
17+
1318
## Version 4.10.0
1419

1520
New feature:

lib/es5/index.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,13 @@ var Parser = /*#__PURE__*/function (_Transform) {
131131
options.columns = false;
132132
} else {
133133
throw new CsvError('CSV_INVALID_OPTION_COLUMNS', ['Invalid option columns:', 'expect an object, a function or true,', "got ".concat(JSON.stringify(options.columns))]);
134+
} // Normalize option `columns_duplicates_to_array`
135+
136+
137+
if (options.columns_duplicates_to_array === undefined || options.columns_duplicates_to_array === null || options.columns_duplicates_to_array === false) {
138+
options.columns_duplicates_to_array = false;
139+
} else if (options.columns_duplicates_to_array !== true) {
140+
throw new CsvError('CSV_INVALID_OPTION_COLUMNS_DUPLICATES_TO_ARRAY', ['Invalid option columns_duplicates_to_array:', 'expect an boolean,', "got ".concat(JSON.stringify(options.columns_duplicates_to_array))]);
134141
} // Normalize option `comment`
135142

136143

lib/index.d.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,11 @@ declare namespace parse {
7777
* affect the result data set in the sense that records will be objects instead of arrays.
7878
*/
7979
columns?: ColumnOption[] | boolean | ((record: any) => ColumnOption[]);
80+
/**
81+
* Convert values into an array of values when columns are activated and
82+
* when multiple columns of the same name are found.
83+
*/
84+
columns_duplicates_to_array?: boolean;
8085
/**
8186
* Treat all the characters after this one as a comment, default to '' (disabled).
8287
*/

lib/index.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,16 @@ class Parser extends Transform {
7979
`got ${JSON.stringify(options.columns)}`
8080
])
8181
}
82+
// Normalize option `columns_duplicates_to_array`
83+
if(options.columns_duplicates_to_array === undefined || options.columns_duplicates_to_array === null || options.columns_duplicates_to_array === false){
84+
options.columns_duplicates_to_array = false
85+
}else if(options.columns_duplicates_to_array !== true){
86+
throw new CsvError('CSV_INVALID_OPTION_COLUMNS_DUPLICATES_TO_ARRAY', [
87+
'Invalid option columns_duplicates_to_array:',
88+
'expect an boolean,',
89+
`got ${JSON.stringify(options.columns_duplicates_to_array)}`
90+
])
91+
}
8292
// Normalize option `comment`
8393
if(options.comment === undefined || options.comment === null || options.comment === false || options.comment === ''){
8494
options.comment = null

test/api.types.ts

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,13 @@ describe('API Types', () => {
2929
const options: Options = parser.options
3030
const keys: string[] = Object.keys(options)
3131
keys.sort().should.eql([
32-
'bom', 'cast', 'cast_date', 'columns', 'comment', 'delimiter',
33-
'escape', 'from', 'from_line', 'info', 'ltrim', 'max_record_size',
34-
'objname', 'on_record', 'quote', 'raw', 'record_delimiter',
35-
'relax', 'relax_column_count', 'relax_column_count_less',
36-
'relax_column_count_more', 'rtrim', 'skip_empty_lines',
37-
'skip_lines_with_empty_values', 'skip_lines_with_error', 'to',
38-
'to_line', 'trim'
32+
'bom', 'cast', 'cast_date', 'columns', 'columns_duplicates_to_array',
33+
'comment', 'delimiter', 'escape', 'from', 'from_line', 'info', 'ltrim',
34+
'max_record_size', 'objname', 'on_record', 'quote', 'raw',
35+
'record_delimiter', 'relax', 'relax_column_count',
36+
'relax_column_count_less', 'relax_column_count_more', 'rtrim',
37+
'skip_empty_lines', 'skip_lines_with_empty_values',
38+
'skip_lines_with_error', 'to', 'to_line', 'trim'
3939
])
4040
})
4141

@@ -151,6 +151,11 @@ describe('API Types', () => {
151151
}
152152
})
153153

154+
it('columns_duplicates_to_array', () => {
155+
const options: Options = {}
156+
options.columns_duplicates_to_array = true
157+
})
158+
154159
it('comment', () => {
155160
const options: Options = {}
156161
options.comment = '\\'

test/option.columns_duplicates_to_array.coffee

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,18 @@ parse = require '../lib'
33
assert_error = require './api.assert_error'
44

55
describe 'Option `columns_duplicates_to_array`', ->
6+
7+
it 'validate', ->
8+
(->
9+
parse "", columns_duplicates_to_array: 'invalid'
10+
).should.throw
11+
code: 'CSV_INVALID_OPTION_COLUMNS_DUPLICATES_TO_ARRAY'
12+
message: [
13+
'Invalid option columns_duplicates_to_array:',
14+
'expect an boolean, got "invalid"'
15+
].join ' '
616

17+
718
it 'when false', (next) ->
819
parse """
920
FIELD_1,FIELD_1

0 commit comments

Comments
 (0)