Skip to content

Commit 5122172

Browse files
committed
fix: update types for it-ndjson
1 parent 9a1ec79 commit 5122172

File tree

5 files changed

+38
-50
lines changed

5 files changed

+38
-50
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ Utility modules to make dealing with async iterators easier, some trivial, some
1818
* [it-map](./packages/it-map) Map the output of an iterable
1919
* [it-merge](./packages/it-merge) Treat multiple iterables as one
2020
* [it-multipart](./packages/it-multipart) Parse multipart message bodies as an iterable
21+
* [it-ndjson](./packages/it-ndjson) Parse multipart message bodies as an iterable
2122
* [it-parallel](./packages/it-parallel) Take an iterable of functions that return promises and run them in parallel up to a concurrency limit
2223
* [it-parallel-batch](./packages/it-parallel-batch) Take an iterable of functions that return promises and run them in parallel in batches
2324
* [it-peekable](./packages/it-peekable) Peek/push an iterable

packages/it-ndjson/README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,8 @@ const values = [0, 1, 2, 3, 4]
2222
const arr = await all(ndjson.stringify(values))
2323

2424
console.info(arr) // '0\n', '1\n', '2\n', '3\n', '4\n'
25+
26+
const res = await all(ndjson.parse(arr))
27+
28+
console.info(res) // [0, 1, 2, 3, 4]
2529
```

packages/it-ndjson/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,10 @@
2121
"devDependencies": {
2222
"ava": "^3.12.1",
2323
"buffer": "^6.0.3",
24+
"it-all": "^1.0.6",
2425
"nyc": "^15.1.0",
2526
"standard": "^16.0.3",
2627
"typescript": "^4.0.2"
2728
},
28-
"types": "dist/src/index.d.ts"
29+
"types": "dist/index.d.ts"
2930
}

packages/it-ndjson/parse.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* @param {AsyncIterable<Uint8Array> | Iterable<Uint8Array>} source
2+
* @param {AsyncIterable<Uint8Array | string> | Iterable<Uint8Array | string>} source
33
*/
44
async function * parse (source) {
55
const matcher = /\r?\n/

packages/it-ndjson/test.js

Lines changed: 30 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,22 @@
11
const test = require('ava')
22
const ndjson = require('.')
33
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+
}
1115
}
1216

17+
/**
18+
* @param {string} str
19+
*/
1320
function 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

2128
test('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

3235
test('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

4342
test('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

5449
test('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

6556
test('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

7663
test('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

8769
test('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

9876
test('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

Comments
 (0)