@contrail/cli 3.4.4-alpha.7adb62a → 3.4.4-alpha.e5b7d5d
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +1 -1
- package/node_modules/@contrail/loader/README.md +0 -0
- package/node_modules/@contrail/loader/lib/index.d.ts +941 -0
- package/node_modules/@contrail/loader/lib/index.js +12492 -0
- package/node_modules/@contrail/loader/lib/index.js.map +1 -0
- package/node_modules/@contrail/loader/package.json +70 -0
- package/node_modules/csv-parse/LICENSE +21 -0
- package/node_modules/csv-parse/README.md +86 -0
- package/node_modules/csv-parse/dist/cjs/index.cjs +1424 -0
- package/node_modules/csv-parse/dist/cjs/index.d.cts +283 -0
- package/node_modules/csv-parse/dist/cjs/sync.cjs +1336 -0
- package/node_modules/csv-parse/dist/cjs/sync.d.cts +11 -0
- package/node_modules/csv-parse/dist/esm/index.d.ts +283 -0
- package/node_modules/csv-parse/dist/esm/index.js +6544 -0
- package/node_modules/csv-parse/dist/esm/sync.d.ts +11 -0
- package/node_modules/csv-parse/dist/esm/sync.js +3305 -0
- package/node_modules/csv-parse/dist/iife/index.js +6553 -0
- package/node_modules/csv-parse/dist/iife/sync.js +3313 -0
- package/node_modules/csv-parse/dist/umd/index.js +6554 -0
- package/node_modules/csv-parse/dist/umd/sync.js +3314 -0
- package/node_modules/csv-parse/lib/api/CsvError.js +19 -0
- package/node_modules/csv-parse/lib/api/index.js +729 -0
- package/node_modules/csv-parse/lib/api/init_state.js +58 -0
- package/node_modules/csv-parse/lib/api/normalize_columns_array.js +33 -0
- package/node_modules/csv-parse/lib/api/normalize_options.js +448 -0
- package/node_modules/csv-parse/lib/index.d.ts +283 -0
- package/node_modules/csv-parse/lib/index.js +121 -0
- package/node_modules/csv-parse/lib/stream.js +27 -0
- package/node_modules/csv-parse/lib/sync.d.ts +11 -0
- package/node_modules/csv-parse/lib/sync.js +27 -0
- package/node_modules/csv-parse/lib/utils/ResizeableBuffer.js +64 -0
- package/node_modules/csv-parse/lib/utils/is_object.js +6 -0
- package/node_modules/csv-parse/lib/utils/underscore.js +8 -0
- package/node_modules/csv-parse/package.json +142 -0
- package/oclif.manifest.json +1 -1
- package/package.json +3 -1
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
|
|
2
|
+
import ResizeableBuffer from '../utils/ResizeableBuffer.js';
|
|
3
|
+
|
|
4
|
+
// white space characters
|
|
5
|
+
// https://en.wikipedia.org/wiki/Whitespace_character
|
|
6
|
+
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions/Character_Classes#Types
|
|
7
|
+
// \f\n\r\t\v\u00a0\u1680\u2000-\u200a\u2028\u2029\u202f\u205f\u3000\ufeff
|
|
8
|
+
const np = 12;
|
|
9
|
+
const cr = 13; // `\r`, carriage return, 0x0D in hexadécimal, 13 in decimal
|
|
10
|
+
const nl = 10; // `\n`, newline, 0x0A in hexadecimal, 10 in decimal
|
|
11
|
+
const space = 32;
|
|
12
|
+
const tab = 9;
|
|
13
|
+
|
|
14
|
+
const init_state = function(options){
|
|
15
|
+
return {
|
|
16
|
+
bomSkipped: false,
|
|
17
|
+
bufBytesStart: 0,
|
|
18
|
+
castField: options.cast_function,
|
|
19
|
+
commenting: false,
|
|
20
|
+
// Current error encountered by a record
|
|
21
|
+
error: undefined,
|
|
22
|
+
enabled: options.from_line === 1,
|
|
23
|
+
escaping: false,
|
|
24
|
+
escapeIsQuote: Buffer.isBuffer(options.escape) && Buffer.isBuffer(options.quote) && Buffer.compare(options.escape, options.quote) === 0,
|
|
25
|
+
// columns can be `false`, `true`, `Array`
|
|
26
|
+
expectedRecordLength: Array.isArray(options.columns) ? options.columns.length : undefined,
|
|
27
|
+
field: new ResizeableBuffer(20),
|
|
28
|
+
firstLineToHeaders: options.cast_first_line_to_header,
|
|
29
|
+
needMoreDataSize: Math.max(
|
|
30
|
+
// Skip if the remaining buffer smaller than comment
|
|
31
|
+
options.comment !== null ? options.comment.length : 0,
|
|
32
|
+
// Skip if the remaining buffer can be delimiter
|
|
33
|
+
...options.delimiter.map((delimiter) => delimiter.length),
|
|
34
|
+
// Skip if the remaining buffer can be escape sequence
|
|
35
|
+
options.quote !== null ? options.quote.length : 0,
|
|
36
|
+
),
|
|
37
|
+
previousBuf: undefined,
|
|
38
|
+
quoting: false,
|
|
39
|
+
stop: false,
|
|
40
|
+
rawBuffer: new ResizeableBuffer(100),
|
|
41
|
+
record: [],
|
|
42
|
+
recordHasError: false,
|
|
43
|
+
record_length: 0,
|
|
44
|
+
recordDelimiterMaxLength: options.record_delimiter.length === 0 ? 0 : Math.max(...options.record_delimiter.map((v) => v.length)),
|
|
45
|
+
trimChars: [Buffer.from(' ', options.encoding)[0], Buffer.from('\t', options.encoding)[0]],
|
|
46
|
+
wasQuoting: false,
|
|
47
|
+
wasRowDelimiter: false,
|
|
48
|
+
timchars: [
|
|
49
|
+
Buffer.from(Buffer.from([cr], 'utf8').toString(), options.encoding),
|
|
50
|
+
Buffer.from(Buffer.from([nl], 'utf8').toString(), options.encoding),
|
|
51
|
+
Buffer.from(Buffer.from([np], 'utf8').toString(), options.encoding),
|
|
52
|
+
Buffer.from(Buffer.from([space], 'utf8').toString(), options.encoding),
|
|
53
|
+
Buffer.from(Buffer.from([tab], 'utf8').toString(), options.encoding),
|
|
54
|
+
]
|
|
55
|
+
};
|
|
56
|
+
};
|
|
57
|
+
|
|
58
|
+
export {init_state};
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
|
|
2
|
+
import {CsvError} from './CsvError.js';
|
|
3
|
+
import {is_object} from '../utils/is_object.js';
|
|
4
|
+
|
|
5
|
+
const normalize_columns_array = function(columns){
|
|
6
|
+
const normalizedColumns = [];
|
|
7
|
+
for(let i = 0, l = columns.length; i < l; i++){
|
|
8
|
+
const column = columns[i];
|
|
9
|
+
if(column === undefined || column === null || column === false){
|
|
10
|
+
normalizedColumns[i] = { disabled: true };
|
|
11
|
+
}else if(typeof column === 'string'){
|
|
12
|
+
normalizedColumns[i] = { name: column };
|
|
13
|
+
}else if(is_object(column)){
|
|
14
|
+
if(typeof column.name !== 'string'){
|
|
15
|
+
throw new CsvError('CSV_OPTION_COLUMNS_MISSING_NAME', [
|
|
16
|
+
'Option columns missing name:',
|
|
17
|
+
`property "name" is required at position ${i}`,
|
|
18
|
+
'when column is an object literal'
|
|
19
|
+
]);
|
|
20
|
+
}
|
|
21
|
+
normalizedColumns[i] = column;
|
|
22
|
+
}else{
|
|
23
|
+
throw new CsvError('CSV_INVALID_COLUMN_DEFINITION', [
|
|
24
|
+
'Invalid column definition:',
|
|
25
|
+
'expect a string or a literal object,',
|
|
26
|
+
`got ${JSON.stringify(column)} at position ${i}`
|
|
27
|
+
]);
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
return normalizedColumns;
|
|
31
|
+
};
|
|
32
|
+
|
|
33
|
+
export {normalize_columns_array};
|
|
@@ -0,0 +1,448 @@
|
|
|
1
|
+
|
|
2
|
+
import {normalize_columns_array} from './normalize_columns_array.js';
|
|
3
|
+
import {CsvError} from './CsvError.js';
|
|
4
|
+
import {underscore} from '../utils/underscore.js';
|
|
5
|
+
|
|
6
|
+
const normalize_options = function(opts){
|
|
7
|
+
const options = {};
|
|
8
|
+
// Merge with user options
|
|
9
|
+
for(const opt in opts){
|
|
10
|
+
options[underscore(opt)] = opts[opt];
|
|
11
|
+
}
|
|
12
|
+
// Normalize option `encoding`
|
|
13
|
+
// Note: defined first because other options depends on it
|
|
14
|
+
// to convert chars/strings into buffers.
|
|
15
|
+
if(options.encoding === undefined || options.encoding === true){
|
|
16
|
+
options.encoding = 'utf8';
|
|
17
|
+
}else if(options.encoding === null || options.encoding === false){
|
|
18
|
+
options.encoding = null;
|
|
19
|
+
}else if(typeof options.encoding !== 'string' && options.encoding !== null){
|
|
20
|
+
throw new CsvError('CSV_INVALID_OPTION_ENCODING', [
|
|
21
|
+
'Invalid option encoding:',
|
|
22
|
+
'encoding must be a string or null to return a buffer,',
|
|
23
|
+
`got ${JSON.stringify(options.encoding)}`
|
|
24
|
+
], options);
|
|
25
|
+
}
|
|
26
|
+
// Normalize option `bom`
|
|
27
|
+
if(options.bom === undefined || options.bom === null || options.bom === false){
|
|
28
|
+
options.bom = false;
|
|
29
|
+
}else if(options.bom !== true){
|
|
30
|
+
throw new CsvError('CSV_INVALID_OPTION_BOM', [
|
|
31
|
+
'Invalid option bom:', 'bom must be true,',
|
|
32
|
+
`got ${JSON.stringify(options.bom)}`
|
|
33
|
+
], options);
|
|
34
|
+
}
|
|
35
|
+
// Normalize option `cast`
|
|
36
|
+
options.cast_function = null;
|
|
37
|
+
if(options.cast === undefined || options.cast === null || options.cast === false || options.cast === ''){
|
|
38
|
+
options.cast = undefined;
|
|
39
|
+
}else if(typeof options.cast === 'function'){
|
|
40
|
+
options.cast_function = options.cast;
|
|
41
|
+
options.cast = true;
|
|
42
|
+
}else if(options.cast !== true){
|
|
43
|
+
throw new CsvError('CSV_INVALID_OPTION_CAST', [
|
|
44
|
+
'Invalid option cast:', 'cast must be true or a function,',
|
|
45
|
+
`got ${JSON.stringify(options.cast)}`
|
|
46
|
+
], options);
|
|
47
|
+
}
|
|
48
|
+
// Normalize option `cast_date`
|
|
49
|
+
if(options.cast_date === undefined || options.cast_date === null || options.cast_date === false || options.cast_date === ''){
|
|
50
|
+
options.cast_date = false;
|
|
51
|
+
}else if(options.cast_date === true){
|
|
52
|
+
options.cast_date = function(value){
|
|
53
|
+
const date = Date.parse(value);
|
|
54
|
+
return !isNaN(date) ? new Date(date) : value;
|
|
55
|
+
};
|
|
56
|
+
}else if (typeof options.cast_date !== 'function'){
|
|
57
|
+
throw new CsvError('CSV_INVALID_OPTION_CAST_DATE', [
|
|
58
|
+
'Invalid option cast_date:', 'cast_date must be true or a function,',
|
|
59
|
+
`got ${JSON.stringify(options.cast_date)}`
|
|
60
|
+
], options);
|
|
61
|
+
}
|
|
62
|
+
// Normalize option `columns`
|
|
63
|
+
options.cast_first_line_to_header = null;
|
|
64
|
+
if(options.columns === true){
|
|
65
|
+
// Fields in the first line are converted as-is to columns
|
|
66
|
+
options.cast_first_line_to_header = undefined;
|
|
67
|
+
}else if(typeof options.columns === 'function'){
|
|
68
|
+
options.cast_first_line_to_header = options.columns;
|
|
69
|
+
options.columns = true;
|
|
70
|
+
}else if(Array.isArray(options.columns)){
|
|
71
|
+
options.columns = normalize_columns_array(options.columns);
|
|
72
|
+
}else if(options.columns === undefined || options.columns === null || options.columns === false){
|
|
73
|
+
options.columns = false;
|
|
74
|
+
}else{
|
|
75
|
+
throw new CsvError('CSV_INVALID_OPTION_COLUMNS', [
|
|
76
|
+
'Invalid option columns:',
|
|
77
|
+
'expect an array, a function or true,',
|
|
78
|
+
`got ${JSON.stringify(options.columns)}`
|
|
79
|
+
], options);
|
|
80
|
+
}
|
|
81
|
+
// Normalize option `group_columns_by_name`
|
|
82
|
+
if(options.group_columns_by_name === undefined || options.group_columns_by_name === null || options.group_columns_by_name === false){
|
|
83
|
+
options.group_columns_by_name = false;
|
|
84
|
+
}else if(options.group_columns_by_name !== true){
|
|
85
|
+
throw new CsvError('CSV_INVALID_OPTION_GROUP_COLUMNS_BY_NAME', [
|
|
86
|
+
'Invalid option group_columns_by_name:',
|
|
87
|
+
'expect an boolean,',
|
|
88
|
+
`got ${JSON.stringify(options.group_columns_by_name)}`
|
|
89
|
+
], options);
|
|
90
|
+
}else if(options.columns === false){
|
|
91
|
+
throw new CsvError('CSV_INVALID_OPTION_GROUP_COLUMNS_BY_NAME', [
|
|
92
|
+
'Invalid option group_columns_by_name:',
|
|
93
|
+
'the `columns` mode must be activated.'
|
|
94
|
+
], options);
|
|
95
|
+
}
|
|
96
|
+
// Normalize option `comment`
|
|
97
|
+
if(options.comment === undefined || options.comment === null || options.comment === false || options.comment === ''){
|
|
98
|
+
options.comment = null;
|
|
99
|
+
}else{
|
|
100
|
+
if(typeof options.comment === 'string'){
|
|
101
|
+
options.comment = Buffer.from(options.comment, options.encoding);
|
|
102
|
+
}
|
|
103
|
+
if(!Buffer.isBuffer(options.comment)){
|
|
104
|
+
throw new CsvError('CSV_INVALID_OPTION_COMMENT', [
|
|
105
|
+
'Invalid option comment:',
|
|
106
|
+
'comment must be a buffer or a string,',
|
|
107
|
+
`got ${JSON.stringify(options.comment)}`
|
|
108
|
+
], options);
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
// Normalize option `comment_no_infix`
|
|
112
|
+
if(options.comment_no_infix === undefined || options.comment_no_infix === null || options.comment_no_infix === false){
|
|
113
|
+
options.comment_no_infix = false;
|
|
114
|
+
}else if(options.comment_no_infix !== true){
|
|
115
|
+
throw new CsvError('CSV_INVALID_OPTION_COMMENT', [
|
|
116
|
+
'Invalid option comment_no_infix:',
|
|
117
|
+
'value must be a boolean,',
|
|
118
|
+
`got ${JSON.stringify(options.comment_no_infix)}`
|
|
119
|
+
], options);
|
|
120
|
+
}
|
|
121
|
+
// Normalize option `delimiter`
|
|
122
|
+
const delimiter_json = JSON.stringify(options.delimiter);
|
|
123
|
+
if(!Array.isArray(options.delimiter)) options.delimiter = [options.delimiter];
|
|
124
|
+
if(options.delimiter.length === 0){
|
|
125
|
+
throw new CsvError('CSV_INVALID_OPTION_DELIMITER', [
|
|
126
|
+
'Invalid option delimiter:',
|
|
127
|
+
'delimiter must be a non empty string or buffer or array of string|buffer,',
|
|
128
|
+
`got ${delimiter_json}`
|
|
129
|
+
], options);
|
|
130
|
+
}
|
|
131
|
+
options.delimiter = options.delimiter.map(function(delimiter){
|
|
132
|
+
if(delimiter === undefined || delimiter === null || delimiter === false){
|
|
133
|
+
return Buffer.from(',', options.encoding);
|
|
134
|
+
}
|
|
135
|
+
if(typeof delimiter === 'string'){
|
|
136
|
+
delimiter = Buffer.from(delimiter, options.encoding);
|
|
137
|
+
}
|
|
138
|
+
if(!Buffer.isBuffer(delimiter) || delimiter.length === 0){
|
|
139
|
+
throw new CsvError('CSV_INVALID_OPTION_DELIMITER', [
|
|
140
|
+
'Invalid option delimiter:',
|
|
141
|
+
'delimiter must be a non empty string or buffer or array of string|buffer,',
|
|
142
|
+
`got ${delimiter_json}`
|
|
143
|
+
], options);
|
|
144
|
+
}
|
|
145
|
+
return delimiter;
|
|
146
|
+
});
|
|
147
|
+
// Normalize option `escape`
|
|
148
|
+
if(options.escape === undefined || options.escape === true){
|
|
149
|
+
options.escape = Buffer.from('"', options.encoding);
|
|
150
|
+
}else if(typeof options.escape === 'string'){
|
|
151
|
+
options.escape = Buffer.from(options.escape, options.encoding);
|
|
152
|
+
}else if (options.escape === null || options.escape === false){
|
|
153
|
+
options.escape = null;
|
|
154
|
+
}
|
|
155
|
+
if(options.escape !== null){
|
|
156
|
+
if(!Buffer.isBuffer(options.escape)){
|
|
157
|
+
throw new Error(`Invalid Option: escape must be a buffer, a string or a boolean, got ${JSON.stringify(options.escape)}`);
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
// Normalize option `from`
|
|
161
|
+
if(options.from === undefined || options.from === null){
|
|
162
|
+
options.from = 1;
|
|
163
|
+
}else{
|
|
164
|
+
if(typeof options.from === 'string' && /\d+/.test(options.from)){
|
|
165
|
+
options.from = parseInt(options.from);
|
|
166
|
+
}
|
|
167
|
+
if(Number.isInteger(options.from)){
|
|
168
|
+
if(options.from < 0){
|
|
169
|
+
throw new Error(`Invalid Option: from must be a positive integer, got ${JSON.stringify(opts.from)}`);
|
|
170
|
+
}
|
|
171
|
+
}else{
|
|
172
|
+
throw new Error(`Invalid Option: from must be an integer, got ${JSON.stringify(options.from)}`);
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
// Normalize option `from_line`
|
|
176
|
+
if(options.from_line === undefined || options.from_line === null){
|
|
177
|
+
options.from_line = 1;
|
|
178
|
+
}else{
|
|
179
|
+
if(typeof options.from_line === 'string' && /\d+/.test(options.from_line)){
|
|
180
|
+
options.from_line = parseInt(options.from_line);
|
|
181
|
+
}
|
|
182
|
+
if(Number.isInteger(options.from_line)){
|
|
183
|
+
if(options.from_line <= 0){
|
|
184
|
+
throw new Error(`Invalid Option: from_line must be a positive integer greater than 0, got ${JSON.stringify(opts.from_line)}`);
|
|
185
|
+
}
|
|
186
|
+
}else{
|
|
187
|
+
throw new Error(`Invalid Option: from_line must be an integer, got ${JSON.stringify(opts.from_line)}`);
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
// Normalize options `ignore_last_delimiters`
|
|
191
|
+
if(options.ignore_last_delimiters === undefined || options.ignore_last_delimiters === null){
|
|
192
|
+
options.ignore_last_delimiters = false;
|
|
193
|
+
}else if(typeof options.ignore_last_delimiters === 'number'){
|
|
194
|
+
options.ignore_last_delimiters = Math.floor(options.ignore_last_delimiters);
|
|
195
|
+
if(options.ignore_last_delimiters === 0){
|
|
196
|
+
options.ignore_last_delimiters = false;
|
|
197
|
+
}
|
|
198
|
+
}else if(typeof options.ignore_last_delimiters !== 'boolean'){
|
|
199
|
+
throw new CsvError('CSV_INVALID_OPTION_IGNORE_LAST_DELIMITERS', [
|
|
200
|
+
'Invalid option `ignore_last_delimiters`:',
|
|
201
|
+
'the value must be a boolean value or an integer,',
|
|
202
|
+
`got ${JSON.stringify(options.ignore_last_delimiters)}`
|
|
203
|
+
], options);
|
|
204
|
+
}
|
|
205
|
+
if(options.ignore_last_delimiters === true && options.columns === false){
|
|
206
|
+
throw new CsvError('CSV_IGNORE_LAST_DELIMITERS_REQUIRES_COLUMNS', [
|
|
207
|
+
'The option `ignore_last_delimiters`',
|
|
208
|
+
'requires the activation of the `columns` option'
|
|
209
|
+
], options);
|
|
210
|
+
}
|
|
211
|
+
// Normalize option `info`
|
|
212
|
+
if(options.info === undefined || options.info === null || options.info === false){
|
|
213
|
+
options.info = false;
|
|
214
|
+
}else if(options.info !== true){
|
|
215
|
+
throw new Error(`Invalid Option: info must be true, got ${JSON.stringify(options.info)}`);
|
|
216
|
+
}
|
|
217
|
+
// Normalize option `max_record_size`
|
|
218
|
+
if(options.max_record_size === undefined || options.max_record_size === null || options.max_record_size === false){
|
|
219
|
+
options.max_record_size = 0;
|
|
220
|
+
}else if(Number.isInteger(options.max_record_size) && options.max_record_size >= 0){
|
|
221
|
+
// Great, nothing to do
|
|
222
|
+
}else if(typeof options.max_record_size === 'string' && /\d+/.test(options.max_record_size)){
|
|
223
|
+
options.max_record_size = parseInt(options.max_record_size);
|
|
224
|
+
}else{
|
|
225
|
+
throw new Error(`Invalid Option: max_record_size must be a positive integer, got ${JSON.stringify(options.max_record_size)}`);
|
|
226
|
+
}
|
|
227
|
+
// Normalize option `objname`
|
|
228
|
+
if(options.objname === undefined || options.objname === null || options.objname === false){
|
|
229
|
+
options.objname = undefined;
|
|
230
|
+
}else if(Buffer.isBuffer(options.objname)){
|
|
231
|
+
if(options.objname.length === 0){
|
|
232
|
+
throw new Error(`Invalid Option: objname must be a non empty buffer`);
|
|
233
|
+
}
|
|
234
|
+
if(options.encoding === null){
|
|
235
|
+
// Don't call `toString`, leave objname as a buffer
|
|
236
|
+
}else{
|
|
237
|
+
options.objname = options.objname.toString(options.encoding);
|
|
238
|
+
}
|
|
239
|
+
}else if(typeof options.objname === 'string'){
|
|
240
|
+
if(options.objname.length === 0){
|
|
241
|
+
throw new Error(`Invalid Option: objname must be a non empty string`);
|
|
242
|
+
}
|
|
243
|
+
// Great, nothing to do
|
|
244
|
+
}else if(typeof options.objname === 'number'){
|
|
245
|
+
// if(options.objname.length === 0){
|
|
246
|
+
// throw new Error(`Invalid Option: objname must be a non empty string`);
|
|
247
|
+
// }
|
|
248
|
+
// Great, nothing to do
|
|
249
|
+
}else{
|
|
250
|
+
throw new Error(`Invalid Option: objname must be a string or a buffer, got ${options.objname}`);
|
|
251
|
+
}
|
|
252
|
+
if(options.objname !== undefined){
|
|
253
|
+
if(typeof options.objname === 'number'){
|
|
254
|
+
if(options.columns !== false){
|
|
255
|
+
throw Error('Invalid Option: objname index cannot be combined with columns or be defined as a field');
|
|
256
|
+
}
|
|
257
|
+
}else{ // A string or a buffer
|
|
258
|
+
if(options.columns === false){
|
|
259
|
+
throw Error('Invalid Option: objname field must be combined with columns or be defined as an index');
|
|
260
|
+
}
|
|
261
|
+
}
|
|
262
|
+
}
|
|
263
|
+
// Normalize option `on_record`
|
|
264
|
+
if(options.on_record === undefined || options.on_record === null){
|
|
265
|
+
options.on_record = undefined;
|
|
266
|
+
}else if(typeof options.on_record !== 'function'){
|
|
267
|
+
throw new CsvError('CSV_INVALID_OPTION_ON_RECORD', [
|
|
268
|
+
'Invalid option `on_record`:',
|
|
269
|
+
'expect a function,',
|
|
270
|
+
`got ${JSON.stringify(options.on_record)}`
|
|
271
|
+
], options);
|
|
272
|
+
}
|
|
273
|
+
// Normalize option `quote`
|
|
274
|
+
if(options.quote === null || options.quote === false || options.quote === ''){
|
|
275
|
+
options.quote = null;
|
|
276
|
+
}else{
|
|
277
|
+
if(options.quote === undefined || options.quote === true){
|
|
278
|
+
options.quote = Buffer.from('"', options.encoding);
|
|
279
|
+
}else if(typeof options.quote === 'string'){
|
|
280
|
+
options.quote = Buffer.from(options.quote, options.encoding);
|
|
281
|
+
}
|
|
282
|
+
if(!Buffer.isBuffer(options.quote)){
|
|
283
|
+
throw new Error(`Invalid Option: quote must be a buffer or a string, got ${JSON.stringify(options.quote)}`);
|
|
284
|
+
}
|
|
285
|
+
}
|
|
286
|
+
// Normalize option `raw`
|
|
287
|
+
if(options.raw === undefined || options.raw === null || options.raw === false){
|
|
288
|
+
options.raw = false;
|
|
289
|
+
}else if(options.raw !== true){
|
|
290
|
+
throw new Error(`Invalid Option: raw must be true, got ${JSON.stringify(options.raw)}`);
|
|
291
|
+
}
|
|
292
|
+
// Normalize option `record_delimiter`
|
|
293
|
+
if(options.record_delimiter === undefined){
|
|
294
|
+
options.record_delimiter = [];
|
|
295
|
+
}else if(typeof options.record_delimiter === 'string' || Buffer.isBuffer(options.record_delimiter)){
|
|
296
|
+
if(options.record_delimiter.length === 0){
|
|
297
|
+
throw new CsvError('CSV_INVALID_OPTION_RECORD_DELIMITER', [
|
|
298
|
+
'Invalid option `record_delimiter`:',
|
|
299
|
+
'value must be a non empty string or buffer,',
|
|
300
|
+
`got ${JSON.stringify(options.record_delimiter)}`
|
|
301
|
+
], options);
|
|
302
|
+
}
|
|
303
|
+
options.record_delimiter = [options.record_delimiter];
|
|
304
|
+
}else if(!Array.isArray(options.record_delimiter)){
|
|
305
|
+
throw new CsvError('CSV_INVALID_OPTION_RECORD_DELIMITER', [
|
|
306
|
+
'Invalid option `record_delimiter`:',
|
|
307
|
+
'value must be a string, a buffer or array of string|buffer,',
|
|
308
|
+
`got ${JSON.stringify(options.record_delimiter)}`
|
|
309
|
+
], options);
|
|
310
|
+
}
|
|
311
|
+
options.record_delimiter = options.record_delimiter.map(function(rd, i){
|
|
312
|
+
if(typeof rd !== 'string' && ! Buffer.isBuffer(rd)){
|
|
313
|
+
throw new CsvError('CSV_INVALID_OPTION_RECORD_DELIMITER', [
|
|
314
|
+
'Invalid option `record_delimiter`:',
|
|
315
|
+
'value must be a string, a buffer or array of string|buffer',
|
|
316
|
+
`at index ${i},`,
|
|
317
|
+
`got ${JSON.stringify(rd)}`
|
|
318
|
+
], options);
|
|
319
|
+
}else if(rd.length === 0){
|
|
320
|
+
throw new CsvError('CSV_INVALID_OPTION_RECORD_DELIMITER', [
|
|
321
|
+
'Invalid option `record_delimiter`:',
|
|
322
|
+
'value must be a non empty string or buffer',
|
|
323
|
+
`at index ${i},`,
|
|
324
|
+
`got ${JSON.stringify(rd)}`
|
|
325
|
+
], options);
|
|
326
|
+
}
|
|
327
|
+
if(typeof rd === 'string'){
|
|
328
|
+
rd = Buffer.from(rd, options.encoding);
|
|
329
|
+
}
|
|
330
|
+
return rd;
|
|
331
|
+
});
|
|
332
|
+
// Normalize option `relax_column_count`
|
|
333
|
+
if(typeof options.relax_column_count === 'boolean'){
|
|
334
|
+
// Great, nothing to do
|
|
335
|
+
}else if(options.relax_column_count === undefined || options.relax_column_count === null){
|
|
336
|
+
options.relax_column_count = false;
|
|
337
|
+
}else{
|
|
338
|
+
throw new Error(`Invalid Option: relax_column_count must be a boolean, got ${JSON.stringify(options.relax_column_count)}`);
|
|
339
|
+
}
|
|
340
|
+
if(typeof options.relax_column_count_less === 'boolean'){
|
|
341
|
+
// Great, nothing to do
|
|
342
|
+
}else if(options.relax_column_count_less === undefined || options.relax_column_count_less === null){
|
|
343
|
+
options.relax_column_count_less = false;
|
|
344
|
+
}else{
|
|
345
|
+
throw new Error(`Invalid Option: relax_column_count_less must be a boolean, got ${JSON.stringify(options.relax_column_count_less)}`);
|
|
346
|
+
}
|
|
347
|
+
if(typeof options.relax_column_count_more === 'boolean'){
|
|
348
|
+
// Great, nothing to do
|
|
349
|
+
}else if(options.relax_column_count_more === undefined || options.relax_column_count_more === null){
|
|
350
|
+
options.relax_column_count_more = false;
|
|
351
|
+
}else{
|
|
352
|
+
throw new Error(`Invalid Option: relax_column_count_more must be a boolean, got ${JSON.stringify(options.relax_column_count_more)}`);
|
|
353
|
+
}
|
|
354
|
+
// Normalize option `relax_quotes`
|
|
355
|
+
if(typeof options.relax_quotes === 'boolean'){
|
|
356
|
+
// Great, nothing to do
|
|
357
|
+
}else if(options.relax_quotes === undefined || options.relax_quotes === null){
|
|
358
|
+
options.relax_quotes = false;
|
|
359
|
+
}else{
|
|
360
|
+
throw new Error(`Invalid Option: relax_quotes must be a boolean, got ${JSON.stringify(options.relax_quotes)}`);
|
|
361
|
+
}
|
|
362
|
+
// Normalize option `skip_empty_lines`
|
|
363
|
+
if(typeof options.skip_empty_lines === 'boolean'){
|
|
364
|
+
// Great, nothing to do
|
|
365
|
+
}else if(options.skip_empty_lines === undefined || options.skip_empty_lines === null){
|
|
366
|
+
options.skip_empty_lines = false;
|
|
367
|
+
}else{
|
|
368
|
+
throw new Error(`Invalid Option: skip_empty_lines must be a boolean, got ${JSON.stringify(options.skip_empty_lines)}`);
|
|
369
|
+
}
|
|
370
|
+
// Normalize option `skip_records_with_empty_values`
|
|
371
|
+
if(typeof options.skip_records_with_empty_values === 'boolean'){
|
|
372
|
+
// Great, nothing to do
|
|
373
|
+
}else if(options.skip_records_with_empty_values === undefined || options.skip_records_with_empty_values === null){
|
|
374
|
+
options.skip_records_with_empty_values = false;
|
|
375
|
+
}else{
|
|
376
|
+
throw new Error(`Invalid Option: skip_records_with_empty_values must be a boolean, got ${JSON.stringify(options.skip_records_with_empty_values)}`);
|
|
377
|
+
}
|
|
378
|
+
// Normalize option `skip_records_with_error`
|
|
379
|
+
if(typeof options.skip_records_with_error === 'boolean'){
|
|
380
|
+
// Great, nothing to do
|
|
381
|
+
}else if(options.skip_records_with_error === undefined || options.skip_records_with_error === null){
|
|
382
|
+
options.skip_records_with_error = false;
|
|
383
|
+
}else{
|
|
384
|
+
throw new Error(`Invalid Option: skip_records_with_error must be a boolean, got ${JSON.stringify(options.skip_records_with_error)}`);
|
|
385
|
+
}
|
|
386
|
+
// Normalize option `rtrim`
|
|
387
|
+
if(options.rtrim === undefined || options.rtrim === null || options.rtrim === false){
|
|
388
|
+
options.rtrim = false;
|
|
389
|
+
}else if(options.rtrim !== true){
|
|
390
|
+
throw new Error(`Invalid Option: rtrim must be a boolean, got ${JSON.stringify(options.rtrim)}`);
|
|
391
|
+
}
|
|
392
|
+
// Normalize option `ltrim`
|
|
393
|
+
if(options.ltrim === undefined || options.ltrim === null || options.ltrim === false){
|
|
394
|
+
options.ltrim = false;
|
|
395
|
+
}else if(options.ltrim !== true){
|
|
396
|
+
throw new Error(`Invalid Option: ltrim must be a boolean, got ${JSON.stringify(options.ltrim)}`);
|
|
397
|
+
}
|
|
398
|
+
// Normalize option `trim`
|
|
399
|
+
if(options.trim === undefined || options.trim === null || options.trim === false){
|
|
400
|
+
options.trim = false;
|
|
401
|
+
}else if(options.trim !== true){
|
|
402
|
+
throw new Error(`Invalid Option: trim must be a boolean, got ${JSON.stringify(options.trim)}`);
|
|
403
|
+
}
|
|
404
|
+
// Normalize options `trim`, `ltrim` and `rtrim`
|
|
405
|
+
if(options.trim === true && opts.ltrim !== false){
|
|
406
|
+
options.ltrim = true;
|
|
407
|
+
}else if(options.ltrim !== true){
|
|
408
|
+
options.ltrim = false;
|
|
409
|
+
}
|
|
410
|
+
if(options.trim === true && opts.rtrim !== false){
|
|
411
|
+
options.rtrim = true;
|
|
412
|
+
}else if(options.rtrim !== true){
|
|
413
|
+
options.rtrim = false;
|
|
414
|
+
}
|
|
415
|
+
// Normalize option `to`
|
|
416
|
+
if(options.to === undefined || options.to === null){
|
|
417
|
+
options.to = -1;
|
|
418
|
+
}else{
|
|
419
|
+
if(typeof options.to === 'string' && /\d+/.test(options.to)){
|
|
420
|
+
options.to = parseInt(options.to);
|
|
421
|
+
}
|
|
422
|
+
if(Number.isInteger(options.to)){
|
|
423
|
+
if(options.to <= 0){
|
|
424
|
+
throw new Error(`Invalid Option: to must be a positive integer greater than 0, got ${JSON.stringify(opts.to)}`);
|
|
425
|
+
}
|
|
426
|
+
}else{
|
|
427
|
+
throw new Error(`Invalid Option: to must be an integer, got ${JSON.stringify(opts.to)}`);
|
|
428
|
+
}
|
|
429
|
+
}
|
|
430
|
+
// Normalize option `to_line`
|
|
431
|
+
if(options.to_line === undefined || options.to_line === null){
|
|
432
|
+
options.to_line = -1;
|
|
433
|
+
}else{
|
|
434
|
+
if(typeof options.to_line === 'string' && /\d+/.test(options.to_line)){
|
|
435
|
+
options.to_line = parseInt(options.to_line);
|
|
436
|
+
}
|
|
437
|
+
if(Number.isInteger(options.to_line)){
|
|
438
|
+
if(options.to_line <= 0){
|
|
439
|
+
throw new Error(`Invalid Option: to_line must be a positive integer greater than 0, got ${JSON.stringify(opts.to_line)}`);
|
|
440
|
+
}
|
|
441
|
+
}else{
|
|
442
|
+
throw new Error(`Invalid Option: to_line must be an integer, got ${JSON.stringify(opts.to_line)}`);
|
|
443
|
+
}
|
|
444
|
+
}
|
|
445
|
+
return options;
|
|
446
|
+
};
|
|
447
|
+
|
|
448
|
+
export {normalize_options};
|