Skip to content

Commit

Permalink
fix: update types for it-ndjson
Browse files Browse the repository at this point in the history
  • Loading branch information
achingbrain committed Jan 15, 2022
1 parent 9a1ec79 commit 5122172
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 50 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ Utility modules to make dealing with async iterators easier, some trivial, some
* [it-map](./packages/it-map) Map the output of an iterable
* [it-merge](./packages/it-merge) Treat multiple iterables as one
* [it-multipart](./packages/it-multipart) Parse multipart message bodies as an iterable
* [it-ndjson](./packages/it-ndjson) Parse multipart message bodies as an iterable
* [it-parallel](./packages/it-parallel) Take an iterable of functions that return promises and run them in parallel up to a concurrency limit
* [it-parallel-batch](./packages/it-parallel-batch) Take an iterable of functions that return promises and run them in parallel in batches
* [it-peekable](./packages/it-peekable) Peek/push an iterable
Expand Down
4 changes: 4 additions & 0 deletions packages/it-ndjson/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,8 @@ const values = [0, 1, 2, 3, 4]
const arr = await all(ndjson.stringify(values))

console.info(arr) // '0\n', '1\n', '2\n', '3\n', '4\n'

const res = await all(ndjson.parse(arr))

console.info(res) // [0, 1, 2, 3, 4]
```
3 changes: 2 additions & 1 deletion packages/it-ndjson/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,10 @@
"devDependencies": {
"ava": "^3.12.1",
"buffer": "^6.0.3",
"it-all": "^1.0.6",
"nyc": "^15.1.0",
"standard": "^16.0.3",
"typescript": "^4.0.2"
},
"types": "dist/src/index.d.ts"
"types": "dist/index.d.ts"
}
2 changes: 1 addition & 1 deletion packages/it-ndjson/parse.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* @param {AsyncIterable<Uint8Array> | Iterable<Uint8Array>} source
* @param {AsyncIterable<Uint8Array | string> | Iterable<Uint8Array | string>} source
*/
async function * parse (source) {
const matcher = /\r?\n/
Expand Down
78 changes: 30 additions & 48 deletions packages/it-ndjson/test.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,22 @@
const test = require('ava')
const ndjson = require('.')
const { Buffer } = require('buffer')

function toAsyncIterator (array) {
return (async function * () {
for (let i = 0; i < array.length; i++) {
yield new Promise(resolve => setTimeout(() => resolve(array[i])))
}
})()
const all = require('it-all')

/**
* @template T
* @param {T[]} array
* @returns {AsyncIterable<T>}
*/
async function * toAsyncIterator (array) {
for (let i = 0; i < array.length; i++) {
yield new Promise(resolve => setTimeout(() => resolve(array[i])))
}
}

/**
* @param {string} str
*/
function toUint8Array (str) {
const arr = new Uint8Array(str.length)
for (let i = 0; i < str.length; i++) {
Expand All @@ -20,89 +27,64 @@ function toUint8Array (str) {

test('should split 1 item from 1 chunk', async t => {
const source = toAsyncIterator(['{ "id": 1 }\n'])
const results = []

for await (const value of ndjson.parse(source)) {
results.push(value)
}
const results = await all(ndjson.parse(source))

t.deepEqual(results, [{ id: 1 }])
})

test('should split 1 item from 2 chunks', async t => {
const source = toAsyncIterator(['{ "id', '": 1 }\n'])
const results = []

for await (const value of ndjson.parse(source)) {
results.push(value)
}
const results = await all(ndjson.parse(source))

t.deepEqual(results, [{ id: 1 }])
})

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.parse(source)) {
results.push(value)
}
const results = await all(ndjson.parse(source))

t.deepEqual(results, [{ id: 1 }, { id: 2 }])
})

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.parse(source)) {
results.push(value)
}
const results = await all(ndjson.parse(source))

t.deepEqual(results, [{ id: 1 }, { id: 2 }])
})

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.parse(source)) {
results.push(value)
}
const results = await all(ndjson.parse(source))

t.deepEqual(results, [{ id: 1 }, { id: 2 }, { id: 3 }])
})

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.parse(source)) {
results.push(value)
}

const results = await all(ndjson.parse(source))
t.deepEqual(results, [{ id: 1 }, { id: 2 }, { id: 3 }])
})

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.parse(source)) {
results.push(value)
}
const results = await all(ndjson.parse(source))

t.deepEqual(results, [{ id: 1 }, { id: 2 }, { id: 3 }])
})

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)
}
const results = await all(ndjson.stringify(ndjson.parse(source)))

t.is(results.join(''), input)
})

test('should stringify trip', async t => {
const input = [{ id: 1 }, { id: 2 }, { id: 3 }]
const source = toAsyncIterator(input)
const results = await all(ndjson.stringify(source))

t.is(results.join(''), '{"id":1}\n{"id":2}\n{"id":3}\n')
})

0 comments on commit 5122172

Please sign in to comment.