2.1.0 • Published 2 years ago
@studio/ndjson v2.1.0
Studio ndjson
☯️ Transforms to parse and stringify ndjson.
This module also works in the browser using a naive
streamshim 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/ndjsonAPI
Require the transform you need:
@studio/ndjson/stringify: Exports theStringifyTransformclass which reads objects and writes strings.@studio/ndjson/parse: Exports theParseTransformclass 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. Impliesloose.
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
- 👻 Studio Log: This module was refactored out of the logger for JavaScript Studio.
- 💧 Studio Browser Stream is a naive
streamshim for small Browserify bundles. - 📦 Studio Changes is used to create the changelog for this module.
License
MIT