包详细信息

@studio/ndjson

Transforms to parse and stringify ndjson

json, ndjson, transform

自述文件

Studio ndjson

☯️ Transforms to parse and stringify ndjson.

This module also works in the browser using a naive stream shim for small Browserify bundles.

Usage

To stringify an object stream into ndjson:

const StringifyTransform = require('@studio/ndjson/stringify');

object_stream.pipe(new StringifyTransform()).pipe(process.stdout);

To parse ndjson into an object stream:

const ParseTransform = require('@studio/ndjson/parse');

process.stdin.pipe(new ParseTransform()).pipe(object_stream);

Install

❯ npm i @studio/ndjson

API

Require the transform you need:

  • @studio/ndjson/stringify: Exports the StringifyTransform class which reads objects and writes strings.
  • @studio/ndjson/parse: Exports the ParseTransform class which reads strings and writes objects.

The module main exports StringifyTransform and ParseTransform if you need both:

const { StringifyTransform, ParseTransform } = require('@studio/ndjson');

ParseTransform options

The ParseTransform constructor accepts an options object with these properties:

  • loose: Whether to ignore data before the first { in each line.
  • loose_out: A stream to receive data that was found before the first { in each line. Implies loose.

Error handling

If JSON.parse or JSON.stringify throw an error, the transform emits an error event with the error object having a code property with ERR_JSON_PARSE or ERR_JSON_STRINGIFY. For parse errors, the line property on the error is the string that could not be parsed.

Related modules

License

MIT

Made with ❤️ on 🌍

更新日志

Changes

2.1.0

  • 🍏 115d96d Pass line through to loose_out if JSON.parse fails
  • 🐛 cc497b8 Fix tests on new node
  • 1df2afc Install prettier
  • 83c7269 Upgrade Studio Changes
  • 9ea1ec3 Upgrade eslint and eslint-config
  • 961c065 Upgrade mocha and use new mochify with esbuild
  • 796f0c2 Use @sinonjs/referee-sinon

Released by Maximilian Antoni on 2024-01-11.

2.0.0

1.2.0

  • 🍏 Add more details to emitted errors

    • JSON.parse errors have code set to "ERR_JSON_PARSE" and line to the string causing the parse error.
    • JSON.stringify errors have code set to "ERR_JSON_STRINGIFY".

1.1.2

  • 📚 Add install instructions
  • 📚 Improve API documentation
  • 📚 Add related modules section

1.1.1

  • 🐛 Fix typo in git URL

1.1.0

  • 🍏 Add loose and loose_out options to parser

    • If loose is set to true, any data received before { is ignored.
    • If loose_out is given, it is expected to a stream. It receives any data that is found before {. Implies loose.

1.0.0

  • ✨ Initial release