# iterable-ndjson

> ndjson to async iterator

Latest version **1.1.0** (published 2019-03-27) · MIT license · 0 weekly downloads

## Install

```sh
npm install iterable-ndjson
pnpm add iterable-ndjson
yarn add iterable-ndjson
bun add iterable-ndjson
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.1.0 |
| Published | 2019-03-27 |
| First published | 2019-02-12 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 1 |
| Unpacked size | 9.4 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 6 |
| Author | Alan Shaw |
| Maintainers | alanshaw |
| Keywords | ndjson, json, async, iterator, iterable |

## Links

- npm: https://www.npmjs.com/package/iterable-ndjson
- Repository: https://github.com/alanshaw/iterable-ndjson
- Homepage: https://github.com/alanshaw/iterable-ndjson#readme
- Issues: https://github.com/alanshaw/iterable-ndjson/issues
- npm.io page: https://npm.io/package/iterable-ndjson

## Dependencies (1)

- [string_decoder](https://npm.io/package/string_decoder.md) ^1.2.0

## Alternatives

- [@mapbox/jsonlint-lines-primitives](https://npm.io/package/@mapbox/jsonlint-lines-primitives.md) — 5.3M weekly downloads
- [reftools](https://npm.io/package/reftools.md) — 3.5M weekly downloads
- [@hey-api/openapi-ts](https://npm.io/package/@hey-api/openapi-ts.md) — 3.5M weekly downloads
- [@mapbox/geojson-rewind](https://npm.io/package/@mapbox/geojson-rewind.md) — 2.4M weekly downloads
- [turbo-stream](https://npm.io/package/turbo-stream.md) — 1.7M weekly downloads

## Recent versions

- 1.1.0 (latest) — 2019-03-27
- 1.0.1 — 2019-03-07
- 1.0.0 — 2019-02-12

## README

# iterable-ndjson

[![Build Status](https://travis-ci.org/alanshaw/iterable-ndjson.svg?branch=master)](https://travis-ci.org/alanshaw/iterable-ndjson)
[![dependencies Status](https://david-dm.org/alanshaw/iterable-ndjson/status.svg)](https://david-dm.org/alanshaw/iterable-ndjson)
[![JavaScript Style Guide](https://img.shields.io/badge/code_style-standard-brightgreen.svg)](https://standardjs.com)

> Takes an (async) iterable that yields ndjson and returns an async iterable that yields JS objects

## Install

```sh
npm install iterable-ndjson
```

## Usage

```js
const ndjson = require('iterable-ndjson')
const it = ndjson.parse(source) // where `source` is any iterable that yields ndjson

for await (const obj of it)
  console.log(obj)
```

### Examples

Node.js streams are async iterable:

```js
const ndjson = require('iterable-ndjson')
const fs = require('fs')
const source = fs.createReadStream('/path/to/file.ndjson')

for await (const obj of ndjson.parse(source))
  console.log(obj)
```

Async iterable:

```js
const ndjson = require('iterable-ndjson')

// An ndjson async iterator
const source = (() => {
  const array = ['{"id": 1}\n', '{"id"', ': 2}', '\n{"id": 3}\n']
  return {
    [Symbol.asyncIterator] () {
      return this
    },
    async next () {
      await new Promise(resolve => setTimeout(resolve))
      return array.length
        ? { done: false, value: array.shift() }
        : { done: true }
    }
  }
})()

async function main () {
  for await (const obj of ndjson.parse(source))
    console.log(obj)
    // Logs out:
    // { id: 1 }
    // { id: 2 }
    // { id: 3 }
}

main()
```

Async iterable generator:

```js
const ndjson = require('iterable-ndjson')

// An ndjson async iterator
const source = (async function * () {
  const array = ['{"id": 1}\n', '{"id"', ': 2}', '\n{"id": 3}\n']
  for (let i = 0; i < array.length; i++) {
    yield new Promise(resolve => setTimeout(() => resolve(array[i])))
  }
})()

async function main () {
  for await (const obj of ndjson.parse(source))
    console.log(obj)
    // Logs out:
    // { id: 1 }
    // { id: 2 }
    // { id: 3 }
}

main()
```

Regular iterable (like an array):

```js
const ndjson = require('iterable-ndjson')
const source = ['{"id": 1}\n', '{"id"', ': 2}', '\n{"id": 3}\n']

async function main () {
  for await (const obj of ndjson.parse(source))
    console.log(obj)
    // Logs out:
    // { id: 1 }
    // { id: 2 }
    // { id: 3 }
}

main()
```

Stringify JS objects to NDJSON:

```js
const ndjson = require('iterable-ndjson')
const source = [{ id: 1 }, { id: 2 }, { id: 3 }]

async function main () {
  for await (const obj of ndjson.stringify(source))
    console.log(obj)
    // Logs out:
    // '{"id":1}\n'
    // '{"id":2}\n'
    // '{"id":3}\n'
}

main()
```

## Contribute

Feel free to dive in! [Open an issue](https://github.com/alanshaw/iterable-ndjson/issues/new) or submit PRs.

## License

[MIT](LICENSE) © Alan Shaw

---
_Source: https://npm.io/package/iterable-ndjson · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
