-
Notifications
You must be signed in to change notification settings - Fork 271
/
Copy pathnormalize_options.js
212 lines (209 loc) · 7.52 KB
/
normalize_options.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
import { CsvError } from './CsvError.js';
import { normalize_columns } from './normalize_columns.js';
import { underscore } from '../utils/underscore.js';
const normalize_options = function(opts) {
const options = {};
// Merge with user options
for(const opt in opts){
options[underscore(opt)] = opts[opt];
}
// Normalize option `bom`
if(options.bom === undefined || options.bom === null || options.bom === false){
options.bom = false;
}else if(options.bom !== true){
return [new CsvError('CSV_OPTION_BOOLEAN_INVALID_TYPE', [
'option `bom` is optional and must be a boolean value,',
`got ${JSON.stringify(options.bom)}`
])];
}
// Normalize option `delimiter`
if(options.delimiter === undefined || options.delimiter === null){
options.delimiter = ',';
}else if(Buffer.isBuffer(options.delimiter)){
options.delimiter = options.delimiter.toString();
}else if(typeof options.delimiter !== 'string'){
return [new CsvError('CSV_OPTION_DELIMITER_INVALID_TYPE', [
'option `delimiter` must be a buffer or a string,',
`got ${JSON.stringify(options.delimiter)}`
])];
}
// Normalize option `quote`
if(options.quote === undefined || options.quote === null){
options.quote = '"';
}else if(options.quote === true){
options.quote = '"';
}else if(options.quote === false){
options.quote = '';
}else if (Buffer.isBuffer(options.quote)){
options.quote = options.quote.toString();
}else if(typeof options.quote !== 'string'){
return [new CsvError('CSV_OPTION_QUOTE_INVALID_TYPE', [
'option `quote` must be a boolean, a buffer or a string,',
`got ${JSON.stringify(options.quote)}`
])];
}
// Normalize option `quoted`
if(options.quoted === undefined || options.quoted === null){
options.quoted = false;
}else{
// todo
}
// Normalize option `escape_formulas`
if(options.escape_formulas === undefined || options.escape_formulas === null){
options.escape_formulas = false;
}else if(typeof options.escape_formulas !== 'boolean'){
return [new CsvError('CSV_OPTION_ESCAPE_FORMULAS_INVALID_TYPE', [
'option `escape_formulas` must be a boolean,',
`got ${JSON.stringify(options.escape_formulas)}`
])];
}
// Normalize option `quoted_empty`
if(options.quoted_empty === undefined || options.quoted_empty === null){
options.quoted_empty = undefined;
}else{
// todo
}
// Normalize option `quoted_match`
if(options.quoted_match === undefined || options.quoted_match === null || options.quoted_match === false){
options.quoted_match = null;
}else if(!Array.isArray(options.quoted_match)){
options.quoted_match = [options.quoted_match];
}
if(options.quoted_match){
for(const quoted_match of options.quoted_match){
const isString = typeof quoted_match === 'string';
const isRegExp = quoted_match instanceof RegExp;
if(!isString && !isRegExp){
return [Error(`Invalid Option: quoted_match must be a string or a regex, got ${JSON.stringify(quoted_match)}`)];
}
}
}
// Normalize option `quoted_string`
if(options.quoted_string === undefined || options.quoted_string === null){
options.quoted_string = false;
}else{
// todo
}
// Normalize option `eof`
if(options.eof === undefined || options.eof === null){
options.eof = true;
}else{
// todo
}
// Normalize option `escape`
if(options.escape === undefined || options.escape === null){
options.escape = '"';
}else if(Buffer.isBuffer(options.escape)){
options.escape = options.escape.toString();
}else if(typeof options.escape !== 'string'){
return [Error(`Invalid Option: escape must be a buffer or a string, got ${JSON.stringify(options.escape)}`)];
}
if (options.escape.length > 1){
return [Error(`Invalid Option: escape must be one character, got ${options.escape.length} characters`)];
}
// Normalize option `header`
if(options.header === undefined || options.header === null){
options.header = false;
}else{
// todo
}
// Normalize option `headers_as_comment`
if(!options.header_as_comment){
options.header_as_comment = false;
}else if(!options.comment?.length){
throw new CsvError('CSV_INVALID_OPTION_COMMENT', [
'Invalid option comment:',
'comment must be a non empty string or buffer when enable header_as_comment,',
`got ${JSON.stringify(options.comment)}`
], options);
}else{
if(Buffer.isBuffer(options.comment)){
options.comment = options.comment.toString();
}
if(typeof options.comment !== 'string'){
throw new CsvError('CSV_INVALID_OPTION_COMMENT', [
'Invalid option comment:',
'comment must be a buffer or a string when enable header_as_comment,',
`got ${JSON.stringify(options.comment)}`
], options);
}
options.header = true;
}
// Normalize option `columns`
const [errColumns, columns] = normalize_columns(options.columns);
if(errColumns !== undefined) return [errColumns];
options.columns = columns;
// Normalize option `quoted`
if(options.quoted === undefined || options.quoted === null){
options.quoted = false;
}else{
// todo
}
// Normalize option `cast`
if(options.cast === undefined || options.cast === null){
options.cast = {};
}else{
// todo
}
// Normalize option cast.bigint
if(options.cast.bigint === undefined || options.cast.bigint === null){
// Cast boolean to string by default
options.cast.bigint = value => '' + value;
}
// Normalize option cast.boolean
if(options.cast.boolean === undefined || options.cast.boolean === null){
// Cast boolean to string by default
options.cast.boolean = value => value ? '1' : '';
}
// Normalize option cast.date
if(options.cast.date === undefined || options.cast.date === null){
// Cast date to timestamp string by default
options.cast.date = value => '' + value.getTime();
}
// Normalize option cast.number
if(options.cast.number === undefined || options.cast.number === null){
// Cast number to string using native casting by default
options.cast.number = value => '' + value;
}
// Normalize option cast.object
if(options.cast.object === undefined || options.cast.object === null){
// Stringify object as JSON by default
options.cast.object = value => JSON.stringify(value);
}
// Normalize option cast.string
if(options.cast.string === undefined || options.cast.string === null){
// Leave string untouched
options.cast.string = function(value){return value;};
}
// Normalize option `on_record`
if(options.on_record !== undefined && typeof options.on_record !== 'function'){
return [Error(`Invalid Option: "on_record" must be a function.`)];
}
// Normalize option `record_delimiter`
if(options.record_delimiter === undefined || options.record_delimiter === null){
options.record_delimiter = '\n';
}else if(Buffer.isBuffer(options.record_delimiter)){
options.record_delimiter = options.record_delimiter.toString();
}else if(typeof options.record_delimiter !== 'string'){
return [Error(`Invalid Option: record_delimiter must be a buffer or a string, got ${JSON.stringify(options.record_delimiter)}`)];
}
switch(options.record_delimiter){
case 'unix':
options.record_delimiter = "\n";
break;
case 'mac':
options.record_delimiter = "\r";
break;
case 'windows':
options.record_delimiter = "\r\n";
break;
case 'ascii':
options.record_delimiter = "\u001e";
break;
case 'unicode':
options.record_delimiter = "\u2028";
break;
}
return [undefined, options];
};
export {normalize_options};