forked from finnp/detect-csv
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.js
68 lines (53 loc) · 1.63 KB
/
test.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
var spectrum = require('csv-spectrum')
var test = require('tape')
var detect = require('./')
test('detect csvs from csv-spectrum (and reject jsons)', function (t) {
spectrum(function (err, data) {
if (err) throw err
data.forEach(function (datum) {
t.ok(detect(datum.csv), 'csv ' + datum.name)
t.notOk(detect(datum.json), 'json ' + datum.name)
})
t.end()
})
})
test('detect delimiters', function (t) {
var d
d = detect('a,b,c\n1,2,3') || {}
t.equals(d.delimiter, ',', ',')
d = detect('a;b;c\n\r1;2;3') || {}
t.equals(d.delimiter, ';', ';')
d = detect('"a,,,,,,,,,"\tb\tc\n1\t2\t3') || {}
t.equals(d.delimiter, '\t', '\\t and quotes')
t.end()
})
test('detect newlines', function (t) {
var d
d = detect('a,b,c\n1,2,3\n') || {}
t.equals(d.newline, '\n', '\\n')
d = detect('a,b,c\r1,2,3\r') || {}
t.equals(d.newline, '\r', '\\r')
t.end()
})
test('one column with quotes', function (t) {
var d
d = detect('"hello"\n"test"') || {}
t.equals(d.newline, '\n', 'newline')
t.notOk(d.delimiter, 'unknown delimiter') // ~false or null
d = detect('"hello ""you"""\n"hi"') || {}
t.equals(d.newline, '\n', 'newline')
t.notOk(d.delimiter, 'unknown delimiter') // ~false or null
d = detect('hello\nhello')
t.notOk(d, 'not quoted')
d = detect('"hello""\n"hello""')
t.notOk(d, 'wrong quotes')
t.end()
})
// should this considered be valid csv? {"a": 1, "b": 2, "c": 3}\n{"a": 1, "b": 2, "c": 3}
// it's like ['{"a": 1', '"b": 2' ... ]
// test('reject ndjson', function (t) {
// var d
// d = detect('{"a": 1, "b": 2, "c": 3}\n{"a": 1, "b": 2, "c": 3}')
// t.notOk(d)
// t.end()
// })