-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtest.js
115 lines (89 loc) · 2.71 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
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
const test = require('tape')
const ndjson = require('.')
function toAsyncIterator (array) {
return (async function * () {
for (let i = 0; i < array.length; i++) {
yield new Promise(resolve => setTimeout(() => resolve(array[i])))
}
})()
}
function toUint8Array (str) {
const arr = new Uint8Array(str.length)
for (let i = 0; i < str.length; i++) {
arr[i] = str.charCodeAt(i)
}
return arr
}
test('should split 1 item from 1 chunk', async t => {
const source = toAsyncIterator(['{ "id": 1 }\n'])
const results = []
for await (const value of ndjson(source)) {
results.push(value)
}
t.deepEqual(results, [{ id: 1 }])
t.end()
})
test('should split 1 item from 2 chunks', async t => {
const source = toAsyncIterator(['{ "id', '": 1 }\n'])
const results = []
for await (const value of ndjson(source)) {
results.push(value)
}
t.deepEqual(results, [{ id: 1 }])
t.end()
})
test('should split 2 items from 2 chunks', async t => {
const source = toAsyncIterator(['{ "id": 1 }\n', '{ "id": 2 }'])
const results = []
for await (const value of ndjson(source)) {
results.push(value)
}
t.deepEqual(results, [{ id: 1 }, { id: 2 }])
t.end()
})
test('should split 2 items from 1 chunk', async t => {
const source = toAsyncIterator(['{ "id": 1 }\n{ "id": 2 }'])
const results = []
for await (const value of ndjson(source)) {
results.push(value)
}
t.deepEqual(results, [{ id: 1 }, { id: 2 }])
t.end()
})
test('should split 3 items from 2 chunks', async t => {
const source = toAsyncIterator(['{ "id": 1 }\n{ "i', 'd": 2 }', '\n{"id":3}'])
const results = []
for await (const value of ndjson(source)) {
results.push(value)
}
t.deepEqual(results, [{ id: 1 }, { id: 2 }, { id: 3 }])
t.end()
})
test('should split from Buffers', async t => {
const source = toAsyncIterator([Buffer.from('{ "id": 1 }\n{ "i'), Buffer.from('d": 2 }'), Buffer.from('\n{"id":3}')])
const results = []
for await (const value of ndjson(source)) {
results.push(value)
}
t.deepEqual(results, [{ id: 1 }, { id: 2 }, { id: 3 }])
t.end()
})
test('should split from Uint8Arrays', async t => {
const source = toAsyncIterator([toUint8Array('{ "id": 1 }\n{ "i'), toUint8Array('d": 2 }'), toUint8Array('\n{"id":3}')])
const results = []
for await (const value of ndjson(source)) {
results.push(value)
}
t.deepEqual(results, [{ id: 1 }, { id: 2 }, { id: 3 }])
t.end()
})
test('should round trip', async t => {
const input = '{"id":1}\n{"id":2}\n{"id":3}\n'
const source = toAsyncIterator([input])
const results = []
for await (const value of ndjson.stringify(ndjson.parse(source))) {
results.push(value)
}
t.equal(results.join(''), input)
t.end()
})