@@ -23,10 +23,7 @@ export const parse = (opts = {}) => {
2323 const options = { ...defaultOptions , ...opts }
2424 options . escapeChar ??= options . quoteChar
2525
26- let { header } = options
27- let headerLength = length ( header )
28-
29- let { newlineChar, delimiterChar } = options
26+ let { header, newlineChar, delimiterChar } = options
3027 const {
3128 quoteChar,
3229 escapeChar,
@@ -38,8 +35,9 @@ export const parse = (opts = {}) => {
3835 errorOnFieldsMismatch
3936 // errorOnFieldMalformed
4037 } = options
41- const delimiterCharRegExp = / , | \t | \| | ; | \x1E | \x1F / g // eslint-disable-line no-control-regex
42- const newlineCharRegExp = / \r \n | \n | \r / g
38+ let headerLength = length ( header )
39+ const detectDelimiterCharRegExp = / , | \t | \| | ; | \x1E | \x1F / g // eslint-disable-line no-control-regex
40+ const detectNewlineCharRegExp = / \r \n | \n | \r / g
4341
4442 const escapedQuoteChar = escapeChar + quoteChar
4543 const escapedQuoteCharRegExp = new RegExp (
@@ -138,21 +136,6 @@ export const parse = (opts = {}) => {
138136 }
139137 }
140138
141- const detectChar = ( chunk , pattern ) => {
142- let match
143- const chars = { }
144- while ( ( match = pattern . exec ( chunk ) ) ) {
145- const char = match [ 0 ]
146- chars [ char ] ??= 0
147- chars [ char ] += 1
148- if ( chars [ char ] > 5 ) return char
149- }
150- // pattern.lastIndex = 0 // not reused again
151- const { key } = Object . keys ( chars )
152- . map ( ( key ) => ( { key, value : chars [ key ] } ) )
153- . sort ( ( a , b ) => a . value - b . value ) [ 0 ]
154- return key
155- }
156139 const chunkParse = ( string , controller , flush = false ) => {
157140 chunk = string
158141 chunkLength = length ( chunk )
@@ -163,10 +146,16 @@ export const parse = (opts = {}) => {
163146
164147 // auto-detect
165148 if ( ! newlineChar ) {
166- newlineChar = detectChar ( chunk . substring ( 0 , 1024 ) , newlineCharRegExp )
149+ newlineChar = detectChar (
150+ chunk . substring ( 0 , 1024 ) ,
151+ detectNewlineCharRegExp
152+ )
167153 newlineCharLength = length ( newlineChar )
168154 }
169- delimiterChar ||= detectChar ( chunk . substring ( 0 , 1024 ) , delimiterCharRegExp )
155+ delimiterChar ||= detectChar (
156+ chunk . substring ( 0 , 1024 ) ,
157+ detectDelimiterCharRegExp
158+ )
170159
171160 checkForEmptyLine ( )
172161 let lineStart = 0
@@ -261,6 +250,24 @@ export const parse = (opts = {}) => {
261250 }
262251}
263252
253+ export const detectChar = ( chunk , pattern ) => {
254+ let match
255+ const chars = { }
256+ while ( ( match = pattern . exec ( chunk ) ) ) {
257+ const char = match [ 0 ]
258+ console . log ( { char, chars } )
259+ chars [ char ] ??= 0
260+ chars [ char ] += 1
261+ if ( chars [ char ] > 5 ) return char
262+ }
263+ // pattern.lastIndex = 0 // not reused again
264+ console . log ( pattern , chars , chunk )
265+ const { key } = Object . keys ( chars )
266+ . map ( ( key ) => ( { key, value : chars [ key ] } ) )
267+ . sort ( ( a , b ) => a . value - b . value ) [ 0 ]
268+ return key
269+ }
270+
264271export const coerceTo = {
265272 string : ( field ) => field ,
266273 boolean : ( field ) => {
0 commit comments