1
1
const test = require ( 'ava' )
2
2
const ndjson = require ( '.' )
3
3
const { Buffer } = require ( 'buffer' )
4
-
5
- function toAsyncIterator ( array ) {
6
- return ( async function * ( ) {
7
- for ( let i = 0 ; i < array . length ; i ++ ) {
8
- yield new Promise ( resolve => setTimeout ( ( ) => resolve ( array [ i ] ) ) )
9
- }
10
- } ) ( )
4
+ const all = require ( 'it-all' )
5
+
6
+ /**
7
+ * @template T
8
+ * @param {T[] } array
9
+ * @returns {AsyncIterable<T> }
10
+ */
11
+ async function * toAsyncIterator ( array ) {
12
+ for ( let i = 0 ; i < array . length ; i ++ ) {
13
+ yield new Promise ( resolve => setTimeout ( ( ) => resolve ( array [ i ] ) ) )
14
+ }
11
15
}
12
16
17
+ /**
18
+ * @param {string } str
19
+ */
13
20
function toUint8Array ( str ) {
14
21
const arr = new Uint8Array ( str . length )
15
22
for ( let i = 0 ; i < str . length ; i ++ ) {
@@ -20,89 +27,64 @@ function toUint8Array (str) {
20
27
21
28
test ( 'should split 1 item from 1 chunk' , async t => {
22
29
const source = toAsyncIterator ( [ '{ "id": 1 }\n' ] )
23
- const results = [ ]
24
-
25
- for await ( const value of ndjson . parse ( source ) ) {
26
- results . push ( value )
27
- }
30
+ const results = await all ( ndjson . parse ( source ) )
28
31
29
32
t . deepEqual ( results , [ { id : 1 } ] )
30
33
} )
31
34
32
35
test ( 'should split 1 item from 2 chunks' , async t => {
33
36
const source = toAsyncIterator ( [ '{ "id' , '": 1 }\n' ] )
34
- const results = [ ]
35
-
36
- for await ( const value of ndjson . parse ( source ) ) {
37
- results . push ( value )
38
- }
37
+ const results = await all ( ndjson . parse ( source ) )
39
38
40
39
t . deepEqual ( results , [ { id : 1 } ] )
41
40
} )
42
41
43
42
test ( 'should split 2 items from 2 chunks' , async t => {
44
43
const source = toAsyncIterator ( [ '{ "id": 1 }\n' , '{ "id": 2 }' ] )
45
- const results = [ ]
46
-
47
- for await ( const value of ndjson . parse ( source ) ) {
48
- results . push ( value )
49
- }
44
+ const results = await all ( ndjson . parse ( source ) )
50
45
51
46
t . deepEqual ( results , [ { id : 1 } , { id : 2 } ] )
52
47
} )
53
48
54
49
test ( 'should split 2 items from 1 chunk' , async t => {
55
50
const source = toAsyncIterator ( [ '{ "id": 1 }\n{ "id": 2 }' ] )
56
- const results = [ ]
57
-
58
- for await ( const value of ndjson . parse ( source ) ) {
59
- results . push ( value )
60
- }
51
+ const results = await all ( ndjson . parse ( source ) )
61
52
62
53
t . deepEqual ( results , [ { id : 1 } , { id : 2 } ] )
63
54
} )
64
55
65
56
test ( 'should split 3 items from 2 chunks' , async t => {
66
57
const source = toAsyncIterator ( [ '{ "id": 1 }\n{ "i' , 'd": 2 }' , '\n{"id":3}' ] )
67
- const results = [ ]
68
-
69
- for await ( const value of ndjson . parse ( source ) ) {
70
- results . push ( value )
71
- }
58
+ const results = await all ( ndjson . parse ( source ) )
72
59
73
60
t . deepEqual ( results , [ { id : 1 } , { id : 2 } , { id : 3 } ] )
74
61
} )
75
62
76
63
test ( 'should split from Buffers' , async t => {
77
64
const source = toAsyncIterator ( [ Buffer . from ( '{ "id": 1 }\n{ "i' ) , Buffer . from ( 'd": 2 }' ) , Buffer . from ( '\n{"id":3}' ) ] )
78
- const results = [ ]
79
-
80
- for await ( const value of ndjson . parse ( source ) ) {
81
- results . push ( value )
82
- }
83
-
65
+ const results = await all ( ndjson . parse ( source ) )
84
66
t . deepEqual ( results , [ { id : 1 } , { id : 2 } , { id : 3 } ] )
85
67
} )
86
68
87
69
test ( 'should split from Uint8Arrays' , async t => {
88
70
const source = toAsyncIterator ( [ toUint8Array ( '{ "id": 1 }\n{ "i' ) , toUint8Array ( 'd": 2 }' ) , toUint8Array ( '\n{"id":3}' ) ] )
89
- const results = [ ]
90
-
91
- for await ( const value of ndjson . parse ( source ) ) {
92
- results . push ( value )
93
- }
71
+ const results = await all ( ndjson . parse ( source ) )
94
72
95
73
t . deepEqual ( results , [ { id : 1 } , { id : 2 } , { id : 3 } ] )
96
74
} )
97
75
98
76
test ( 'should round trip' , async t => {
99
77
const input = '{"id":1}\n{"id":2}\n{"id":3}\n'
100
78
const source = toAsyncIterator ( [ input ] )
101
- const results = [ ]
102
-
103
- for await ( const value of ndjson . stringify ( ndjson . parse ( source ) ) ) {
104
- results . push ( value )
105
- }
79
+ const results = await all ( ndjson . stringify ( ndjson . parse ( source ) ) )
106
80
107
81
t . is ( results . join ( '' ) , input )
108
82
} )
83
+
84
+ test ( 'should stringify trip' , async t => {
85
+ const input = [ { id : 1 } , { id : 2 } , { id : 3 } ]
86
+ const source = toAsyncIterator ( input )
87
+ const results = await all ( ndjson . stringify ( source ) )
88
+
89
+ t . is ( results . join ( '' ) , '{"id":1}\n{"id":2}\n{"id":3}\n' )
90
+ } )
0 commit comments