11const test = require ( 'ava' )
22const ndjson = require ( '.' )
33const { 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+ }
1115}
1216
17+ /**
18+ * @param {string } str
19+ */
1320function toUint8Array ( str ) {
1421 const arr = new Uint8Array ( str . length )
1522 for ( let i = 0 ; i < str . length ; i ++ ) {
@@ -20,89 +27,64 @@ function toUint8Array (str) {
2027
2128test ( 'should split 1 item from 1 chunk' , async t => {
2229 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 ) )
2831
2932 t . deepEqual ( results , [ { id : 1 } ] )
3033} )
3134
3235test ( 'should split 1 item from 2 chunks' , async t => {
3336 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 ) )
3938
4039 t . deepEqual ( results , [ { id : 1 } ] )
4140} )
4241
4342test ( 'should split 2 items from 2 chunks' , async t => {
4443 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 ) )
5045
5146 t . deepEqual ( results , [ { id : 1 } , { id : 2 } ] )
5247} )
5348
5449test ( 'should split 2 items from 1 chunk' , async t => {
5550 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 ) )
6152
6253 t . deepEqual ( results , [ { id : 1 } , { id : 2 } ] )
6354} )
6455
6556test ( 'should split 3 items from 2 chunks' , async t => {
6657 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 ) )
7259
7360 t . deepEqual ( results , [ { id : 1 } , { id : 2 } , { id : 3 } ] )
7461} )
7562
7663test ( 'should split from Buffers' , async t => {
7764 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 ) )
8466 t . deepEqual ( results , [ { id : 1 } , { id : 2 } , { id : 3 } ] )
8567} )
8668
8769test ( 'should split from Uint8Arrays' , async t => {
8870 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 ) )
9472
9573 t . deepEqual ( results , [ { id : 1 } , { id : 2 } , { id : 3 } ] )
9674} )
9775
9876test ( 'should round trip' , async t => {
9977 const input = '{"id":1}\n{"id":2}\n{"id":3}\n'
10078 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 ) ) )
10680
10781 t . is ( results . join ( '' ) , input )
10882} )
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