iterable-ndjson
Takes an (async) iterable that yields ndjson and returns an async iterable that yields JS objects
Install
npm install iterable-ndjson
Usage
const ndjson = const it = ndjson // where `source` is any iterable that yields ndjson for await const obj of it console
Examples
Node.js streams are async iterable:
const ndjson = const fs = const source = fs for await const obj of ndjson console
Async iterable:
const ndjson = // An ndjson async iteratorconst source = { const array = '{"id": 1}\n' '{"id"' ': 2}' '\n{"id": 3}\n' return { return this } async { await return arraylength ? done: false value: array : done: true } } { for await const obj of ndjson console // Logs out: // { id: 1 } // { id: 2 } // { id: 3 }}
Async iterable generator:
const ndjson = // An ndjson async iteratorconst source = { const array = '{"id": 1}\n' '{"id"' ': 2}' '\n{"id": 3}\n' for let i = 0; i < arraylength; i++ } { for await const obj of ndjson console // Logs out: // { id: 1 } // { id: 2 } // { id: 3 }}
Regular iterable (like an array):
const ndjson = const source = '{"id": 1}\n' '{"id"' ': 2}' '\n{"id": 3}\n' { for await const obj of ndjson console // Logs out: // { id: 1 } // { id: 2 } // { id: 3 }}
Stringify JS objects to NDJSON:
const ndjson = const source = id: 1 id: 2 id: 3 { for await const obj of ndjson console // Logs out: // '{"id":1}\n' // '{"id":2}\n' // '{"id":3}\n'}
Contribute
Feel free to dive in! Open an issue or submit PRs.
License
MIT © Alan Shaw