Skip to content

Commit 57c5b9f

Browse files
committed
chore: clean up
1 parent b2e121a commit 57c5b9f

File tree

5 files changed

+37
-28
lines changed

5 files changed

+37
-28
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,7 @@ dist
113113
# IDE
114114
.idea
115115
*.iml
116+
.nova
116117

117118
# OS
118119
.DS_Store

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "csv-rex",
3-
"version": "0.3.0",
3+
"version": "0.3.1",
44
"description": "A tiny and fast CSV parser for JavaScript.",
55
"type": "module",
66
"files": [

parse.js

Lines changed: 30 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -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+
264271
export const coerceTo = {
265272
string: (field) => field,
266273
boolean: (field) => {

parse.test.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -330,9 +330,10 @@ for (const method of quoteMethods) {
330330
for (const method of quoteMethods) {
331331
test(`${method}: Should parse with { coerceField: (field) => ... }`, async (t) => {
332332
const coerceField = (field, idx) => Object.values(coerceTo)[idx](field)
333-
const options = { quoteChar: '\'', coerceField }
333+
const options = { quoteChar: "'", coerceField }
334334
const enqueue = sinon.spy()
335-
const chunk = 'string,boolean,integer,decimal,json,timestamp,_true,_false,_null\r\nstring,true,-1,-1.1,\'{"a":"b"}\',2022-07-30T04:46:24.466Z,true,false,null\r\n'
335+
const chunk =
336+
'string,boolean,integer,decimal,json,timestamp,_true,_false,_null\r\nstring,true,-1,-1.1,\'{"a":"b"}\',2022-07-30T04:46:24.466Z,true,false,null\r\n'
336337
const parser = parse(options)
337338
parser[method](chunk, { enqueue })
338339
deepEqual(enqueue.firstCall.args, [

0 commit comments

Comments
 (0)