-
Notifications
You must be signed in to change notification settings - Fork 9
/
index.js
55 lines (49 loc) · 1.35 KB
/
index.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
module.exports = function detectCSV (chunk, opts) {
opts = opts || {}
if (Buffer.isBuffer(chunk)) chunk = chunk + ''
var delimiters = opts.delimiters || [',', ';', '\t', '|']
var newlines = opts.newlines || ['\n', '\r']
var lines = chunk.split(/[\n\r]+/g)
var delimiter = determineMost(lines[0], delimiters)
var newline = determineMost(chunk, newlines)
if (!delimiter) {
if (isQuoted(lines[0])) return { newline: newline }
return null
}
return {
delimiter: delimiter,
newline: newline
}
}
function determineMost (chunk, items) {
var ignoreString = false
var itemCount = {}
var maxValue = 0
var maxChar
var currValue
items.forEach(function (item) {
itemCount[item] = 0
})
for (var i = 0; i < chunk.length; i++) {
if (chunk[i] === '"') ignoreString = !ignoreString
else if (!ignoreString && chunk[i] in itemCount) {
currValue = ++itemCount[chunk[i]]
if (currValue > maxValue) {
maxValue = currValue
maxChar = chunk[i]
}
}
}
return maxChar
}
function isQuoted (chunk) {
// is correctly quoted
var nextQuote = false
if (chunk[0] !== '"') return false
if (chunk[chunk.length - 1] !== '"') return false
for (var i = 1; i < chunk.length - 1; i++) {
if (chunk[i] === '"') nextQuote = !nextQuote
else if (nextQuote) return false
}
return !nextQuote
}